-
Notifications
You must be signed in to change notification settings - Fork 31
/
api.js
926 lines (908 loc) · 32.2 KB
/
api.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
const fs = require('fs');
const path = require('path');
const he = require('he');
const imageSize = require('image-size');
const logic = require('./logic.js');
const config = require('./configLoader.js');
const l10n = require('./assets/l10n.json');
const supportedLanguages = require(`${require.main.path}/${config.folders.assets}/languageCatcher.js`);
let session = {
name: 'main-session',
language: 'en',
status: ''
}
module.exports = {
// load favicon.ico file
favicon: (request, response, next) => {
try {
const icon = fs.readFileSync(`${require.main.path}/favicon.ico`);
response.set('Content-Type', 'image/x-icon');
response.end(icon);
}
catch (error) {
handleError(error, response);
}
},
// renders dashboard
dashboard: async (request, response, next) => {
try {
manageSession(null, {
language: request.query?.language,
name: request.query?.session
});
const html = fs.readFileSync(`${require.main.path}/${config.folders.assets}/templates/dashboard.html`, 'utf-8');
const labels = await getLangLabels();
const languageFiles = logic.getFileList(`${config.folders.libraries}/h5p-editor-php-library/language`);
const languages = {};
for (let item of languageFiles) {
const key = item.match(/language\/(.*?)\.js/)?.[1];
languages[key] = supportedLanguages[key];
}
let input = {
assets: config.folders.assets,
api: config.api,
status: session.status,
language: session.language,
languages: JSON.stringify(languages)
}
input = {...input, ...labels};
response.set('Content-Type', 'text/html');
response.end(logic.fromTemplate(html, input));
session.status = '';
}
catch (error) {
handleError(error, response);
}
},
// lists runnable libraries
contentTypes: async (request, response, next) => {
try {
const registry = await logic.getRegistry();
const libraryDirs = await logic.parseLibraryFolders();
if (!registry.runnable) {
registry.runnable = {};
const list = [];
for (let item in registry.regular) {
if (registry.regular[item].runnable && libraryDirs[registry.regular[item].id]) {
list.push(item);
}
}
list.sort((a, b) => {
const first = a.toLowerCase();
const second = b.toLowerCase();
if (first < second) {
return -1;
}
if (first > second) {
return 1;
}
return 0;
});
for (let item of list) {
registry.runnable[item] = registry.regular[item];
}
}
response.set('Content-Type', 'application/json');
response.end(JSON.stringify(registry.runnable));
}
catch (error) {
handleError(error, response);
}
},
// updates session file used for resume functionality
setUserData: (request, response, next) => {
try {
manageSession(request.params.folder, {
language: request.query?.language,
name: request.query?.session
});
if (session.name == 'null') {
response.set('Content-Type', 'application/json');
response.end(JSON.stringify({success: true}));
return;
}
const dataFile = `content/${request.params.folder}/sessions/${session.name}.json`;
const data = getSession(request.params.folder);
data.resume[request.params.id] = data.resume[request.params.id] || {};
data.resume[request.params.id][request.params.type] = request.body.data;
fs.writeFileSync(dataFile, JSON.stringify(data));
response.set('Content-Type', 'application/json');
response.end(JSON.stringify({success: true}));
}
catch (error) {
handleError(error, response);
}
},
// deletes the session file used for resume functionality
resetUserData: (request, response, next) => {
try {
const dataFile = `content/${request.params.folder}/sessions/${session.name}.json`;
response.set('Content-Type', 'application/json');
if (fs.existsSync(dataFile)) {
fs.unlinkSync(dataFile);
response.end(JSON.stringify({success: true}));
}
else {
response.end(JSON.stringify({success: true, message: 'no_file'}));
}
}
catch (error) {
handleError(error, response);
}
},
// retrieves session data for resume functionality
getUserData: (request, response, next) => {
try {
manageSession(request.params.folder, {
language: request.query?.language,
name: request.query?.session
});
const data = getSession(request.params.folder);
response.set('Content-Type', 'application/json');
response.end(JSON.stringify(data?.[0] || {}));
}
catch (error) {
handleError(error, response);
}
},
// import zipped archive of content type
import: (request, response, next) => {
try {
request.params.folder = request.params.folder.replaceAll(/[^a-zA-Z0-9 -]/g, '');
request.params.folder = request.params.folder.replaceAll(' ', '-');
const path = logic.import(request.params.folder, request.file.path);
fs.rmSync(request.file.path);
response.set('Content-Type', 'application/json');
response.end(JSON.stringify({path}));
}
catch (error) {
handleError(error, response);
}
},
// download zipped archive of content type
export: async (request, response, next) => {
try {
const file = await logic.export(request.params.library, request.params.folder);
response.download(file);
}
catch (error) {
handleError(error, response);
}
},
// create empty content type
create: async (request, response, next) => {
try {
request.params.folder = request.params.folder.replaceAll(/[^a-zA-Z0-9 -]/g, '');
request.params.folder = request.params.folder.replaceAll(' ', '-');
const target = `content/${request.params.folder}`;
if (fs.existsSync(target)) {
response.set('Content-Type', 'application/json');
response.end(JSON.stringify({
error: `"${target}" folder already exists`
}));
return;
}
fs.mkdirSync(target);
logic.generateInfo(request.params.folder, request.params.type);
fs.writeFileSync(`${target}/content.json`, JSON.stringify({}));
response.set('Content-Type', 'application/json');
response.end(JSON.stringify({
result: request.params.folder
}));
}
catch (error) {
handleError(error, response);
}
},
// deletes a content folder
remove: (request, response, next) => {
try {
fs.rmSync(`content/${request.params.folder}`, { recursive: true, force: true });
response.set('Content-Type', 'application/json');
response.end(JSON.stringify({result: `removed "content/${request.params.folder}"`}));
}
catch (error) {
handleError(error, response);
}
},
// lists content folders
projects: async (request, response, next) => {
try {
const registry = await logic.getRegistry();
const limit = parseInt(request.query.limit) || 10;
const page = parseInt(request.query.page) || 0;
const start = page * limit;
const end = start + limit;
const libraryDirs = await logic.parseLibraryFolders();
const output = {
list: [],
total: 0
}
const dirs = fs.readdirSync('content');
const list = [];
for (let item of dirs) {
if (!fs.existsSync(`content/${item}/h5p.json`)) {
continue;
}
const info = JSON.parse(fs.readFileSync(`content/${item}/h5p.json`, 'utf-8'));
if (!registry.reversed[info.mainLibrary]) {
continue;
}
list.push({
id: info.mainLibrary,
title: info.title,
folder: item
});
}
list.sort((a, b) => {
const first = a.title.toLowerCase();
const second = b.title.toLowerCase();
if (first < second) {
return -1;
}
if (first > second) {
return 1;
}
return 0;
});
output.total = list.length;
for (let i = start; i < Math.min(end, list.length); i++) {
let entry = registry.reversed?.[list[i].id];
const library = entry.shortName;
entry = (await logic.computeDependencies(library, 'view', null, libraryDirs[registry.regular[library].id]))[library];
let icon = '/assets/icon.svg';
if (entry.version) {
const libraryFolder = `${config.folders.libraries}/${libraryDirs[list[i].id]}`;
const iconFile = `${libraryFolder}/icon.svg`;
if (fs.existsSync(libraryFolder)) {
if (fs.existsSync(iconFile)) {
icon = iconFile;
}
else {
const files = fs.readdirSync(libraryFolder);
for (let item of files) {
if (item.split('.')?.[1] == 'svg') {
icon = `${libraryFolder}/${item}`;
break;
}
}
}
}
}
list[i].library = library;
list[i].icon = icon;
output.list.push(list[i]);
}
response.set('Content-Type', 'application/json');
response.end(JSON.stringify(output));
}
catch (error) {
handleError(error, response);
}
},
// renders view & edit modes on the same page
splitView: async (request, response, next) => {
try {
const splitView_html = fs.readFileSync(`${require.main.path}/${config.folders.assets}/templates/splitView.html`, 'utf-8');
const labels = await getLangLabels();
let input = {
assets: config.folders.assets,
viewFrameSRC: `/view/${request.params.library}/${request.params.folder}?simple=1`,
editFrameSRC: `/edit/${request.params.library}/${request.params.folder}?simple=1`
}
input = {...input, ...labels};
response.set('Content-Type', 'text/html');
response.end(logic.fromTemplate(splitView_html, input));
}
catch (error) {
handleError(error, response);
}
},
// editor file upload
uploadFile: (request, response, next) => {
try {
const form = JSON.parse(request.body.field);
const targetFolder = `content/${request.params.folder}/${form.type}s`;
if (!fs.existsSync(targetFolder)) {
fs.mkdirSync(targetFolder);
}
const ext =
(request.file.originalname.split('.')?.pop() || '').toLowerCase();
const valid =
validateFileForUpload(form.type, request.file.mimetype, ext);
if (valid !== 'OK') {
throw new Error(valid);
}
const path = `${form.type}s/${request.file.filename}.${ext}`;
const targetFile = `${targetFolder}/${request.file.filename}.${ext}`;
fs.renameSync(`${request.file.path}`, targetFile);
const output = {
mime: request.file.mimetype,
path
}
if (form.type == 'image') {
const info = imageSize(targetFile);
if (info.width && info.height) {
output.width = info.width;
output.height = info.height;
}
}
response.set('Content-Type', 'application/json');
response.end(JSON.stringify(output));
}
catch (error) {
handleError(error, response);
}
},
// updates content.json file with data from content type editor form
saveContent: (request, response, next) => {
try {
const input = JSON.parse(request.body.parameters);
const currentParams = fs.readFileSync(`content/${request.params.folder}/content.json`, 'utf-8');
const newParams = JSON.stringify(input.params);
fs.writeFileSync(`content/${request.params.folder}/content.json`, newParams);
const infoFile = `content/${request.params.folder}/h5p.json`;
let info = JSON.parse(fs.readFileSync(infoFile, 'utf-8'));
info = {...info, ...input.metadata};
if (info.authors && !info.authors.length) {
delete info.authors;
}
if (info.changes && !info.changes.length) {
delete info.changes;
}
fs.writeFileSync(infoFile, JSON.stringify(info));
const contentFiles = parseContentFiles([input.params]);
const list = [];
for (let item in contentFiles) {
const parts = item.split('/');
list.push(parts[parts.length - 1]);
}
for (let type of config.mediaTypes) {// delete unused media files
const targetFolder = `content/${request.params.folder}/${type}`;
if (!fs.existsSync(targetFolder)) {
continue;
}
const files = fs.readdirSync(targetFolder);
for (let item of files) {
if (!list.includes(item)) {
fs.unlinkSync(`${targetFolder}/${item}`);
}
}
}
const simple = request.query.simple;
if (currentParams !== '{}' && currentParams !== newParams) {
resetContentUserData(request.params.folder); // content changed; reset user data
}
response.redirect(`/${simple ? 'edit' : 'view'}/${request.params.library}/${request.params.folder}${simple ? '?simple=1' : ''}`);
}
catch (error) {
handleError(error, response);
}
},
// return translations entries for library and dependencies
ajaxTranslations: async (request, response, next) => {
try {
const library = request.params.library;
const registry = await logic.getRegistry();
const libraryDirs = await logic.parseLibraryFolders();
const libFolder = libraryDirs[registry.regular[library].id];
const libs = await logic.computeDependencies(library, 'view', null, libFolder);
const translations = {};
for (let item of request.body.libraries) {
const entry = libs[registry.reversed[item.split(' ')[0]].shortName];
const folder = libraryDirs[entry.id];
const idx = `${entry.id} ${entry.version.major}.${entry.version.minor}`;
const languageFolder = `${config.folders.libraries}/${folder}/language`;
const langFile = `${languageFolder}/${request.query.language}.json`;
if (fs.existsSync(langFile)) {
translations[idx] = fs.readFileSync(langFile, 'utf-8');
}
}
response.set('Content-Type', 'application/json');
response.end(JSON.stringify({ success: true, data: translations }));
}
catch (error) {
handleError(error, response);
}
},
// endpoint that lists library data; used as ajax request by the content type editors;
ajaxLibraries: async (request, response, next) => {
try {
const output = await ajaxLibraries({
library: request.params.library,
libraries: request.body.libraries,
machineName: request.query.machineName
});
response.set('Content-Type', 'application/json');
response.end(JSON.stringify(output));
}
catch (error) {
handleError(error, response);
}
},
// html page that initializes and renders h5p content type editors
edit: async (request, response, next) => {
try {
const baseUrl = config.api;
const library = request.params.library;
const folder = request.params.folder;
const registry = await logic.getRegistry();
const libraryDirs = await logic.parseLibraryFolders();
if (!await verifySetup(library, response)) {
return;
}
const metadataSemantics = fs.readFileSync(`${require.main.path}/${config.folders.assets}/metadataSemantics.json`, 'utf-8');
const copyrightSemantics = fs.readFileSync(`${require.main.path}/${config.folders.assets}/copyrightSemantics.json`, 'utf-8');
const libs = await logic.computeDependencies(library, 'edit', null, libraryDirs[registry.regular[library].id]);
const jsonContent = fs.readFileSync(`./content/${folder}/content.json`, 'utf8');
let preloadedJs = [];
let preloadedCss = [];
for (let item in libs) {
if (item == library) {
continue;
}
const entry = libs[item];
if (!entry.id) {
continue;
}
const libFolder = libraryDirs[entry.id];
if (!libFolder) {
continue;
}
for (let jsItem of entry.preloadedJs) {
preloadedJs.push(`"/${config.folders.libraries}/${libFolder}/${jsItem.path}"`);
}
for (let cssItem of entry.preloadedCss) {
preloadedCss.push(`"/${config.folders.libraries}/${libFolder}/${cssItem.path}"`);
}
}
const mathDisplay = (await logic.computeDependencies('h5p-math-display', 'view', null, libraryDirs[registry.regular['h5p-math-display'].id]))['h5p-math-display'];
const mathDisplayLabel = libraryDirs[mathDisplay.id];
preloadedJs.push(`"/${config.folders.libraries}/${mathDisplayLabel}/dist/h5p-math-display.js"`);
const libraryConfig = JSON.parse(logic.fromTemplate(fs.readFileSync(`${require.main.path}/${config.folders.assets}/libraryConfig.json`, 'utf-8'), {
baseUrl,
mathDisplayLabel
}));
const html = fs.readFileSync(`${require.main.path}/${config.folders.assets}/templates/edit.html`, 'utf-8');
const info = JSON.parse(fs.readFileSync(`content/${folder}/h5p.json`, 'utf-8'));
info.language = session.language;
const id = libs[library].id;
let mainLibrary = {};
for (let item of info.preloadedDependencies) {
if (item.machineName == id) {
mainLibrary = item;
break;
}
}
manageSession(request.params.folder, {
language: request.query?.language,
name: request.query?.session
}, true);
const formParams = {
params: JSON.parse(jsonContent),
metadata: info
}
const labels = await getLangLabels();
const machineName = `${libs[library].id} ${libs[library].version.major}.${libs[library].version.minor}`;
const libraryDirectories = JSON.stringify((await ajaxLibraries({ machineName: id })).directories);
let input = {
assets: config.folders.assets,
libraries: config.folders.libraries,
title: info.title,
baseUrl,
ajaxPath: `${baseUrl}/edit/${library}/${folder}/`,
copyrightSemantics,
metadataSemantics,
library,
folder,
preloadedCss: preloadedCss.join(',\n'),
preloadedJs: preloadedJs.join(',\n'),
l10n: JSON.stringify(l10n),
machineName,
version: `${libs[library].version.major}.${libs[library].version.minor}`,
contentVersion: `${mainLibrary.majorVersion}.${mainLibrary.minorVersion}`,
id,
libraryDirectories,
parameters: he.encode(JSON.stringify(formParams)),
libraryConfig: JSON.stringify(libraryConfig),
language: session.language,
watcher: config.files.watch,
simple: request.query.simple ? 'hidden' : ''
}
input = {...input, ...labels};
response.set('Content-Type', 'text/html');
response.end(logic.fromTemplate(html, input));
}
catch (error) {
handleError(error, response);
}
},
// html page that initializes and renders h5p content types
view: async (request, response, next) => {
try {
const baseUrl = config.api;
const library = request.params.library;
const folder = request.params.folder;
const registry = await logic.getRegistry();
const libraryDirs = await logic.parseLibraryFolders();
if (!await verifySetup(library, response)) {
return;
}
logic.upgrade(folder, library);
const libs = await logic.computeDependencies(library, 'view', null, libraryDirs[registry.regular[library].id]);
const jsonContent = fs.readFileSync(`./content/${folder}/content.json`, 'utf8');
const sessions = manageSession(request.params.folder, {
language: request.query?.language,
name: request.query?.session
}, true);
const userData = getSession(request.params.folder);
let metadata = await logic.getFile(`content/${folder}/h5p.json`, true);
metadata.language = session.language;
metadata = JSON.stringify(metadata);
let preloadedJs = [];
let preloadedCss = [];
for (let item in libs) {
const entry = libs[item];
if (!entry.id) {
continue;
}
const libFolder = libraryDirs[entry.id];
if (!libFolder) {
continue;
}
for (let jsItem of entry.preloadedJs) {
preloadedJs.push(`/${config.folders.libraries}/${libFolder}/${jsItem.path}`);
}
for (let cssItem of entry.preloadedCss) {
preloadedCss.push(`/${config.folders.libraries}/${libFolder}/${cssItem.path}`);
}
}
const mathDisplay = (await logic.computeDependencies('h5p-math-display', 'view', null, libraryDirs[registry.regular['h5p-math-display'].id]))['h5p-math-display'];
const mathDisplayLabel = libraryDirs[mathDisplay.id];
preloadedJs.push(`/${config.folders.libraries}/${mathDisplayLabel}/dist/h5p-math-display.js`);
const libraryConfig = JSON.parse(logic.fromTemplate(fs.readFileSync(`${require.main.path}/${config.folders.assets}/libraryConfig.json`, 'utf-8'), {
baseUrl,
mathDisplayLabel
}));
const html = fs.readFileSync(`${require.main.path}/${config.folders.assets}/templates/view.html`, 'utf-8');
const info = JSON.parse(fs.readFileSync(`content/${folder}/h5p.json`, 'utf-8'));
const id = libs[library].id;
let mainLibrary = {};
for (let item of info.preloadedDependencies) {
if (item.machineName == id) {
mainLibrary = item;
break;
}
}
const machineName = `${id} ${libs[library].version.major}.${libs[library].version.minor}`;
const labels = await getLangLabels();
const libraryDirectories = JSON.stringify((await ajaxLibraries({ machineName: id })).directories);
let input = {
assets: config.folders.assets,
libraries: config.folders.libraries,
title: info.title,
baseUrl,
library,
folder,
session: session.name,
sessions: JSON.stringify(sessions),
machineName,
version: `${libs[library].version.major}.${libs[library].version.minor}`,
contentVersion: `${mainLibrary.majorVersion}.${mainLibrary.minorVersion}`,
id,
fullscreen: libs[library].fullscreen,
libraryDirectories,
jsonContent: JSON.stringify(jsonContent),
preloadedCss: JSON.stringify(preloadedCss),
preloadedJs: JSON.stringify(preloadedJs),
l10n: JSON.stringify(l10n),
libraryConfig: JSON.stringify(libraryConfig),
language: session.language,
metadata,
contentUserData: JSON.stringify(userData.resume),
watcher: config.files.watch,
simple: request.query.simple ? 'hidden' : '',
saveFreq: config.saveFreq
}
input = {...input, ...labels};
response.set('Content-Type', 'text/html');
response.end(logic.fromTemplate(html, input));
}
catch (error) {
handleError(error, response);
}
}
}
/**
* Validate file for upload by mime type and extension.
* @param {string} uploadFieldType Upload field type.
* @param {string} mimeType File's mime type.
* @param {string} extension File's extension.
* @returns {string} Error message or 'OK'.
*/
const validateFileForUpload = (uploadFieldType, mimeType, extension) => {
let allowed;
// Set allowed extensions per mime type depending on upload field type
switch (uploadFieldType) {
// Image upload field
case 'image':
allowed = {
'image/png': ['png'],
'image/jpeg': ['jpg', 'jpeg'],
'image/gif': ['gif'],
};
break;
// Audio upload field
case 'audio':
allowed = {
'audio/mpeg': ['mp3'],
'audio/mp3': ['mp3'],
'audio/mp4': ['m4a'],
'audio/x-wav': ['wav'],
'audio/wav': ['wav'],
'audio/ogg': ['ogg'],
};
break;
// Video upload field
case 'video':
allowed = {
'video/mp4': ['mp4'],
'video/webm': ['webm'],
'video/ogg': ['ogv'],
};
break;
// File upload field
case 'file':
const allowedExtensions = config.files.patterns.allowed.toString()
.replace(/[^a-zA-Z0-9|]/g, '')
.split('|');
mimeType = '*'; // Allow allowedExtensions for all mime types
allowed = { mimeType : allowedExtensions };
break;
default:
allowed = {};
}
const isExtensionAllowed = (allowed[mimeType] ?? []).includes(extension);
const message = isExtensionAllowed ?
'OK' :
`Invalid ${uploadFieldType} file format.`;
const extensionsToUse = isExtensionAllowed ?
'' :
listExtensions(Object.values(allowed).flat());
return [message, extensionsToUse].filter(Boolean).join(' ');
}
/**
* List extension options as human language string.
* @param {string[]} extensions List of extensions.
* @returns {string} Human language string.
*/
const listExtensions = (extensions = []) => {
if (!Array.isArray(extensions) || !extensions.length) {
return '';
}
else if (extensions.length === 1) {
return `Use ${extensions[0]}.`;
}
else {
return `Use ${extensions.slice(0, -1).join(', ')} or ${extensions.slice(-1)}.`;
}
};
// parses content.json objects for entries of file type
const parseContentFiles = (entries) => {
let toDo = [];
let list = [];
const output = {};
const valid = ['path', 'mime'];
const parseList = () => {
toDo = [];
for (let obj of list) {
for (let attr in obj) {
if (valid.includes(attr) && typeof attr == 'string') {
output[obj.path] = obj;
continue;
}
if (typeof obj[attr] == 'object' && !Array.isArray(obj[attr])) {
toDo.push(obj[attr]);
}
if (Array.isArray(obj[attr])) {
toDo = toDo.concat(obj[attr]);
}
}
}
list = toDo;
}
list = entries;
while (list.length) {
parseList();
}
return output;
}
/* generates lists of JavaScript & CSS files to load
as well as translations and directories entries for use in content types */
const computePreloaded = async (library, baseUrl) => {
const registry = await logic.getRegistry();
const libraryDirs = await logic.parseLibraryFolders();
const libs = await logic.computeDependencies(library, 'edit', null, libraryDirs[registry.regular[library].id]);
const directories = {};
const translations = {};
const languages = [];
let preloadedJs = [];
let preloadedCss = [];
for (let item in libs) {
const entry = libs[item];
if (!entry.id) {
continue;
}
const folder = libraryDirs[entry.id];
if (!folder) {
continue;
}
const label = `${entry.id}-${entry.version.major}.${entry.version.minor}.${entry.version.patch}`;
const languageFolder = `${config.folders.libraries}/${folder}/language`;
const langFile = `${languageFolder}/${session.language}.json`;
if (fs.existsSync(langFile)) {
translations[entry.id] = JSON.parse(fs.readFileSync(langFile, 'utf-8'));
}
if (!languages.length && fs.existsSync(languageFolder)) {
const langFiles = fs.readdirSync(languageFolder);
for (let item of langFiles) {
const id = (item.replace(/^\./, "")).split('.')[0];
languages.push(id);
}
}
if (item == library && entry.requiredBy.length == 1) {
let required = false;
for (let obj of entry.editorDependencies) {
if (obj.machineName == entry.id) {
required = true;
}
}
if (!required) {
continue;
}
}
for (let jsItem of entry.preloadedJs) {
preloadedJs.push(`${baseUrl}/${config.folders.libraries}/${folder}/${jsItem.path}`);
}
for (let cssItem of entry.preloadedCss) {
preloadedCss.push(`${baseUrl}/${config.folders.libraries}/${folder}/${cssItem.path}`);
}
directories[folder] = label;
}
return { library, preloadedJs, preloadedCss, languages, translations, directories };
}
const ajaxLibraries = async (options) => {
const baseUrl = config.api;
const registry = await logic.getRegistry();
const libraryDirs = await logic.parseLibraryFolders();
let libraries = [options.library];
if (Array.isArray(options.libraries)) {
libraries = [];
for (let item of options.libraries) {
item = item.split(' ')[0];
if (!registry.reversed[item] || !libraryDirs[item]) {
continue;
}
libraries.push(registry.reversed[item].shortName);
}
}
if (options.machineName) {
libraries = [];
libraries.push(registry.reversed[options.machineName].shortName);
}
const toDo = [];
for (let item of libraries) {
toDo.push(computePreloaded(item, baseUrl));
}
const preloaded = await Promise.all(toDo);
let output;
if (options.machineName) {
const library = libraries[0];
const libs = await logic.computeDependencies(library, 'edit', null, libraryDirs[registry.regular[library].id]);
const version = libs[library].version;
const folder = libraryDirs[libs[library].id];
output = {
name: libs[library].id,
version,
title: libs[library].title,
upgradesScript: `${baseUrl}/${config.folders.libraries}/${folder}/upgrades.js`,
semantics: JSON.parse(fs.readFileSync(`${config.folders.libraries}/${folder}/semantics.json`, 'utf-8')),
language: null,
defaultLanguage: null,
languages: preloaded[0].languages,
javascript: preloaded[0].preloadedJs,
css: preloaded[0].preloadedCss,
translations: preloaded[0].translations,
directories: preloaded[0].directories
}
}
else {
output = [];
for (let item of preloaded) {
const library = item.library;
const libs = await logic.computeDependencies(library, 'edit', null, libraryDirs[registry.regular[library].id]);
output.push({
uberName: `${libs[library].id} ${libs[library].version.major}.${libs[library].version.minor}`,
name: libs[library].id,
majorVersion: libs[library].version.major,
minorVersion: libs[library].version.minor,
title: libs[library].title,
runnable: libs[library].runnable,
restricted: false,
metadataSettings: libs[library].metadataSettings
});
}
}
return output;
}
const handleError = (error, response) => {
console.log(error);
response.set('Content-Type', 'application/json');
response.end(JSON.stringify({ error: error.toString() }));
}
const verifySetup = async (library, response) => {
const setupStatus = await logic.verifySetup(library);
if (!setupStatus.ok) {
session.status = `"${library}" is not properly set up. Please run "h5p setup ${library}" for setup.
For a setup status report run "h5p verify ${library}".`;
response.redirect(`/dashboard`);
return false;
}
else {
return true;
}
}
const manageSession = (folder, options, getSessions) => {
for (let key in options) {
if (typeof options[key] !== 'undefined') {
session[key] = options[key];
}
}
const sessionFolder = `content/${folder}/sessions`;
const sessionFile = `${sessionFolder}/${session.name}.json`;
if (folder && !fs.existsSync(sessionFolder)) {
fs.mkdirSync(sessionFolder);
}
if (folder && session.name != 'null' && !fs.existsSync(sessionFile)) {
fs.writeFileSync(sessionFile, JSON.stringify({
resume: []
}));
}
if (folder && getSessions) {
const files = fs.readdirSync(sessionFolder);
return files;
}
return [];
}
const getSession = (folder) => {
const dataFile = `content/${folder}/sessions/${session.name}.json`;
let userData = {};
if (fs.existsSync(dataFile)) {
userData = JSON.parse(fs.readFileSync(dataFile, 'utf-8'));
}
return userData;
}
const getLangLabels = async () => {
let langFile = `${require.main.path}/${config.folders.assets}/languages/${session.language}.json`;
if (!fs.existsSync(langFile)) {
langFile = `${config.folders.assets}/languages/en.json`;
}
return await logic.getFile(langFile, true);
}
/* Reset content user data.
* @param {string} folder Folder name (contentId) if content to reset user data for.
*/
const resetContentUserData = (folder) => {
const sessionsDir = `content/${folder}/sessions`;
fs.readdirSync(sessionsDir).forEach((file) => {
if (path.extname(file) !== '.json') {
return;
}
const dataFile = path.join(sessionsDir, file);
const data = JSON.parse(fs.readFileSync(dataFile, 'utf8'));
data.resume.forEach(entry => {
entry.state = null; // Means to reset state for H5P core
});
fs.writeFileSync(dataFile, JSON.stringify(data, null, 2));
});
};