-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathCore.js
964 lines (749 loc) · 22.2 KB
/
Core.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
/*!
@preserve
ATON Core service routines
@author Bruno Fanini
VHLab, CNR ISPC
==================================================================================*/
const fs = require('fs');
const path = require('path');
//const glob = require("glob");
const jsonpatch = require('fast-json-patch');
const del = require('del');
const makeDir = require('make-dir');
const { nanoid } = require('nanoid');
const fsx = require('fs-extra');
//const axios = require('axios');
//const chokidar = require('chokidar');
const fg = require('fast-glob');
const chalk = require('chalk');
const sharp = require("sharp");
const { networkInterfaces } = require('os');
// Authentication
/*
let passport = require('passport');
let Strategy = require('passport-local').Strategy;
const cookieParser = require('cookie-parser');
const session = require('express-session');
const FileStore = require('session-file-store')(session);
*/
// Local modules
const BaseAPI = require("./API/v1.js");
const Maat = require("./maat/Maat.js");
Core = {};
Core.DIR_DATA = path.join(__dirname,"/../data/");
Core.DIR_WAPPS = path.join(__dirname,"/../wapps/");
Core.DIR_PUBLIC = path.join(__dirname,"/../public/");
Core.DIR_RES = path.join(Core.DIR_PUBLIC,"res/");
Core.DIR_PRV = path.join(__dirname, "_prv/");
Core.DIR_CONFIG = path.join(__dirname, "/../config/");
Core.DIR_CONFIGPUB = path.join(Core.DIR_CONFIG, "public/");
//Core.DIR_CUST_MODS = path.join(Core.DIR_CONFIG,"modules/");
Core.DIR_CUST_CERTS = path.join(Core.DIR_CONFIG,"certs/");
Core.DIR_NODE_MODULES = path.join(__dirname, "/../node_modules");
//Core.DIR_APIDOC = path.join(__dirname, "/../API/");
Core.DIR_FE = path.join(Core.DIR_PUBLIC,"hathor/");
Core.DIR_BE = path.join(Core.DIR_PUBLIC,"shu/");
Core.DIR_COLLECTIONS = path.join(Core.DIR_DATA,"collections/"); //path.join(Core.DIR_PUBLIC,"collection/");
Core.DIR_SCENES = path.join(Core.DIR_DATA,"scenes/"); //path.join(Core.DIR_PUBLIC,"scenes/");
Core.DIR_EXAMPLES = path.join(Core.DIR_PUBLIC,"examples/");
Core.DIR_FLARES = path.join(Core.DIR_CONFIG,"flares/"); //path.join(Core.DIR_PUBLIC,"custom/flares/");
Core.STD_SCENEFILE = "scene.json";
Core.STD_PUBFILE = "pub.txt"; // deprecated
Core.STD_COVERFILE_HI = "cover.png";
Core.STD_COVERFILE = "cover.jpg";
Core.STD_COVERSIZE = 256;
Core.STD_COVERFILE_PATH = path.join(Core.DIR_RES,"scenecover.png");
// Unused
Core.STATUS_COMPLETE = "complete";
Core.STATUS_PROCESSING = "processing";
Core.config = undefined; // main config
Core.users = []; // users config
Core.SCENES_GLOB_OPTS = {
cwd: Core.DIR_SCENES,
follow: true
};
Core.COLLECTIONS_GLOB_OPTS = {
cwd: Core.DIR_COLLECTIONS,
follow: true
};
// Modules setup
Core.realizeBaseAPI = BaseAPI;
//Core.passport = passport; // set configured passport
Core.Maat = Maat;
// LOG Utils
Core.logGreen = (str)=>{
console.log(chalk.green(str));
};
Core.logYellow = (str)=>{
console.log(chalk.yellow(str));
};
// Flares
Core.flares = {};
Core.setupFlares = (app)=>{
if (!fs.existsSync(Core.DIR_FLARES)) return;
// From flares
let O = {};
O.cwd = Core.DIR_FLARES; //Core.DIR_PUBLIC;
O.follow = true;
// Collect all flares
let plugins = fg.sync("**/flare.json", O);
for (let f in plugins){
let flarename = path.dirname(plugins[f]);
let pp = Core.DIR_FLARES + plugins[f];
//console.log(">> "+pp)
let P = JSON.parse(fs.readFileSync(pp, 'utf8'));
let fbasepath = Core.DIR_FLARES + flarename + "/";
//Core.flares.push( flarename );
// Client (public) components
/*
if (P.client){
for (let s in P.client.files) Core.FEScripts.push( "/flares/"+ flarename +"/"+ P.client.files[s] );
}
*/
// Server (private) components
if (P.server){
for (let m in P.server.modules){
let mname = P.server.modules[m];
const M = require(fbasepath + mname);
if (M.init) M.init(app);
}
}
if (P.respatterns && P.respatterns.length>2) Core.mpattern += ","+P.respatterns;
Core.flares[flarename] = P;
}
console.log("\nFlares (plugins) found: ");
console.log(Core.flares);
};
// Configs
//========================================
// Default main config
Core.CONF_MAIN = {
name: "", // name (ID) of this istance (star)
data: {
/*
collections: "",
scenes: "",
patterns: {
models: "*.gltf,*.glb,*.json"
}
*/
},
/* Additional scripts to load for Hathor front-end
hathor:{
scripts: []
},
*/
services: {
main: {
PORT: 8080, // main ATON port
PORT_S: 8083, // secure ATON port
pathCert: "", // custom path to cert
pathKey: "" // custom path to key
},
photon: {
PORT: 8890, // local Photon port
address: "ws://localhost"
//maxClientsPerSession: 50 // Max clients per scene
},
webdav: {
PORT: 8081
},
maat: {
PORT: 8891
},
/* Custom node modules (found in /config/modules/)
custom_modules:[
"myCustomModule.js"
]
*/
},
// Soon deprecated
landing: {
gallery: true, // Show gallery (public scenes) in the landing page
samples: true, // Show samples (def true)
//header: "", // Custom header (HTML partial)
//redirect: "", // Redirect to URL (e.g. specific web-app: "a/app_template")
//apps: [] // List of app IDs to show
},
shu: {
samples: true,
apps: ["app_template"], // List of apps to display
staffpick: {} // List of staff picked scene-IDs
}
};
// Default users config
// NOTE: this is a sample users config, you'll need to provide your own
Core.CONF_USERS = [
{
username: "ra",
password: "ra2020",
admin: true
},
{
username: "bastet",
password: "bas2020"
}
];
//=========================================
// Maat
// Maat as external service (NOT USED for now)
/*
Core.maatQuery = (str, onresponse)=>{
let q = Core._maatEP + str;
axios.get(q)
.then(res => {
//console.log(res.data);
if (onresponse) onresponse(res.data);
})
.catch(err => {
console.log('Error: ', err.message);
});
};
*/
// Main init routine
//==========================================================================
Core.init = ()=>{
Core.touchConfigFolders();
Core.config = Core.loadConfigFile("main.json", Core.CONF_MAIN);
Core.users = Core.loadConfigFile("users.json", Core.CONF_USERS);
// 3D models base formats
Core.mpattern = "*.gltf,*.glb,*tileset.json";
// Panoramic content
Core.panopattern = "*.jpg,*.hdr,*.exr,*.mp4,*.webm,*.m3u8";
// Media
Core.mediapattern = "*.jpg,*.png,*.mp4,*.webm,*.m3u8,*.wav,*.mp3";
if (Core.config.data && Core.config.data.patterns && Core.config.data.patterns.models){
Core.mpattern = Core.config.data.patterns.models;
console.log("Custom models pattern: "+Core.mpattern);
}
/*
if (Core.config.landing.header){
let srcpath = path.join(Core.DIR_CONFIG, Core.config.landing.header);
fs.readFile(srcpath, 'utf8', (err, data)=>{
if (err) throw err;
Core.config.landing.header = data;
console.log(data);
});
}
*/
Core.touchUserCollectionFolders();
//const maatport = (Core.config.services.maat)? Core.config.services.maat.PORT : 8891;
//Core._maatEP = "http://localhost:"+maatport+"/";
console.log("DB users: "+Core.users.length);
// Retrieve network interfaces
const nif = networkInterfaces();
Core.nets = {};
for (const name of Object.keys(nif)) {
for (const net of nif[name]) {
// Skip over non-IPv4 and internal (i.e. 127.0.0.1) addresses
if (net.family === 'IPv4' && !net.internal){
if (!Core.nets[name]) Core.nets[name] = [];
Core.nets[name].push(net.address);
}
}
}
Core.Maat.init();
// Directly from config
Core.FEScripts = [];
if (Core.config.hathor){
for (let s in Core.config.hathor.scripts) Core.FEScripts.push(Core.config.hathor.scripts[s]);
}
//Core.populateFEScripts();
};
// Touch config folders
Core.touchConfigFolders = ()=>{
if (!fs.existsSync(Core.DIR_CONFIG)) makeDir.sync(Core.DIR_CONFIG);
if (!fs.existsSync(Core.DIR_FLARES)){
try {
makeDir.sync(Core.DIR_FLARES);
} catch (e){
console.log(e);
return;
}
}
};
Core.loadConfigFile = (jsonfile, defconf)=>{
let customconfig = path.join(Core.DIR_CONFIG,jsonfile);
if (fs.existsSync(customconfig)){
let C = JSON.parse(fs.readFileSync(customconfig, 'utf8'));
console.log("Found custom config "+jsonfile);
return C;
}
// Custom config does not exist...
console.log("Custom config not found in "+Core.DIR_CONFIG+", using default...");
// Create custom config from default
fs.writeFileSync(customconfig, JSON.stringify(defconf, null, 4));
return defconf;
};
Core.touchCollectionFolder = (user)=>{
if (user === undefined) return;
let dirColl = path.join( Core.DIR_COLLECTIONS, user.username );
if (!fs.existsSync(dirColl)){
try {
makeDir.sync(dirColl);
} catch (e){
console.log(e);
return;
}
}
/*
let dirModels = path.join(dirColl,"/models/");
let dirPano = path.join(dirColl,"/pano/");
if (!fs.existsSync(dirModels)) makeDir.sync(dirModels);
if (!fs.existsSync(dirPano)) makeDir.sync(dirPano);
*/
};
Core.touchUserCollectionFolders = ()=>{
let len = Core.users.length;
for (let i = 0; i < len; i++){
let U = Core.users[i];
Core.touchCollectionFolder( U );
}
};
// SSL certs
Core.getCertPath = ()=>{
let cpath = Core.config.services.main.pathCert;
if (cpath && cpath.length>4) return cpath;
return path.join(Core.DIR_CUST_CERTS,'server.crt');
};
Core.getKeyPath = ()=>{
let cpath = Core.config.services.main.pathKey;
if (cpath && cpath.length>4) return cpath;
return path.join(Core.DIR_CUST_CERTS,'server.key');
};
// Users
//=======================================
Core.createNewUser = (entry)=>{
if (entry === undefined) return false;
// Add new entry into users json
Core.users = Core.loadConfigFile("users.json", Core.CONF_USERS);
Core.users.push(entry);
let uconfig = path.join(Core.DIR_CONFIG,"users.json");
fs.writeFileSync(uconfig, JSON.stringify(Core.users, null, 4));
Core.touchCollectionFolder(entry);
console.log("Created new user: "+entry);
return true;
};
//TODO:
Core.deleteUser = (username)=>{
if (username === undefined) return false;
Core.users = Core.loadConfigFile("users.json", Core.CONF_USERS);
let num = Core.users.length;
for (let u=0; u<num; u++){
if (Core.users[u].username === username){
Core.users.splice(u,1);
let uconfig = path.join(Core.DIR_CONFIG,"users.json");
fs.writeFileSync(uconfig, JSON.stringify(Core.users, null, 4));
return true;
}
}
return false;
};
// Utils
//=======================================
Core.generateTodayString = ()=>{
let today = new Date();
let dd = String( today.getDate() );
let mm = String( today.getMonth()+1 );
let yyyy = String( today.getFullYear() );
if(dd<10) dd = '0'+dd;
if(mm<10) mm = '0'+mm;
return yyyy+mm+dd;
};
// Readapted from: https://stackoverflow.com/questions/7616461/generate-a-hash-from-string-in-javascript
Core.hashCodeFromString = (str)=>{
let hash = 0, chr;
let len = str.length;
if (len === 0) return hash;
for (let i = 0; i < len; i++) {
chr = str.charCodeAt(i);
hash = ((hash << 5) - hash) + chr;
hash = hash & hash; // Convert to 32bit integer
}
return hash.toString(16);
};
Core.hashCodeFromString2 = (str)=>{
str = str.replaceAll(":","");
str = str.replaceAll("/","");
str = str.replaceAll(".","");
str.replace(/[aeiouAEIOU]/g, '');
console.log(str);
return str;
};
// TODO: improve
Core.isURL3Dmodel = (itempath)=>{
let mp = Core.mpattern;
mp = mp.replaceAll("*","");
let exts = mp.split(",");
for (let e=0; e<exts.length; e++){
if (itempath.endsWith( exts[e] )) return true;
}
return false;
};
// Scenes
//=======================================
Core.getSceneFolder = (sid)=>{
return path.join(Core.DIR_SCENES,sid);
};
Core.getSceneJSONPath = (sid)=>{
let jsonfile = path.join( Core.getSceneFolder(sid), Core.STD_SCENEFILE);
return jsonfile;
};
// Deprecated
Core.getPubFilePath = (sid)=>{
let pubfile = path.join( Core.getSceneFolder(sid), Core.STD_PUBFILE);
return pubfile;
};
// Check if scene exists on disk
Core.existsScene = (sid)=>{;
let b = fs.existsSync(Core.getSceneJSONPath(sid));
return b;
};
// Generate timestamped user SID
Core.generateUserSID = ()=>{
let sid = Core.generateTodayString() + '-' + nanoid(10);
return sid;
};
Core.createBasicScene = ()=>{
let sobj = {};
//sobj.status = Core.STATUS_COMPLETE;
sobj.scenegraph = {};
sobj.scenegraph.nodes = {};
sobj.scenegraph.nodes.main = {};
sobj.scenegraph.edges = {};
sobj.scenegraph.edges["."] = ["main"];
sobj.scenegraph.nodes.main.urls = [];
console.log(sobj);
return sobj;
};
Core.createBasicSceneFromModel = (user, mpath)=>{
console.log(user,mpath)
let sid = Core.hashCodeFromString(mpath);
sid = sid.replace("-","m");
sid = user + "/" + sid;
if (Core.existsScene(sid)) return sid;
let S = Core.createBasicScene();
S.scenegraph.nodes.main.urls.push( mpath );
Core.writeSceneJSON(sid, S);
return sid;
};
// Create sub-folder structure on disk
Core.touchSceneFolder = (sid)=>{
let D = Core.getSceneFolder(sid);
//if (!fs.existsSync(D)) fs.mkdirSync(D, { recursive: true }); // note: NodeJS > 12.0
if (!fs.existsSync(D)) makeDir.sync(D);
};
// Delete a scene folder
Core.deleteScene = (sid)=>{
let D = Core.getSceneFolder(sid);
console.log("Deleting "+D);
//if (fs.existsSync(D)) fs.rmdirSync(D, { recursive: true }); // note: NodeJS > 12.0
if (fs.existsSync(D)) del(D, {force: true});
};
Core.readSceneJSON = (sid)=>{
let jspath = Core.getSceneJSONPath(sid);
if (!fs.existsSync(jspath)) return undefined;
//let S = JSON.parse(fs.readFileSync(jspath, 'utf8'));
let F = fs.readFileSync(jspath, 'utf8');
let S = undefined;
try {
S = JSON.parse(F);
return S;
} catch(e) {
console.log("ERROR malformed scene: ", sid);
console.log(e);
return undefined;
}
};
// Apply partial edit to sobj
Core.addOBJEdit = (sobj, edit)=>{
//if (sobj === undefined) return undefined;
// object or array
if (typeof edit === "object"){
for (let k in edit){
let E = edit[k];
//if (Array.isArray(E)){
// sobj[k] = E;
//}
// Touch
if (sobj[k] === undefined){
//sobj[k] = {};
sobj[k] = Array.isArray(E)? [] : {};
}
sobj[k] = Core.addOBJEdit(sobj[k], E);
}
return sobj;
}
// not object
sobj = edit;
return sobj;
};
Core.deleteOBJEdit = (sobj, edit)=>{
if (sobj === undefined) return undefined;
// object or array
if (typeof edit === "object"){
for (let k in edit){
let E = edit[k];
//if (Array.isArray(sobj)) sobj = sobj.filter(e => e !== k);
if (sobj[k] !== undefined){
if (Object.keys(E).length > 0){
sobj[k] = Core.deleteOBJEdit(sobj[k], E);
}
else {
//if (Array.isArray(sobj)) sobj = sobj.filter(e => e !== k);
//else
delete sobj[k];
}
}
}
return sobj;
}
return undefined;
};
// Apply incoming patch to sid JSON
Core.applySceneEdit = (sid, patch, mode)=>{
let sjpath = Core.getSceneJSONPath(sid);
let S = Core.readSceneJSON(sid);
if (S === undefined) return; // scene does not exist
//jsonpatch.applyPatch(S, patch);
if (mode === "DEL") S = Core.deleteOBJEdit(S, patch);
else S = Core.addOBJEdit(S, patch);
S = Core.cleanScene(S);
fs.writeFileSync(sjpath, JSON.stringify(S)); // , null, 4
//console.log(S);
return S;
};
/*
Core.applySceneEdit = (M, sobj)=>{
let sid = M.sid;
let data = M.data;
let task = M.task;
if (sid === undefined) return false;
if (task === undefined) return false;
let sjpath = Core.getSceneJSONPath(sid);
let sobj = Core.readSceneJSON(sid);
if (sobj === undefined) return false; // scene does not exist
//if (task === "DEL") sobj = Core.deleteOBJEdit(sobj, patch);
//if (task === "ADD") sobj = Core.addOBJEdit(sobj, patch);
if (task === "UPD_SEM_NODE"){
let nid = data.nid;
let content = data.content;
if (nid === undefined) return;
if (sobj.semanticgraph[nid] === undefined) sobj.semanticgraph[nid] = {}; // touch
sobj.semanticgraph[nid] = content;
}
// write
sobj = Core.cleanScene(sobj);
fs.writeFileSync(sjpath, JSON.stringify(sobj)); // , null, 4
//console.log(sobj);
//return sobj;
return true;
};
*/
Core.cleanScene = (sobj)=>{
// semantic graph
if (sobj.semanticgraph && sobj.semanticgraph.edges){
for (let e in sobj.semanticgraph.edges){
let children = sobj.semanticgraph.edges[e];
for (let c in children){
let nid = children[c];
//console.log(nid);
if (sobj.semanticgraph.nodes === undefined || sobj.semanticgraph.nodes[nid] === undefined){
children.splice(c, 1);
}
}
}
}
// scene-graph
if (sobj.scenegraph && sobj.scenegraph.edges){
for (let e in sobj.scenegraph.edges){
let children = sobj.scenegraph.edges[e];
for (let c in children){
let nid = children[c];
//console.log(nid);
if (sobj.scenegraph.nodes === undefined || sobj.scenegraph.nodes[nid] === undefined){
children.splice(c, 1);
}
}
}
}
return sobj;
};
// Write scene JSON from sid and data
Core.writeSceneJSON = (sid, data, vis)=>{
if (sid === undefined) return false;
if (data === undefined) return false;
Core.touchSceneFolder(sid);
let sjpath = Core.getSceneJSONPath(sid);
if (vis) data.visibility = vis;
fs.writeFileSync(sjpath, JSON.stringify(data, null, 4));
/*
if (pub){
let pubfile = Core.getPubFilePath(sid);
fs.writeFileSync(pubfile, "");
}
*/
return true;
};
// Web-Apps
//=======================================
Core.getAppDataFolder = (wappid)=>{
return path.join( Core.DIR_WAPPS, wappid+"/data");
};
Core.getAppJSONPath = (wappid, fid)=>{
let jsonfile = Core.getAppDataFolder(wappid) + "/"+fid+".json";
console.log(jsonfile);
return jsonfile;
};
Core.readAppJSONData = (wappid, fid)=>{
if (!fs.existsSync(Core.getAppDataFolder(wappid))) return undefined;
let jsonfile = Core.getAppJSONPath(wappid, fid);
if (!fs.existsSync(jsonfile)){
fs.writeFileSync(jsonfile, "{}");
}
let S = JSON.parse(fs.readFileSync(jsonfile, 'utf8'));
return S;
};
Core.wappDataEdit = (wappid, fid, patch, mode)=>{
if (wappid === undefined) return undefined;
if (fid === undefined) return undefined;
let jdpath = Core.getAppJSONPath(wappid, fid);
let D = Core.readAppJSONData(wappid, fid);
if (D === undefined) return undefined; // data does not exist
//jsonpatch.applyPatch(D, patch);
if (mode === "DEL") D = Core.deleteOBJEdit(D, patch);
else D = Core.addOBJEdit(D, patch);
fs.writeFileSync(jdpath, JSON.stringify(D, null, 4));
//console.log(D);
return D;
};
Core.createClientUserAuthResponse = (req)=>{
if (req.user === undefined) return {};
let U = {};
U.username = req.user.username;
U.admin = req.user.admin;
U.webdav = 8081;
if (Core.config.services.webdav && Core.config.services.webdav.PORT) U.webdav = Core.config.services.webdav.PORT;
return U;
};
/*
Core.initUsers = (configfile)=>{
Core.users = Core.loadConfigFile(configfile);
console.log("DB users: "+Core.users.length);
};
*/
/*
Core.findByUsername = (username, cb)=>{
process.nextTick( function(){
// Load
Core.users = Core.maat.getUsers(); //Core.loadConfigFile("users.json", Core.CONF_USERS);
for (let i = 0, len = Core.users.length; i < len; i++){
let U = Core.users[i];
if (U.username === username) return cb(null, U);
}
return cb(null, null);
});
};
Core.findById = (id, cb)=>{
process.nextTick(()=>{
Core.users = Core.maat.getUsers(); //Core.loadConfigFile("users.json", Core.CONF_USERS);
if (Core.users[id]) cb(null, Core.users[id]);
else cb( new Error('User ' + id + ' does not exist') );
});
};
Core.setupPassport = ()=>{
passport.use( new Strategy((username, password, cb)=>{
Core.findByUsername(username, function(err, user) {
if (err) return cb(err);
if (!user) return cb(null, false);
if (user.password != password) return cb(null, false);
return cb(null, user);
});
}));
passport.serializeUser((user, cb)=>{
cb(null, Core.users.indexOf(user));
});
passport.deserializeUser((id, cb)=>{
Core.findById(id, (err, user)=>{
if (err) return cb(err);
cb(null, user);
});
});
};
Core.realizeAuth = (app)=>{
let fileStoreOptions = {
fileExtension: ".ses"
};
let bodyParser = require('body-parser');
app.use(bodyParser.json({ limit: '50mb' }));
app.use(bodyParser.urlencoded({ limit: '50mb', extended: true, parameterLimit: 50000 }));
//app.use(require('body-parser').urlencoded({ extended: true }));
app.use(cookieParser());
app.use(
session({
secret: 'shu',
//cookie: { maxAge: 1800000 }, // 60000 = 1 min
resave: true,
saveUninitialized: true,
//rolling: true
store: new FileStore(fileStoreOptions) // required for consistency in cluster mode
})
);
// Initialize Passport and restore authentication state, if any, from the session
app.use(passport.initialize());
app.use(passport.session());
};
*/
// DATA
//================================================
Core.setupDataRoute = (app)=>{
if (Core.config && Core.config.data){
const D = Core.config.data;
if (D.collections) Core.DIR_COLLECTIONS = D.collections;
if (D.scenes) Core.DIR_SCENES = D.scenes;
}
app.get(/^\/collections\/(.*)$/, function(req,res,next){
let path = req.params[0];
// auth here if needed
res.sendFile(Core.DIR_COLLECTIONS + path);
//console.log(Core.DIR_COLLECTIONS + path);
//next();
});
app.get(/^\/scenes\/(.*)$/, function(req,res,next){
let path = req.params[0];
// auth here if needed
res.sendFile(Core.DIR_SCENES + path);
//next();
});
};
// IMG
Core.generateCoverForScene = (sid, b64img, onComplete)=>{
if (!sid) return;
let scenefolder = Core.getSceneFolder(sid);
let coverfile = path.join(scenefolder, Core.STD_COVERFILE_HI);
let coverfileOpt = path.join(scenefolder, Core.STD_COVERFILE);
fs.writeFile(coverfile, b64img, 'base64', (err)=>{
//if (fs.existsSync(coverfileOpt)) fs.unlinkSync(coverfileOpt);
// Optimize PNG size
sharp(coverfile)
.resize({
width: Core.STD_COVERSIZE,
height: Core.STD_COVERSIZE
})
.withMetadata()
/*
.png({
quality: 90, // 0-100
//compression: 6, // this doesn't need to be set
})
*/
.jpeg({
quality: 60
})
.toFile(coverfileOpt, (err)=>{
if (err) console.log(err);
else if (onComplete) onComplete();
});
});
};
module.exports = Core;