@cocreate/file 1.19.1 → 1.19.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +7 -0
- package/package.json +1 -1
- package/src/client.js +1 -0
- package/src/server.js +526 -495
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,10 @@
|
|
|
1
|
+
## [1.19.2](https://github.com/CoCreate-app/CoCreate-file/compare/v1.19.1...v1.19.2) (2025-01-18)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### Bug Fixes
|
|
5
|
+
|
|
6
|
+
* formating ([65e9a91](https://github.com/CoCreate-app/CoCreate-file/commit/65e9a915b17f616abd5bed917fa7323b1f59bd28))
|
|
7
|
+
|
|
1
8
|
## [1.19.1](https://github.com/CoCreate-app/CoCreate-file/compare/v1.19.0...v1.19.1) (2024-12-14)
|
|
2
9
|
|
|
3
10
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cocreate/file",
|
|
3
|
-
"version": "1.19.
|
|
3
|
+
"version": "1.19.2",
|
|
4
4
|
"description": "A versatile, configurable headless file uploader supporting local and server operations. Accessible via a JavaScript API and HTML5 attributes, it provides seamless file reading, writing, and uploading with fallbacks to the standard HTML5 file input API. Ideal for developers needing robust file management in headless environments.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"file-uploader",
|
package/src/client.js
CHANGED
package/src/server.js
CHANGED
|
@@ -1,500 +1,531 @@
|
|
|
1
|
-
const crud = require(
|
|
2
|
-
const Config = require(
|
|
3
|
-
const fs = require(
|
|
1
|
+
const crud = require("@cocreate/crud-client");
|
|
2
|
+
const Config = require("@cocreate/config");
|
|
3
|
+
const fs = require("fs");
|
|
4
4
|
const realpathAsync = fs.promises.realpath;
|
|
5
5
|
|
|
6
|
-
const path = require(
|
|
6
|
+
const path = require("path");
|
|
7
7
|
const mimeTypes = {
|
|
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
|
-
|
|
8
|
+
".aac": "audio/aac",
|
|
9
|
+
".abw": "application/x-abiword",
|
|
10
|
+
".arc": "application/x-freearc",
|
|
11
|
+
".avi": "video/x-msvideo",
|
|
12
|
+
".azw": "application/vnd.amazon.ebook",
|
|
13
|
+
".bin": "application/octet-stream",
|
|
14
|
+
".bmp": "image/bmp",
|
|
15
|
+
".bz": "application/x-bzip",
|
|
16
|
+
".bz2": "application/x-bzip2",
|
|
17
|
+
".csh": "application/x-csh",
|
|
18
|
+
".css": "text/css",
|
|
19
|
+
".csv": "text/csv",
|
|
20
|
+
".doc": "application/msword",
|
|
21
|
+
".docx":
|
|
22
|
+
"application/vnd.openxmlformats-officedocument.wordprocessingml.document",
|
|
23
|
+
".eot": "application/vnd.ms-fontobject",
|
|
24
|
+
".epub": "application/epub+zip",
|
|
25
|
+
".gif": "image/gif",
|
|
26
|
+
".htm": "text/html",
|
|
27
|
+
".html": "text/html",
|
|
28
|
+
".ico": "image/x-icon",
|
|
29
|
+
".ics": "text/calendar",
|
|
30
|
+
".jar": "application/java-archive",
|
|
31
|
+
".jpeg": "image/jpeg",
|
|
32
|
+
".jpg": "image/jpeg",
|
|
33
|
+
".js": "text/javascript",
|
|
34
|
+
".json": "application/json",
|
|
35
|
+
".jsonld": "application/ld+json",
|
|
36
|
+
".mid": "audio/midi",
|
|
37
|
+
".midi": "audio/midi",
|
|
38
|
+
".mjs": "text/javascript",
|
|
39
|
+
".mp3": "audio/mpeg",
|
|
40
|
+
".mp4": "video/mp4",
|
|
41
|
+
".mpeg": "video/mpeg",
|
|
42
|
+
".mpkg": "application/vnd.apple.installer+xml",
|
|
43
|
+
".odp": "application/vnd.oasis.opendocument.presentation",
|
|
44
|
+
".ods": "application/vnd.oasis.opendocument.spreadsheet",
|
|
45
|
+
".odt": "application/vnd.oasis.opendocument.text",
|
|
46
|
+
".oga": "audio/ogg",
|
|
47
|
+
".ogv": "video/ogg",
|
|
48
|
+
".ogx": "application/ogg",
|
|
49
|
+
".otf": "font/otf",
|
|
50
|
+
".png": "image/png",
|
|
51
|
+
".pdf": "application/pdf",
|
|
52
|
+
".ppt": "application/vnd.ms-powerpoint",
|
|
53
|
+
".pptx":
|
|
54
|
+
"application/vnd.openxmlformats-officedocument.presentationml.presentation",
|
|
55
|
+
".rar": "application/x-rar-compressed",
|
|
56
|
+
".rtf": "application/rtf",
|
|
57
|
+
".sh": "application/x-sh",
|
|
58
|
+
".svg": "image/svg+xml",
|
|
59
|
+
".swf": "application/x-shockwave-flash",
|
|
60
|
+
".tar": "application/x-tar",
|
|
61
|
+
".tif": "image/tiff",
|
|
62
|
+
".tiff": "image/tiff",
|
|
63
|
+
".ts": "video/mp2t",
|
|
64
|
+
".ttf": "font/ttf",
|
|
65
|
+
".txt": "text/plain",
|
|
66
|
+
".vsd": "application/vnd.visio",
|
|
67
|
+
".wav": "audio/wav",
|
|
68
|
+
".weba": "audio/webm",
|
|
69
|
+
".webm": "video/webm",
|
|
70
|
+
".webmanifest": "application/manifest+json",
|
|
71
|
+
".webp": "image/webp",
|
|
72
|
+
".woff": "font/woff",
|
|
73
|
+
".woff2": "font/woff2",
|
|
74
|
+
".xhtml": "application/xhtml+xml",
|
|
75
|
+
".xls": "application/vnd.ms-excel",
|
|
76
|
+
".xlsx":
|
|
77
|
+
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
|
|
78
|
+
".xml": "application/xml",
|
|
79
|
+
".xul": "application/vnd.mozilla.xul+xml",
|
|
80
|
+
".zip": "application/zip",
|
|
81
|
+
".3gp": "video/3gpp",
|
|
82
|
+
".3g2": "video/3gpp2",
|
|
83
|
+
".7z": "application/x-7z-compressed"
|
|
84
|
+
};
|
|
82
85
|
|
|
83
86
|
module.exports = async function file(CoCreateConfig, configPath, match) {
|
|
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
|
-
|
|
87
|
+
let directories = CoCreateConfig.directories;
|
|
88
|
+
let sources = CoCreateConfig.sources;
|
|
89
|
+
let configDirectoryPath = path.dirname(configPath);
|
|
90
|
+
|
|
91
|
+
if (match && !Array.isArray(match)) match = [match];
|
|
92
|
+
else if (!match) match = [];
|
|
93
|
+
|
|
94
|
+
let config = await Config(
|
|
95
|
+
{
|
|
96
|
+
organization_id: {
|
|
97
|
+
prompt: "Enter your organization_id: "
|
|
98
|
+
},
|
|
99
|
+
host: {
|
|
100
|
+
prompt: "Enter the host: "
|
|
101
|
+
},
|
|
102
|
+
prompt: {
|
|
103
|
+
prompt: "Choose an authentication option: \n1.apikey\n2.Sign In\n",
|
|
104
|
+
choices: {
|
|
105
|
+
1: {
|
|
106
|
+
apikey: {
|
|
107
|
+
prompt: "Enter your apikey: "
|
|
108
|
+
}
|
|
109
|
+
},
|
|
110
|
+
2: {
|
|
111
|
+
email: {
|
|
112
|
+
prompt: "Enter your email: "
|
|
113
|
+
},
|
|
114
|
+
password: {
|
|
115
|
+
prompt: "Enter your password: "
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
},
|
|
121
|
+
null,
|
|
122
|
+
null,
|
|
123
|
+
configPath
|
|
124
|
+
);
|
|
125
|
+
|
|
126
|
+
if (
|
|
127
|
+
!config.organization_id ||
|
|
128
|
+
(!config.apikey && (!config.password || config.email))
|
|
129
|
+
) {
|
|
130
|
+
console.log("One or more required config params could not be found");
|
|
131
|
+
process.exit();
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
if (config.email && config.password) {
|
|
135
|
+
let request = {
|
|
136
|
+
method: "signIn",
|
|
137
|
+
array: "users",
|
|
138
|
+
$filter: {
|
|
139
|
+
query: {
|
|
140
|
+
email: config.email,
|
|
141
|
+
password: config.password
|
|
142
|
+
}
|
|
143
|
+
},
|
|
144
|
+
...config
|
|
145
|
+
};
|
|
146
|
+
|
|
147
|
+
let response = await crud.send(request);
|
|
148
|
+
let { success, token } = response;
|
|
149
|
+
|
|
150
|
+
if (success) {
|
|
151
|
+
console.log("succesful sign in");
|
|
152
|
+
// apply token to socket
|
|
153
|
+
} else {
|
|
154
|
+
console.log("The email or password you entered is incorrect");
|
|
155
|
+
process.exit();
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
// console.log('Uploading files...')
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* Store files by config directories
|
|
163
|
+
**/
|
|
164
|
+
let errorLog = [];
|
|
165
|
+
|
|
166
|
+
async function runDirectories() {
|
|
167
|
+
for (const directory of directories) {
|
|
168
|
+
const entry = directory.entry;
|
|
169
|
+
const exclude = directory.exclude || [];
|
|
170
|
+
await runFiles(directory, entry, exclude);
|
|
171
|
+
}
|
|
172
|
+
return;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
async function runFiles(directory, entry, exclude, Path, directoryName) {
|
|
176
|
+
const entryPath = path.resolve(configDirectoryPath, entry);
|
|
177
|
+
let files = fs.readdirSync(entryPath);
|
|
178
|
+
|
|
179
|
+
for (let file of files) {
|
|
180
|
+
let skip = false;
|
|
181
|
+
for (let i = 0; i < exclude.length; i++) {
|
|
182
|
+
if (file.includes(exclude)) {
|
|
183
|
+
skip = true;
|
|
184
|
+
break;
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
if (skip) continue;
|
|
188
|
+
|
|
189
|
+
let isDirectory;
|
|
190
|
+
let isSymlink = fs
|
|
191
|
+
.lstatSync(`${entryPath}/${file}`)
|
|
192
|
+
.isSymbolicLink();
|
|
193
|
+
if (isSymlink) {
|
|
194
|
+
let symlinkPath = await realpathAsync(`${entryPath}/${file}`);
|
|
195
|
+
isDirectory =
|
|
196
|
+
fs.existsSync(symlinkPath) &&
|
|
197
|
+
fs.lstatSync(symlinkPath).isDirectory();
|
|
198
|
+
} else
|
|
199
|
+
isDirectory =
|
|
200
|
+
fs.existsSync(`${entryPath}/${file}`) &&
|
|
201
|
+
fs.lstatSync(`${entryPath}/${file}`).isDirectory();
|
|
202
|
+
|
|
203
|
+
let name = file;
|
|
204
|
+
let source = "";
|
|
205
|
+
|
|
206
|
+
for (let i = 0; i < match.length; i++) {
|
|
207
|
+
skip = true;
|
|
208
|
+
const filePath = path.resolve(entryPath, file);
|
|
209
|
+
if (filePath.startsWith(match[i])) {
|
|
210
|
+
skip = false;
|
|
211
|
+
break;
|
|
212
|
+
} else if (isDirectory && match[i].startsWith(filePath)) {
|
|
213
|
+
skip = "directory";
|
|
214
|
+
break;
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
if (skip === true) continue;
|
|
219
|
+
|
|
220
|
+
const fileExtension = path.extname(file);
|
|
221
|
+
let mimeType = mimeTypes[fileExtension];
|
|
222
|
+
|
|
223
|
+
if (!directoryName) {
|
|
224
|
+
if (directory.object && directory.object.directory) {
|
|
225
|
+
if (directory.object.directory === "{{directory}}") {
|
|
226
|
+
directoryName = entry.split("/");
|
|
227
|
+
directoryName = directoryName[directoryName.length - 1];
|
|
228
|
+
} else directoryName = directory.object.directory;
|
|
229
|
+
} else directoryName = "/";
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
if (exclude && exclude.includes(directoryName)) continue;
|
|
233
|
+
|
|
234
|
+
if (!Path) {
|
|
235
|
+
if (directoryName === "/") Path = directoryName;
|
|
236
|
+
else Path = "/" + directoryName;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
let pathname;
|
|
240
|
+
if (Path === "/") pathname = Path + name;
|
|
241
|
+
else pathname = Path + "/" + name;
|
|
242
|
+
|
|
243
|
+
if (isDirectory) mimeType = "text/directory";
|
|
244
|
+
else
|
|
245
|
+
source = await getSource(
|
|
246
|
+
`${entryPath}/${file}`,
|
|
247
|
+
mimeType,
|
|
248
|
+
isSymlink
|
|
249
|
+
);
|
|
250
|
+
|
|
251
|
+
let values = {
|
|
252
|
+
"{{name}}": name || "",
|
|
253
|
+
"{{source}}": source || "",
|
|
254
|
+
"{{directory}}": directoryName || "",
|
|
255
|
+
"{{path}}": Path || "",
|
|
256
|
+
"{{pathname}}": pathname,
|
|
257
|
+
"{{content-type}}": mimeType || ""
|
|
258
|
+
};
|
|
259
|
+
|
|
260
|
+
let object = { ...directory.object };
|
|
261
|
+
if (!object.name) object.name = "{{name}}";
|
|
262
|
+
if (!object.src) object.src = "{{source}}";
|
|
263
|
+
if (!object.directory) object.directory = "{{directory}}";
|
|
264
|
+
if (!object.path) object.path = "{{path}}";
|
|
265
|
+
if (!object.pathname) object.pathname = "{{pathname}}";
|
|
266
|
+
if (!object["content-type"])
|
|
267
|
+
object["content-type"] = "{{content-type}}";
|
|
268
|
+
if (
|
|
269
|
+
!object.public &&
|
|
270
|
+
object.public != false &&
|
|
271
|
+
object.public != "false"
|
|
272
|
+
)
|
|
273
|
+
object.public = "true";
|
|
274
|
+
|
|
275
|
+
let newObject = {
|
|
276
|
+
array: directory.array || "files",
|
|
277
|
+
object
|
|
278
|
+
};
|
|
279
|
+
|
|
280
|
+
if (directory.storage) newObject.storage = directory.storage;
|
|
281
|
+
if (directory.database) newObject.database = directory.database;
|
|
282
|
+
if (directory.array) newObject.array = directory.array || "files";
|
|
283
|
+
|
|
284
|
+
for (const key of Object.keys(directory.object)) {
|
|
285
|
+
if (typeof directory.object[key] == "string") {
|
|
286
|
+
let variables = directory.object[key].match(
|
|
287
|
+
/{{([A-Za-z0-9_.,\[\]\-\/ ]*)}}/g
|
|
288
|
+
);
|
|
289
|
+
if (variables) {
|
|
290
|
+
for (let variable of variables) {
|
|
291
|
+
newObject.object[key] = newObject.object[
|
|
292
|
+
key
|
|
293
|
+
].replace(variable, values[variable]);
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
if (skip !== "directory") {
|
|
300
|
+
if (!newObject.object._id)
|
|
301
|
+
newObject.$filter = {
|
|
302
|
+
query: {
|
|
303
|
+
pathname
|
|
304
|
+
}
|
|
305
|
+
};
|
|
306
|
+
|
|
307
|
+
response = await runStore(newObject);
|
|
308
|
+
console.log(
|
|
309
|
+
`Uploaded: ${entryPath}/${file}`,
|
|
310
|
+
`To: ${pathname}`
|
|
311
|
+
);
|
|
312
|
+
|
|
313
|
+
if (response.error) errorLog.push(response.error);
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
if (isDirectory && pathname) {
|
|
317
|
+
let newEntry;
|
|
318
|
+
if (entry.endsWith("/")) newEntry = entry + name;
|
|
319
|
+
else newEntry = entry + "/" + name;
|
|
320
|
+
|
|
321
|
+
await runFiles(directory, newEntry, exclude, pathname, name);
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
// if (errorLog.length)
|
|
325
|
+
// console.log(...errorLog)
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
async function getSource(path, mimeType, isSymlink) {
|
|
329
|
+
let readType = "utf8";
|
|
330
|
+
if (mimeType === "image/svg+xml") {
|
|
331
|
+
readType = "utf8";
|
|
332
|
+
} else if (/^(image|audio|video)\/[-+.\w]+/.test(mimeType)) {
|
|
333
|
+
readType = "base64";
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
if (isSymlink) path = await realpathAsync(path);
|
|
337
|
+
|
|
338
|
+
let binary = fs.readFileSync(path);
|
|
339
|
+
let content = new Buffer.from(binary).toString(readType);
|
|
340
|
+
|
|
341
|
+
return content;
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
/**
|
|
345
|
+
* Store files by config sources
|
|
346
|
+
**/
|
|
347
|
+
async function runSources() {
|
|
348
|
+
let updatedSources = [];
|
|
349
|
+
|
|
350
|
+
for (let i = 0; i < sources.length; i++) {
|
|
351
|
+
const { array, object } = sources[i];
|
|
352
|
+
|
|
353
|
+
let source = { ...sources[i] };
|
|
354
|
+
let keys = new Map();
|
|
355
|
+
let response = {};
|
|
356
|
+
let isMatch = false;
|
|
357
|
+
|
|
358
|
+
try {
|
|
359
|
+
if (array) {
|
|
360
|
+
if (!object) object = {};
|
|
361
|
+
else
|
|
362
|
+
for (const key of Object.keys(object)) {
|
|
363
|
+
if (typeof object[key] != "string") continue;
|
|
364
|
+
|
|
365
|
+
let variables = object[key].match(
|
|
366
|
+
/{{([A-Za-z0-9_.,\[\]\-\/ ]*)}}/g
|
|
367
|
+
);
|
|
368
|
+
if (variables) {
|
|
369
|
+
let originalValue = object[key];
|
|
370
|
+
keys.set(key, originalValue);
|
|
371
|
+
let value = "";
|
|
372
|
+
for (let variable of variables) {
|
|
373
|
+
let entry = /{{\s*([\w\W]+)\s*}}/g.exec(
|
|
374
|
+
variable
|
|
375
|
+
);
|
|
376
|
+
entry = entry[1].trim();
|
|
377
|
+
if (entry) {
|
|
378
|
+
if (!fs.existsSync(entry)) continue;
|
|
379
|
+
|
|
380
|
+
if (!isMatch) {
|
|
381
|
+
const filePath = path.resolve(
|
|
382
|
+
configDirectoryPath,
|
|
383
|
+
entry
|
|
384
|
+
);
|
|
385
|
+
for (
|
|
386
|
+
let i = 0;
|
|
387
|
+
i < match.length;
|
|
388
|
+
i++
|
|
389
|
+
) {
|
|
390
|
+
if (
|
|
391
|
+
filePath.startsWith(
|
|
392
|
+
match[i]
|
|
393
|
+
)
|
|
394
|
+
) {
|
|
395
|
+
console.log(
|
|
396
|
+
"Source saved",
|
|
397
|
+
sources[i]
|
|
398
|
+
);
|
|
399
|
+
isMatch = true;
|
|
400
|
+
break;
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
let read_type = "utf8";
|
|
406
|
+
const fileExtension =
|
|
407
|
+
path.extname(entry);
|
|
408
|
+
let mime_type =
|
|
409
|
+
mimeTypes[fileExtension] ||
|
|
410
|
+
"text/html";
|
|
411
|
+
|
|
412
|
+
if (
|
|
413
|
+
/^(image|audio|video)\/[-+.\w]+/.test(
|
|
414
|
+
mime_type
|
|
415
|
+
)
|
|
416
|
+
) {
|
|
417
|
+
read_type = "base64";
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
let binary = fs.readFileSync(entry);
|
|
421
|
+
let content = new Buffer.from(
|
|
422
|
+
binary
|
|
423
|
+
).toString(read_type);
|
|
424
|
+
if (content) value += content;
|
|
425
|
+
// object[key] = object[key].replace(variable, content);
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
object[key] = value;
|
|
429
|
+
}
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
let data = { array, object };
|
|
433
|
+
if (!object._id && object.pathname)
|
|
434
|
+
data.$filter = {
|
|
435
|
+
query: {
|
|
436
|
+
$or: [{ pathname: object.pathname }]
|
|
437
|
+
}
|
|
438
|
+
};
|
|
439
|
+
|
|
440
|
+
if (match.length && isMatch)
|
|
441
|
+
response = await runStore(data);
|
|
442
|
+
}
|
|
443
|
+
} catch (err) {
|
|
444
|
+
console.log(err);
|
|
445
|
+
process.exit();
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
if (
|
|
449
|
+
response.object &&
|
|
450
|
+
response.object[0] &&
|
|
451
|
+
response.object[0]._id
|
|
452
|
+
) {
|
|
453
|
+
source.object._id = response.object[0]._id;
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
for (const [key, value] of keys) {
|
|
457
|
+
source.object[key] = value;
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
updatedSources.push(source);
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
return updatedSources;
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
async function runStore(data) {
|
|
467
|
+
try {
|
|
468
|
+
let response;
|
|
469
|
+
if (!data.object._id && !data.$filter) {
|
|
470
|
+
response = await crud.send({
|
|
471
|
+
method: "object.create",
|
|
472
|
+
...config,
|
|
473
|
+
...data
|
|
474
|
+
});
|
|
475
|
+
} else {
|
|
476
|
+
response = await crud.send({
|
|
477
|
+
method: "object.update",
|
|
478
|
+
...config,
|
|
479
|
+
...data,
|
|
480
|
+
upsert: true
|
|
481
|
+
});
|
|
482
|
+
}
|
|
483
|
+
if (response) {
|
|
484
|
+
return response;
|
|
485
|
+
}
|
|
486
|
+
} catch (err) {
|
|
487
|
+
console.log(err);
|
|
488
|
+
return null;
|
|
489
|
+
}
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
async function run() {
|
|
493
|
+
if (directories) await runDirectories();
|
|
494
|
+
|
|
495
|
+
if (sources && sources.length) {
|
|
496
|
+
let sources = await runSources();
|
|
497
|
+
let newConfig = { ...CoCreateConfig };
|
|
498
|
+
if (directories && directories.length)
|
|
499
|
+
newConfig.directories = directories;
|
|
500
|
+
|
|
501
|
+
newConfig.sources = sources;
|
|
502
|
+
|
|
503
|
+
if (newConfig.repositories)
|
|
504
|
+
newConfig.repositories.forEach((obj) => {
|
|
505
|
+
for (const key in obj) {
|
|
506
|
+
if (!["path", "repo", "exclude"].includes(key)) {
|
|
507
|
+
delete obj[key];
|
|
508
|
+
}
|
|
509
|
+
}
|
|
510
|
+
});
|
|
511
|
+
|
|
512
|
+
delete newConfig.url;
|
|
513
|
+
delete newConfig.broadcast;
|
|
514
|
+
|
|
515
|
+
fs.writeFileSync(
|
|
516
|
+
configPath,
|
|
517
|
+
`module.exports = ${JSON.stringify(newConfig, null, 4)};`
|
|
518
|
+
);
|
|
519
|
+
}
|
|
520
|
+
|
|
521
|
+
if (!match.length) {
|
|
522
|
+
console.log("upload complete!");
|
|
523
|
+
|
|
524
|
+
setTimeout(function () {
|
|
525
|
+
process.exit();
|
|
526
|
+
}, 2000);
|
|
527
|
+
}
|
|
528
|
+
}
|
|
529
|
+
|
|
530
|
+
run();
|
|
531
|
+
};
|