@xuda.io/drive_module 1.1.1120 → 1.1.1121

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.
Files changed (2) hide show
  1. package/index.js +362 -704
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -1,72 +1,50 @@
1
- const path = require("path");
2
- global._conf = require(path.join(
3
- process.env.XUDA_HOME,
4
- process.env.XUDA_CONFIG
5
- ));
6
- console.log("drive module loaded..");
7
-
8
- const _common = require(path.join(
9
- process.env.XUDA_HOME,
10
- "common",
11
- "xuda_node_common.js"
12
- ));
13
-
14
- const _ = require("lodash");
15
-
16
- const module_path =
17
- path.join(process.env.XUDA_HOME, "cpi") +
18
- (!_conf.is_debug ? "/node_modules/@xuda.io" : "");
1
+ const path = require('path');
2
+ global._conf = require(path.join(process.env.XUDA_HOME, process.env.XUDA_CONFIG));
3
+ console.log('drive module loaded..');
4
+
5
+ const _common = require(path.join(process.env.XUDA_HOME, 'common', 'xuda_node_common.js'));
6
+
7
+ const _ = require('lodash');
8
+
9
+ const module_path = path.join(process.env.XUDA_HOME, 'cpi') + (!_conf.is_debug ? '/node_modules/@xuda.io' : '');
19
10
  const fs_module = require(`${module_path}/fs_module`);
20
11
  const db_module = require(`${module_path}/db_module`);
21
12
  const misc_module = require(`${module_path}/misc_module`);
22
13
  ////////////////////////////////////////////
23
14
 
24
- const rimraf = require("rimraf");
25
- const fse = require("fs-extra");
26
- const crypto = require("node:crypto");
27
- const unzipper = require("unzipper");
28
- const fs = require("fs");
29
- const tesseract = require("node-tesseract-ocr");
30
- const countryLanguage = require("country-language");
31
- const url = require("url");
15
+ const rimraf = require('rimraf');
16
+ const fse = require('fs-extra');
17
+ const crypto = require('node:crypto');
18
+ const unzipper = require('unzipper');
19
+ const fs = require('fs');
20
+ const tesseract = require('node-tesseract-ocr');
21
+ const countryLanguage = require('country-language');
22
+ const url = require('url');
32
23
  ////////////////////////////////////////////
33
24
 
34
25
  [
35
- "get_drive_files",
36
- "get_drive_file_info",
37
- "delete_drive_files",
38
- "extract_drive_file",
39
- "upload_drive_file",
40
- "update_drive_file",
41
- "create_drive_folder",
42
- "rename_drive_file",
43
- "rename_drive_folder",
44
- "update_drive_file_sharing_mode",
45
- "delete_file_bulk",
46
- "check_drive_file",
47
- "update_drive_file_tags",
48
- "update_drive_addons",
26
+ 'get_drive_files',
27
+ 'get_drive_file_info',
28
+ 'delete_drive_files',
29
+ 'extract_drive_file',
30
+ 'upload_drive_file',
31
+ 'update_drive_file',
32
+ 'create_drive_folder',
33
+ 'rename_drive_file',
34
+ 'rename_drive_folder',
35
+ 'update_drive_file_sharing_mode',
36
+ 'delete_file_bulk',
37
+ 'check_drive_file',
38
+ 'update_drive_file_tags',
39
+ 'update_drive_addons',
49
40
  ].forEach((method) => {
50
- ["workspace", "studio", "user"].forEach((type) => {
51
- module.exports[method + "_" + type] = async function (
52
- req,
53
- job_id,
54
- headers,
55
- file_obj
56
- ) {
41
+ ['workspace', 'studio', 'user'].forEach((type) => {
42
+ module.exports[method + '_' + type] = async function (req, job_id, headers, file_obj) {
57
43
  req.drive_type = type;
58
44
 
59
- if (
60
- type === "workspace" &&
61
- ![
62
- "get_drive_files",
63
- "get_drive_file_info",
64
-
65
- "check_drive_file",
66
- ].includes(method)
67
- ) {
45
+ if (type === 'workspace' && !['get_drive_files', 'get_drive_file_info', 'check_drive_file'].includes(method)) {
68
46
  const { data: app_obj } = await db_module.get_app_obj(req.app_id);
69
- if (app_obj.app_type === "datacenter") {
47
+ if (app_obj.app_type === 'datacenter') {
70
48
  const deploy_module = require(`${module_path}/deploy_module`);
71
49
  deploy_module.reset_deployment_cache(app_obj._id);
72
50
  }
@@ -78,32 +56,17 @@ const url = require("url");
78
56
  });
79
57
 
80
58
  const get_studio_path = function (raw_path, ref, file_name) {
81
- return (
82
- raw_path
83
- .replaceAll(path.join(_conf.studio_drive_path, ref), "")
84
- .replaceAll("/" + file_name, "") || "/"
85
- );
59
+ return raw_path.replaceAll(path.join(_conf.studio_drive_path, ref), '').replaceAll('/' + file_name, '') || '/';
86
60
  };
87
61
 
88
62
  exports.get_drive_files = async function (req) {
89
- const {
90
- from = 0,
91
- to = 99999,
92
- drive_type,
93
- app_id,
94
- uid,
95
- type,
96
- date,
97
- search_string,
98
- sort_by = "name",
99
- sort_dir = "desc",
100
- } = req;
63
+ const { from = 0, to = 99999, drive_type, app_id, uid, type, date, search_string, sort_by = 'name', sort_dir = 'desc' } = req;
101
64
 
102
65
  let { tags = [] } = req;
103
66
  let tags_query = {};
104
67
  const sort_by_obj = {
105
- name: type === "file" ? "originalname" : "folder_name",
106
- date: "date_created",
68
+ name: type === 'file' ? 'originalname' : 'folder_name',
69
+ date: 'date_created',
107
70
  };
108
71
 
109
72
  try {
@@ -124,25 +87,25 @@ exports.get_drive_files = async function (req) {
124
87
  if (search_string) {
125
88
  let mix_conditions;
126
89
  tags_query = search_string
127
- .split(" ")
128
- .filter((e) => e.includes(":"))
90
+ .split(' ')
91
+ .filter((e) => e.includes(':'))
129
92
  .reduce(
130
93
  (ret, val) => {
131
- var tagKey = val.split(":")[0];
132
- var tagVal = val.split(":")[1];
94
+ var tagKey = val.split(':')[0];
95
+ var tagVal = val.split(':')[1];
133
96
 
134
- if (tagKey === "tag") {
97
+ if (tagKey === 'tag') {
135
98
  tags.push(tagVal);
136
99
  return;
137
100
  }
138
101
  mix_conditions = true;
139
- if (tagKey === "name") {
102
+ if (tagKey === 'name') {
140
103
  ret.$or.push({ originalname: { $regex: `(?i)${tagVal}` } });
141
104
  }
142
- if (tagKey === "mime") {
105
+ if (tagKey === 'mime') {
143
106
  ret.$or.push({ mime: { $regex: `(?i)${tagVal}` } });
144
107
  }
145
- if (tagKey === "ext") {
108
+ if (tagKey === 'ext') {
146
109
  ret.$or.push({ file_ext: { $regex: `(?i)${tagVal}` } });
147
110
  }
148
111
 
@@ -150,14 +113,14 @@ exports.get_drive_files = async function (req) {
150
113
  },
151
114
  {
152
115
  $or: [],
153
- }
116
+ },
154
117
  );
155
118
 
156
119
  var new_search = search_string
157
- .split(" ")
158
- .filter((e) => !e.includes(":"))
120
+ .split(' ')
121
+ .filter((e) => !e.includes(':'))
159
122
  .filter((e) => e)
160
- .join(" ");
123
+ .join(' ');
161
124
  if (mix_conditions) {
162
125
  tags_query.$or = [
163
126
  ...tags_query.$or,
@@ -185,30 +148,23 @@ exports.get_drive_files = async function (req) {
185
148
  validate_drive_type(drive_type);
186
149
 
187
150
  function formatBytes(bytes) {
188
- if (bytes < 1024) return bytes + " Bytes";
189
- else if (bytes < 1048576) return (bytes / 1024).toFixed(2) + " KB";
190
- else if (bytes < 1073741824) return (bytes / 1048576).toFixed(2) + " MB";
191
- else return (bytes / 1073741824).toFixed(2) + " GB";
151
+ if (bytes < 1024) return bytes + ' Bytes';
152
+ else if (bytes < 1048576) return (bytes / 1024).toFixed(2) + ' KB';
153
+ else if (bytes < 1073741824) return (bytes / 1048576).toFixed(2) + ' MB';
154
+ else return (bytes / 1073741824).toFixed(2) + ' GB';
192
155
  }
193
156
 
194
- const fetch_drive_files = async function (
195
- ret,
196
- file_doc,
197
- app_id_reference,
198
- uid,
199
- datasource_id,
200
- domain = process.env.XUDA_HOSTNAME
201
- ) {
157
+ const fetch_drive_files = async function (ret, file_doc, app_id_reference, uid, datasource_id, domain = process.env.XUDA_HOSTNAME) {
202
158
  let files = {
203
- path: req.path || "/",
204
- name: file_doc.data.folder_name || "/",
205
- file_path: req.path || "/",
159
+ path: req.path || '/',
160
+ name: file_doc.data.folder_name || '/',
161
+ file_path: req.path || '/',
206
162
  sizeInBytes: 0,
207
163
  children: [],
208
164
  };
209
165
 
210
166
  for await (let doc of ret.docs) {
211
- if (doc.type === "file") {
167
+ if (doc.type === 'file') {
212
168
  try {
213
169
  files.sizeInBytes += doc.size;
214
170
  files.children.push({
@@ -217,8 +173,7 @@ exports.get_drive_files = async function (req) {
217
173
  size: formatBytes(doc.size),
218
174
  extension: doc?.file_ext?.substr(1),
219
175
  type: doc.type,
220
- access_link: `https://${domain}/${drive_type}-drive/${datasource_id || app_id
221
- }/${doc._id}`,
176
+ access_link: `https://${domain}/${drive_type}-drive/${datasource_id || app_id}/${doc._id}`,
222
177
  is_archive: doc.is_archive,
223
178
  public: doc.public,
224
179
  path: doc.file_path,
@@ -230,21 +185,16 @@ exports.get_drive_files = async function (req) {
230
185
  console.error(err.message);
231
186
  }
232
187
  }
233
- if (doc.type === "directory") {
188
+ if (doc.type === 'directory') {
234
189
  try {
235
- const sizeInBytes = await get_folder_size(
236
- req.path,
237
- drive_type,
238
- app_id_reference,
239
- uid
240
- );
190
+ const sizeInBytes = await get_folder_size(req.path, drive_type, app_id_reference, uid);
241
191
  files.sizeInBytes += sizeInBytes;
242
192
  files.children.push({
243
193
  sizeInBytes,
244
194
  size: formatBytes(sizeInBytes),
245
195
  name: doc.folder_name,
246
- path: "/" + doc._id,
247
- file_path: "/" + doc._id,
196
+ path: '/' + doc._id,
197
+ file_path: '/' + doc._id,
248
198
  type: doc.type,
249
199
  is_folder: true,
250
200
  date_created: doc.date_created,
@@ -262,31 +212,21 @@ exports.get_drive_files = async function (req) {
262
212
 
263
213
  const get_workspace_files = async function () {
264
214
  const app_id_reference = await _common.get_project_app_id(app_id);
265
- opt.selector.docType = "workspace_drive";
215
+ opt.selector.docType = 'workspace_drive';
266
216
 
267
217
  const ret = await db_module.find_app_couch_query(app_id, opt);
268
218
 
269
- const file_doc = await db_module.get_app_couch_doc(
270
- app_id,
271
- path.basename(req.path) || "/"
272
- );
219
+ const file_doc = await db_module.get_app_couch_doc(app_id, path.basename(req.path) || '/');
273
220
 
274
221
  const { data: app_obj } = await db_module.get_app_obj(app_id);
275
222
 
276
223
  return {
277
224
  code: 1,
278
- data: await fetch_drive_files(
279
- ret,
280
- file_doc,
281
- app_id_reference,
282
- null,
283
- app_obj.app_datacenter_id,
284
- app_obj.is_deployment ? app_obj.app_access_url : undefined
285
- ),
225
+ data: await fetch_drive_files(ret, file_doc, app_id_reference, null, app_obj.app_datacenter_id, app_obj.is_deployment ? app_obj.app_access_url : undefined),
286
226
  };
287
227
  };
288
228
  const get_user_files = async function () {
289
- opt.selector.docType = "user_drive";
229
+ opt.selector.docType = 'user_drive';
290
230
  opt.selector.uid = uid;
291
231
 
292
232
  return {
@@ -297,13 +237,10 @@ exports.get_drive_files = async function (req) {
297
237
  const get_studio_files = async function () {
298
238
  if (search_string) {
299
239
  const app_id_reference = await _common.get_project_app_id(app_id);
300
- opt.selector.docType = "studio_drive";
240
+ opt.selector.docType = 'studio_drive';
301
241
  const ret = await db_module.find_app_couch_query(app_id, opt);
302
242
 
303
- const file_doc = await db_module.get_app_couch_doc(
304
- app_id,
305
- path.basename(req.path) || "/"
306
- );
243
+ const file_doc = await db_module.get_app_couch_doc(app_id, path.basename(req.path) || '/');
307
244
 
308
245
  return {
309
246
  code: 1,
@@ -313,11 +250,7 @@ exports.get_drive_files = async function (req) {
313
250
 
314
251
  const app_id_master = await _common.get_project_app_id(app_id, true);
315
252
 
316
- var directoryPath = path.join(
317
- _conf.studio_drive_path,
318
- app_id_master,
319
- req.path || "/"
320
- );
253
+ var directoryPath = path.join(_conf.studio_drive_path, app_id_master, req.path || '/');
321
254
  // var directoryPath =
322
255
  // req.path !== "/"
323
256
  // ? req.path || "/"
@@ -341,10 +274,7 @@ exports.get_drive_files = async function (req) {
341
274
  var tree = dree_ret.data; // dree.scan(directoryPath, options);
342
275
 
343
276
  const rep = async function (file_path, type) {
344
- file_path = file_path.replace(
345
- `${_conf.studio_drive_path}/${app_id_master}/`,
346
- ""
347
- );
277
+ file_path = file_path.replace(`${_conf.studio_drive_path}/${app_id_master}/`, '');
348
278
 
349
279
  return `https://${process.env.XUDA_HOSTNAME}/studio-drive/${app_id_master}/${file_path}`;
350
280
  };
@@ -362,28 +292,20 @@ exports.get_drive_files = async function (req) {
362
292
  }
363
293
 
364
294
  const internal_path = val.path;
365
- const directory_path = get_studio_path(
366
- val.path,
367
- app_id_master,
368
- val.relativePath
369
- );
295
+ const directory_path = get_studio_path(val.path, app_id_master, val.relativePath);
370
296
  const file_path = path.join(directory_path, val.name);
371
297
 
372
298
  let ret = await db_module.get_app_couch_doc(app_id_master, file_path);
373
299
 
374
300
  if (ret.code > -1) {
375
- if (val.type === "file") {
301
+ if (val.type === 'file') {
376
302
  val.cy_data = ret.data;
377
303
 
378
304
  val.access_link = await rep(internal_path, drive_type);
379
305
  }
380
- if (val.type === "directory") {
306
+ if (val.type === 'directory') {
381
307
  val.name = ret.data.folder_name;
382
- val.sizeInBytes = await get_folder_size(
383
- internal_path,
384
- drive_type,
385
- app_id_master
386
- );
308
+ val.sizeInBytes = await get_folder_size(internal_path, drive_type, app_id_master);
387
309
  }
388
310
 
389
311
  delete ret.data._id;
@@ -392,15 +314,11 @@ exports.get_drive_files = async function (req) {
392
314
  delete ret.data.docType;
393
315
  delete ret.data.stat;
394
316
  } else {
395
- if (val.type === "directory") {
396
- val.sizeInBytes = await get_folder_size(
397
- internal_path,
398
- drive_type,
399
- app_id_master
400
- );
317
+ if (val.type === 'directory') {
318
+ val.sizeInBytes = await get_folder_size(internal_path, drive_type, app_id_master);
401
319
  }
402
320
  // supports file uploaded directly to the server
403
- val.access_link = await rep(val.path, "studio"); //+ "?r=" + Date.now();
321
+ val.access_link = await rep(val.path, 'studio'); //+ "?r=" + Date.now();
404
322
  }
405
323
  val.date_created = new Date(val.stat.birthtime).valueOf();
406
324
  delete val.stat;
@@ -415,10 +333,8 @@ exports.get_drive_files = async function (req) {
415
333
  };
416
334
 
417
335
  if (tree?.children) {
418
- if (type === "file") {
419
- tree.children = tree.children
420
- .filter((e) => e.type === type)
421
- .slice(from, to + 1);
336
+ if (type === 'file') {
337
+ tree.children = tree.children.filter((e) => e.type === type).slice(from, to + 1);
422
338
  }
423
339
  await drill(tree.children);
424
340
  } else {
@@ -429,11 +345,7 @@ exports.get_drive_files = async function (req) {
429
345
  };
430
346
  }
431
347
 
432
- tree.path = get_studio_path(
433
- tree.path || tree.name,
434
- app_id_master,
435
- tree?.name || ""
436
- );
348
+ tree.path = get_studio_path(tree.path || tree.name, app_id_master, tree?.name || '');
437
349
  tree.file_path = path.join(tree.path, tree.name);
438
350
 
439
351
  return {
@@ -443,17 +355,17 @@ exports.get_drive_files = async function (req) {
443
355
  };
444
356
 
445
357
  switch (drive_type) {
446
- case "studio":
358
+ case 'studio':
447
359
  return get_studio_files();
448
360
 
449
- case "workspace":
361
+ case 'workspace':
450
362
  return get_workspace_files();
451
363
 
452
- case "user":
364
+ case 'user':
453
365
  return get_user_files();
454
366
 
455
367
  default:
456
- throw new Error("drive_type error");
368
+ throw new Error('drive_type error');
457
369
  }
458
370
  } catch (err) {
459
371
  return err.message;
@@ -464,14 +376,7 @@ exports.get_drive_file_info = async function (req, job_id, headers) {
464
376
  try {
465
377
  const { type, drive_type, uid, app_id, file_path } = req;
466
378
 
467
- let doc = await get_drive_doc(
468
- type,
469
- drive_type,
470
- app_id,
471
- uid,
472
- file_path,
473
- headers
474
- );
379
+ let doc = await get_drive_doc(type, drive_type, app_id, uid, file_path, headers);
475
380
 
476
381
  delete doc._id;
477
382
  delete doc._rev;
@@ -485,10 +390,10 @@ exports.get_drive_file_info = async function (req, job_id, headers) {
485
390
  delete doc.bucket;
486
391
 
487
392
  if (doc.uid) {
488
- let ret = await db_module.get_couch_doc("xuda_accounts", doc.uid);
393
+ let ret = await db_module.get_couch_doc('xuda_accounts', doc.uid);
489
394
  doc.account_info = ret.data.account_info;
490
395
  }
491
- if (req.drive_type === "workspace") {
396
+ if (req.drive_type === 'workspace') {
492
397
  const app_id_master = await _common.get_project_app_id(app_id, true);
493
398
  const ret = await db_module.get_app_obj(app_id_master);
494
399
 
@@ -523,51 +428,34 @@ exports.delete_drive_files = async function (req, job_id, headers) {
523
428
  if (!Array.isArray(files_arr)) {
524
429
  return {
525
430
  code: -400,
526
- data: "error - files_arr must be Array",
431
+ data: 'error - files_arr must be Array',
527
432
  };
528
433
  }
529
434
 
530
435
  for await (const file of files_arr) {
531
- let doc = await get_drive_doc(
532
- "file",
533
- drive_type,
534
- app_id,
535
- uid,
536
- file.file_path,
537
- headers
538
- );
436
+ let doc = await get_drive_doc('file', drive_type, app_id, uid, file.file_path, headers);
539
437
 
540
438
  switch (drive_type) {
541
- case "studio": {
439
+ case 'studio': {
542
440
  const app_id_reference = await _common.get_project_app_id(app_id);
543
- const target_dir = path.join(
544
- _conf[`studio_drive_path`],
545
- app_id_reference,
546
- file.file_path
547
- );
548
- console.log("target_dir", target_dir);
441
+ const target_dir = path.join(_conf[`studio_drive_path`], app_id_reference, file.file_path);
442
+ console.log('target_dir', target_dir);
549
443
  const ret = rimraf.sync(target_dir); // delete the file
550
444
 
551
- if (doc.type === "file") {
445
+ if (doc.type === 'file') {
552
446
  doc.ts = Date.now();
553
447
  doc.stat = 4;
554
- save_ret = await db_module.save_app_couch_doc(
555
- app_id_reference,
556
- doc
557
- );
448
+ save_ret = await db_module.save_app_couch_doc(app_id_reference, doc);
558
449
  }
559
450
 
560
- misc_module.rsync_drive_resources_to_remote_servers(
561
- "studio",
562
- app_id_reference
563
- );
451
+ misc_module.rsync_drive_resources_to_remote_servers('studio', app_id_reference);
564
452
  break;
565
453
  }
566
454
 
567
- case "workspace": {
455
+ case 'workspace': {
568
456
  const app_id_reference = await _common.get_project_app_id(app_id);
569
457
 
570
- if (doc.type === "file") {
458
+ if (doc.type === 'file') {
571
459
  await module.exports.delete_file_from_bucket(doc.bucket);
572
460
  }
573
461
 
@@ -578,8 +466,8 @@ exports.delete_drive_files = async function (req, job_id, headers) {
578
466
  break;
579
467
  }
580
468
 
581
- case "user": {
582
- if (doc.type === "file") {
469
+ case 'user': {
470
+ if (doc.type === 'file') {
583
471
  await module.exports.delete_file_from_bucket(doc.bucket);
584
472
  }
585
473
  doc.ts = Date.now();
@@ -594,16 +482,13 @@ exports.delete_drive_files = async function (req, job_id, headers) {
594
482
  }
595
483
 
596
484
  switch (drive_type) {
597
- case "workspace": {
485
+ case 'workspace': {
598
486
  const app_id_reference = await _common.get_project_app_id(app_id);
599
- misc_module.rsync_drive_resources_to_remote_servers(
600
- "workspace",
601
- app_id_reference
602
- );
487
+ misc_module.rsync_drive_resources_to_remote_servers('workspace', app_id_reference);
603
488
  break;
604
489
  }
605
- case "user": {
606
- misc_module.rsync_drive_resources_to_remote_servers("user", uid);
490
+ case 'user': {
491
+ misc_module.rsync_drive_resources_to_remote_servers('user', uid);
607
492
  break;
608
493
  }
609
494
  default:
@@ -621,45 +506,35 @@ exports.delete_drive_files = async function (req, job_id, headers) {
621
506
 
622
507
  exports.update_drive_file = async function (req, job_id, headers, file_obj) {
623
508
  try {
509
+ debugger;
624
510
  const { drive_type, app_id, file_path, uid } = req;
625
511
  validate_drive_type(drive_type);
626
512
 
627
- const originalname = file_obj?.["originalname"];
513
+ const originalname = file_obj?.['originalname'];
628
514
 
629
515
  const file_name = path.basename(file_path);
630
516
 
631
517
  if (!originalname) {
632
- return { code: -4, data: "file data missing" };
518
+ return { code: -4, data: 'file data missing' };
633
519
  }
634
520
 
635
- let doc = await get_drive_doc(
636
- "file",
637
- drive_type,
638
- app_id,
639
- uid,
640
- file_path,
641
- headers
642
- );
521
+ let doc = await get_drive_doc('file', drive_type, app_id, uid, file_path, headers);
643
522
 
644
523
  var target_file = file_path;
645
524
  const master_id = await _common.get_project_app_id(app_id, true);
646
525
  const tempPath = file_obj.path;
647
- if (drive_type === "studio") {
648
- const target_dir = path.join(
649
- _conf[`studio_drive_path`],
650
- master_id,
651
- file_path
652
- );
526
+ if (drive_type === 'studio') {
527
+ const target_dir = path.join(_conf[`studio_drive_path`], master_id, file_path);
653
528
  try {
654
529
  fse.unlinkSync(target_dir);
655
- } catch (error) { }
530
+ } catch (error) {}
656
531
 
657
532
  fse.copyFileSync(tempPath, target_dir);
658
533
  }
659
534
 
660
535
  doc.date_modified = Date.now();
661
536
 
662
- const fs = require("fs").promises;
537
+ const fs = require('fs').promises;
663
538
  const stat = await fs.stat(tempPath);
664
539
  if (stat.isFile()) {
665
540
  doc.size = stat.size;
@@ -668,46 +543,32 @@ exports.update_drive_file = async function (req, job_id, headers, file_obj) {
668
543
  doc = { ...doc, ...(await get_file_info(tempPath)) };
669
544
 
670
545
  let save_ret;
671
- let ref
546
+ let ref;
672
547
  switch (drive_type) {
673
- case "studio": {
548
+ case 'studio': {
674
549
  ref = await _common.get_project_app_id(app_id, true);
675
550
  const app_id_reference = await _common.get_project_app_id(app_id);
676
551
  save_ret = await db_module.save_app_couch_doc(app_id_reference, doc);
677
552
 
678
- misc_module.rsync_drive_resources_to_remote_servers(
679
- "studio",
680
- app_id_reference
681
- );
553
+ misc_module.rsync_drive_resources_to_remote_servers('studio', app_id_reference);
682
554
  break;
683
555
  }
684
- case "workspace": {
685
-
556
+ case 'workspace': {
686
557
  const app_id_reference = await _common.get_project_app_id(app_id);
687
558
  const app_id_master = await _common.get_project_app_id(app_id, true);
688
559
  ref = app_id_master;
689
560
 
690
- doc.bucket[`${process.env.XUDA_HOSTNAME}`] =
691
- await upload_file_to_spaces(
692
- tempPath,
693
- app_id_master,
694
- drive_type,
695
- file_name
696
- );
561
+ doc.bucket[`${process.env.XUDA_HOSTNAME}`] = await upload_file_to_spaces(tempPath, app_id_master, drive_type, file_name);
697
562
 
698
563
  save_ret = await db_module.save_app_couch_doc(app_id_reference, doc);
699
- misc_module.rsync_drive_resources_to_remote_servers(
700
- "workspace",
701
- app_id_master
702
- );
564
+ misc_module.rsync_drive_resources_to_remote_servers('workspace', app_id_master);
703
565
  break;
704
566
  }
705
- case "user": {
567
+ case 'user': {
706
568
  ref = uid;
707
- doc.bucket[`${process.env.XUDA_HOSTNAME}`] =
708
- await upload_file_to_spaces(tempPath, uid, drive_type, file_name);
709
- save_ret = await db_module.save_couch_doc("xuda_drive", doc);
710
- misc_module.rsync_drive_resources_to_remote_servers("user", doc.uid);
569
+ doc.bucket[`${process.env.XUDA_HOSTNAME}`] = await upload_file_to_spaces(tempPath, uid, drive_type, file_name);
570
+ save_ret = await db_module.save_couch_doc('xuda_drive', doc);
571
+ misc_module.rsync_drive_resources_to_remote_servers('user', doc.uid);
711
572
  break;
712
573
  }
713
574
  default:
@@ -719,9 +580,7 @@ exports.update_drive_file = async function (req, job_id, headers, file_obj) {
719
580
  if (save_ret.code < 0) {
720
581
  return save_ret;
721
582
  }
722
- const server_file_name = `https://${process.env.XUDA_HOSTNAME
723
- }/${drive_type}-drive${drive_type === "studio" ? `/${master_id}/${target_file}` : file_name
724
- }`;
583
+ const server_file_name = `https://${process.env.XUDA_HOSTNAME}/${drive_type}-drive${drive_type === 'studio' ? `/${master_id}/${target_file}` : file_name}`;
725
584
 
726
585
  let file_url = get_file_url(drive_type, file_name, ref);
727
586
 
@@ -730,7 +589,8 @@ exports.update_drive_file = async function (req, job_id, headers, file_obj) {
730
589
  code: 760,
731
590
  data: {
732
591
  server_file_name,
733
- filename: file_obj.originalname, file_url
592
+ filename: file_obj.originalname,
593
+ file_url,
734
594
  },
735
595
  };
736
596
  } catch (err) {
@@ -754,50 +614,47 @@ exports.create_drive_folder = async function (req, job_id, headers) {
754
614
  folder_name,
755
615
  uid,
756
616
  date_created: Date.now(),
757
- ip: headers?.["cf-connecting-ip"],
758
- country: headers?.["cf-ipcountry"],
617
+ ip: headers?.['cf-connecting-ip'],
618
+ country: headers?.['cf-ipcountry'],
759
619
  is_folder: true,
760
- type: "directory",
620
+ type: 'directory',
761
621
  file_path: req.path,
762
622
  };
763
623
 
764
624
  switch (drive_type) {
765
- case "studio": {
625
+ case 'studio': {
766
626
  folder_id = req.folder_name;
767
627
  const app_id_master = await _common.get_project_app_id(app_id, true);
768
628
  if (!req.path.includes(app_id_master)) {
769
629
  return {
770
630
  code: -1,
771
- data: "error - file path unauthorized",
631
+ data: 'error - file path unauthorized',
772
632
  };
773
633
  }
774
- target_dir = req.path + "/" + folder_id;
634
+ target_dir = req.path + '/' + folder_id;
775
635
  folder_exist = await file_exists(target_dir);
776
636
  if (folder_exist) {
777
- throw new Error("folder exist");
637
+ throw new Error('folder exist');
778
638
  }
779
639
  await create_directory(target_dir, 0o775, true);
780
640
  doc._id = folder_id;
781
641
  save_ret = await db_module.save_app_couch_doc(app_id_master, doc);
782
- misc_module.rsync_drive_resources_to_remote_servers(
783
- "studio",
784
- app_id_master
785
- );
642
+ misc_module.rsync_drive_resources_to_remote_servers('studio', app_id_master);
786
643
  break;
787
644
  }
788
645
 
789
- case "workspace": {
790
- folder_id = "drive_folder_" + crypto.randomUUID();
646
+ case 'workspace': {
647
+ folder_id = 'drive_folder_' + crypto.randomUUID();
791
648
  app_id_reference = await _common.get_project_app_id(app_id);
792
649
  doc._id = folder_id;
793
650
  save_ret = await db_module.save_app_couch_doc(app_id_reference, doc);
794
651
  break;
795
652
  }
796
653
 
797
- case "user": {
798
- folder_id = "drive_folder_" + crypto.randomUUID();
654
+ case 'user': {
655
+ folder_id = 'drive_folder_' + crypto.randomUUID();
799
656
  doc._id = folder_id;
800
- save_ret = await db_module.save_couch_doc("xuda_drive", doc);
657
+ save_ret = await db_module.save_couch_doc('xuda_drive', doc);
801
658
  break;
802
659
  }
803
660
  default:
@@ -824,16 +681,9 @@ exports.rename_drive_file = async function (req, job_id, headers) {
824
681
  const { drive_type, app_id, file_name_to, file_path } = req;
825
682
  validate_drive_type(drive_type);
826
683
 
827
- let doc = await get_drive_doc(
828
- "file",
829
- drive_type,
830
- app_id,
831
- uid,
832
- file_path,
833
- headers
834
- );
684
+ let doc = await get_drive_doc('file', drive_type, app_id, uid, file_path, headers);
835
685
 
836
- if (drive_type === "studio") {
686
+ if (drive_type === 'studio') {
837
687
  await fs_module.rename(file_path, file_name_to);
838
688
 
839
689
  doc._delete = true;
@@ -848,14 +698,14 @@ exports.rename_drive_file = async function (req, job_id, headers) {
848
698
  let save_ret;
849
699
 
850
700
  switch (drive_type) {
851
- case "workspace":
852
- case "studio":
701
+ case 'workspace':
702
+ case 'studio':
853
703
  const app_id_reference = await _common.get_project_app_id(app_id);
854
704
  save_ret = await db_module.save_app_couch_doc(app_id_reference, doc);
855
705
  break;
856
706
 
857
- case "user":
858
- save_ret = await db_module.save_couch_doc("xuda_drive", doc);
707
+ case 'user':
708
+ save_ret = await db_module.save_couch_doc('xuda_drive', doc);
859
709
  break;
860
710
 
861
711
  default:
@@ -884,39 +734,27 @@ exports.rename_drive_folder = async function (req) {
884
734
 
885
735
  let doc_ret;
886
736
  switch (drive_type) {
887
- case "studio": {
737
+ case 'studio': {
888
738
  const app_id_reference = await _common.get_project_app_id(app_id);
889
739
 
890
740
  if (!new_path.includes(app_id_reference)) {
891
- throw new Error("error - new_path unauthorized");
741
+ throw new Error('error - new_path unauthorized');
892
742
  }
893
743
 
894
- doc_ret = await db_module.get_app_couch_doc(
895
- app_id_reference,
896
- curr_path
897
- );
744
+ doc_ret = await db_module.get_app_couch_doc(app_id_reference, curr_path);
898
745
  await fs_module.rename(curr_path, new_path);
899
746
  doc_ret.data._delete = true;
900
- save_ret = await db_module.save_app_couch_doc(
901
- app_id_reference,
902
- doc_ret.data
903
- );
747
+ save_ret = await db_module.save_app_couch_doc(app_id_reference, doc_ret.data);
904
748
 
905
749
  doc_ret.data._id = file_name_to;
906
750
  delete doc_ret.data._rev;
907
751
 
908
- misc_module.rsync_drive_resources_to_remote_servers(
909
- "studio",
910
- app_id_reference
911
- );
752
+ misc_module.rsync_drive_resources_to_remote_servers('studio', app_id_reference);
912
753
  break;
913
754
  }
914
- case "workspace": {
755
+ case 'workspace': {
915
756
  const app_id_reference = await _common.get_project_app_id(app_id);
916
- doc_ret = await db_module.get_app_couch_doc(
917
- app_id_reference,
918
- curr_path
919
- );
757
+ doc_ret = await db_module.get_app_couch_doc(app_id_reference, curr_path);
920
758
  if (doc_ret.code < 0) {
921
759
  throw new Error(doc_ret.data);
922
760
  }
@@ -924,8 +762,8 @@ exports.rename_drive_folder = async function (req) {
924
762
  break;
925
763
  }
926
764
 
927
- case "user":
928
- doc_ret = await db_module.get_couch_doc("xuda_drive", curr_path);
765
+ case 'user':
766
+ doc_ret = await db_module.get_couch_doc('xuda_drive', curr_path);
929
767
  if (doc_ret.code < 0) {
930
768
  throw new Error(doc_ret.data);
931
769
  }
@@ -943,14 +781,14 @@ exports.rename_drive_folder = async function (req) {
943
781
  let save_ret;
944
782
 
945
783
  switch (drive_type) {
946
- case "workspace":
947
- case "studio":
784
+ case 'workspace':
785
+ case 'studio':
948
786
  const app_id_reference = await _common.get_project_app_id(app_id);
949
787
  save_ret = await db_module.save_app_couch_doc(app_id_reference, doc);
950
788
  break;
951
789
 
952
- case "user":
953
- save_ret = await db_module.save_couch_doc("xuda_drive", doc);
790
+ case 'user':
791
+ save_ret = await db_module.save_couch_doc('xuda_drive', doc);
954
792
  break;
955
793
 
956
794
  default:
@@ -993,14 +831,7 @@ exports.update_drive_file_sharing_mode = async function (req, job_id, headers) {
993
831
 
994
832
  validate_drive_type(drive_type);
995
833
 
996
- let doc = await get_drive_doc(
997
- type,
998
- drive_type,
999
- app_id,
1000
- uid,
1001
- file_path,
1002
- headers
1003
- );
834
+ let doc = await get_drive_doc(type, drive_type, app_id, uid, file_path, headers);
1004
835
 
1005
836
  doc.ts = Date.now();
1006
837
  doc.public = is_public;
@@ -1017,23 +848,16 @@ exports.extract_drive_file = async function (req, job_id, headers) {
1017
848
  const app_id_master = await _common.get_project_app_id(app_id, true);
1018
849
  const app_id_reference = await _common.get_project_app_id(app_id);
1019
850
 
1020
- let doc = await get_drive_doc(
1021
- "file",
1022
- drive_type,
1023
- app_id,
1024
- uid,
1025
- file_path,
1026
- headers
1027
- );
851
+ let doc = await get_drive_doc('file', drive_type, app_id, uid, file_path, headers);
1028
852
 
1029
853
  const ext = path.extname(file_path).toLowerCase();
1030
854
 
1031
855
  if (!_conf.archive_drive_ext.includes(ext)) {
1032
- throw new Error("error - invalid archive file");
856
+ throw new Error('error - invalid archive file');
1033
857
  }
1034
858
 
1035
859
  if (!file_exists(req)) {
1036
- throw new Error("error - file not found");
860
+ throw new Error('error - file not found');
1037
861
  }
1038
862
 
1039
863
  try {
@@ -1041,43 +865,34 @@ exports.extract_drive_file = async function (req, job_id, headers) {
1041
865
 
1042
866
  fs.createReadStream(file_path)
1043
867
  .pipe(unzipper.Parse())
1044
- .on("entry", async function (entry) {
868
+ .on('entry', async function (entry) {
1045
869
  const fileName = entry.path;
1046
870
  const type = entry.type;
1047
871
  const size = entry.vars.uncompressedSize; // There is also compressedSize;
1048
872
  const ext = path.extname(fileName).toLowerCase();
1049
873
 
1050
- if (fileName.startsWith("__MACOSX/")) {
874
+ if (fileName.startsWith('__MACOSX/')) {
1051
875
  return;
1052
876
  }
1053
877
  // var file_id = path.join(path.dirname(file_path), fileName) + "1";
1054
878
 
1055
879
  var new_file_id = path.basename(fileName);
1056
880
 
1057
- if (doc.drive_type !== "studio") {
1058
- new_file_id =
1059
- "drv_" +
1060
- app_id_master +
1061
- "_" +
1062
- (await _common.xuda_get_uuid(doc.drive_type)) +
1063
- ext;
881
+ if (doc.drive_type !== 'studio') {
882
+ new_file_id = 'drv_' + app_id_master + '_' + (await _common.xuda_get_uuid(doc.drive_type)) + ext;
1064
883
  }
1065
884
 
1066
- const folder_name =
1067
- "drive_folder_" +
1068
- (doc.drive_type === "studio"
1069
- ? path.basename(fileName)
1070
- : crypto.randomUUID());
885
+ const folder_name = 'drive_folder_' + (doc.drive_type === 'studio' ? path.basename(fileName) : crypto.randomUUID());
1071
886
 
1072
887
  var dir = path.dirname(fileName);
1073
- if (dir && dir !== ".") {
1074
- dir = "drive_folder_" + dir;
888
+ if (dir && dir !== '.') {
889
+ dir = 'drive_folder_' + dir;
1075
890
  }
1076
891
 
1077
- const file_doc_id = type === "Directory" ? folder_name : new_file_id;
892
+ const file_doc_id = type === 'Directory' ? folder_name : new_file_id;
1078
893
  const extract_path = path.join(extract_dir, dir, file_doc_id);
1079
894
 
1080
- if (type === "Directory") {
895
+ if (type === 'Directory') {
1081
896
  fs.mkdirSync(extract_path, { recursive: true });
1082
897
  } else {
1083
898
  entry.pipe(fs.createWriteStream(extract_path));
@@ -1097,13 +912,13 @@ exports.extract_drive_file = async function (req, job_id, headers) {
1097
912
  app_id: app_id_reference,
1098
913
  app_id_reference: app_id_master,
1099
914
  date_created: Date.now(),
1100
- ip: headers["cf-connecting-ip"],
1101
- country: headers["cf-ipcountry"],
915
+ ip: headers['cf-connecting-ip'],
916
+ country: headers['cf-ipcountry'],
1102
917
  file_ext: ext,
1103
918
  is_archive: _conf.archive_drive_ext.includes(ext) ? true : false,
1104
919
  size,
1105
920
  };
1106
- if (type === "Directory") {
921
+ if (type === 'Directory') {
1107
922
  file_doc.folder_name = path.basename(fileName);
1108
923
  } else {
1109
924
  file_doc.originalname = new_file_id;
@@ -1111,13 +926,13 @@ exports.extract_drive_file = async function (req, job_id, headers) {
1111
926
 
1112
927
  try {
1113
928
  await db_module.save_app_couch_doc(app_id_reference, file_doc);
1114
- } catch (error) { }
929
+ } catch (error) {}
1115
930
  })
1116
- .on("close", () => {
1117
- console.log("Extraction complete.");
931
+ .on('close', () => {
932
+ console.log('Extraction complete.');
1118
933
  })
1119
- .on("error", (error) => {
1120
- console.error("An error occurred:", error);
934
+ .on('error', (error) => {
935
+ console.error('An error occurred:', error);
1121
936
  });
1122
937
  } catch (err) {
1123
938
  return {
@@ -1128,7 +943,7 @@ exports.extract_drive_file = async function (req, job_id, headers) {
1128
943
 
1129
944
  return {
1130
945
  code: 200,
1131
- data: "",
946
+ data: '',
1132
947
  };
1133
948
  };
1134
949
 
@@ -1140,10 +955,10 @@ const create_directory = async function (file_path, permission) {
1140
955
  };
1141
956
 
1142
957
  exports.delete_file_bulk = async function (req) {
1143
- if (typeof req.server_files !== "object") {
958
+ if (typeof req.server_files !== 'object') {
1144
959
  return {
1145
960
  code: -1,
1146
- data: "error - server_files must be defined as array",
961
+ data: 'error - server_files must be defined as array',
1147
962
  };
1148
963
  }
1149
964
 
@@ -1156,57 +971,49 @@ exports.delete_file_bulk = async function (req) {
1156
971
 
1157
972
  exports.upload_drive_file = async function (req, job_id, headers, file_obj) {
1158
973
  try {
1159
- const { drive_type, app_id, uid, make_public, tags = "" } = req;
974
+ const { drive_type, app_id, uid, make_public, tags = '' } = req;
1160
975
 
1161
976
  /// validations
1162
977
  validate_drive_type(drive_type);
1163
- if (!file_obj?.["originalname"]) {
1164
- throw new Error("file data missing");
978
+ if (!file_obj?.['originalname']) {
979
+ throw new Error('file data missing');
1165
980
  }
1166
981
 
1167
982
  const ext = path.extname(file_obj.originalname).toLowerCase();
1168
983
  let originalname = path.basename(file_obj.originalname);
1169
984
  const tempPath = file_obj.path;
1170
985
 
1171
- if (
1172
- ![..._conf.allowedExts_drive, ..._conf.archive_drive_ext].includes(ext)
1173
- ) {
986
+ if (![..._conf.allowedExts_drive, ..._conf.archive_drive_ext].includes(ext)) {
1174
987
  await fs_module.unlink(tempPath);
1175
- throw new Error(
1176
- `Only ${_conf.allowedExts_drive.toString()} files are allowed!`
1177
- );
988
+ throw new Error(`Only ${_conf.allowedExts_drive.toString()} files are allowed!`);
1178
989
  }
1179
990
 
1180
991
  let ref, file_name, target_file, save_ret;
1181
992
 
1182
- const fs = require("fs").promises;
993
+ const fs = require('fs').promises;
1183
994
  const stat = await fs.stat(tempPath);
1184
995
 
1185
996
  let tags_arr = [];
1186
997
  try {
1187
- if (tags) tags_arr = tags.split(",");
1188
- } catch (error) { }
998
+ if (tags) tags_arr = tags.split(',');
999
+ } catch (error) {}
1189
1000
 
1190
1001
  var doc = {
1191
1002
  docType: `${drive_type}_drive`,
1192
1003
  stat: 3,
1193
1004
  originalname,
1194
1005
  uid,
1195
- type: "file",
1006
+ type: 'file',
1196
1007
  date_created: Date.now(),
1197
- ip: headers["cf-connecting-ip"],
1198
- country: headers["cf-ipcountry"],
1008
+ ip: headers['cf-connecting-ip'],
1009
+ country: headers['cf-ipcountry'],
1199
1010
  file_ext: ext,
1200
1011
  is_archive: _conf.archive_drive_ext.includes(ext) ? true : false,
1201
1012
  drive_type,
1202
1013
  mime: file_obj.mimetype,
1203
- ocr_stat: ["image", "application", "text"].includes(
1204
- file_obj.mimetype.split("/")[0]
1205
- )
1206
- ? 1
1207
- : 0,
1014
+ ocr_stat: ['image', 'application', 'text'].includes(file_obj.mimetype.split('/')[0]) ? 1 : 0,
1208
1015
  target_file,
1209
- file_path: req.path || "/",
1016
+ file_path: req.path || '/',
1210
1017
  size: stat.isFile() ? stat.size : 0,
1211
1018
  public: make_public,
1212
1019
  tags: tags_arr,
@@ -1218,16 +1025,14 @@ exports.upload_drive_file = async function (req, job_id, headers, file_obj) {
1218
1025
  uid,
1219
1026
  drive_type,
1220
1027
  file_path: path.join(doc.file_path, originalname),
1221
- type: "file",
1028
+ type: 'file',
1222
1029
  };
1223
- const check_existing_file_ret = await module.exports.check_drive_file(
1224
- check_req
1225
- );
1030
+ const check_existing_file_ret = await module.exports.check_drive_file(check_req);
1226
1031
 
1227
1032
  if (check_existing_file_ret.code < 0) {
1228
1033
  const rename_file_name = async function (i) {
1229
1034
  if (i > 1000) {
1230
- return { code: -1, data: "too many retries renaming existing file" };
1035
+ return { code: -1, data: 'too many retries renaming existing file' };
1231
1036
  }
1232
1037
 
1233
1038
  let tmp_originalname = originalname + ` (${i})`;
@@ -1251,7 +1056,7 @@ exports.upload_drive_file = async function (req, job_id, headers, file_obj) {
1251
1056
  }
1252
1057
 
1253
1058
  switch (drive_type) {
1254
- case "studio": {
1059
+ case 'studio': {
1255
1060
  ref = await _common.get_project_app_id(req.app_id, true);
1256
1061
  const target_dir = path.join(_conf[`studio_drive_path`], ref);
1257
1062
  file_name = path.join(req.path, file_obj.originalname);
@@ -1264,7 +1069,7 @@ exports.upload_drive_file = async function (req, job_id, headers, file_obj) {
1264
1069
  if (await fs_module.file_exists(target_file)) {
1265
1070
  return {
1266
1071
  code: -762,
1267
- data: "file already exist",
1072
+ data: 'file already exist',
1268
1073
  };
1269
1074
  }
1270
1075
 
@@ -1277,13 +1082,12 @@ exports.upload_drive_file = async function (req, job_id, headers, file_obj) {
1277
1082
  doc._id = file_name;
1278
1083
 
1279
1084
  save_ret = await db_module.save_app_couch_doc(ref, doc);
1280
- misc_module.rsync_drive_resources_to_remote_servers("studio", app_id);
1085
+ misc_module.rsync_drive_resources_to_remote_servers('studio', app_id);
1281
1086
  break;
1282
1087
  }
1283
- case "workspace": {
1088
+ case 'workspace': {
1284
1089
  ref = await _common.get_project_app_id(req.app_id, true);
1285
- file_name =
1286
- "drv_" + ref + "_" + (await _common.xuda_get_uuid(drive_type)) + ext;
1090
+ file_name = 'drv_' + ref + '_' + (await _common.xuda_get_uuid(drive_type)) + ext;
1287
1091
 
1288
1092
  // keep the original filename to support migration
1289
1093
  if (file_obj.originalname.includes(`drv_${ref}`)) {
@@ -1314,25 +1118,14 @@ exports.upload_drive_file = async function (req, job_id, headers, file_obj) {
1314
1118
  // }
1315
1119
 
1316
1120
  doc.bucket = {
1317
- [`${process.env.XUDA_HOSTNAME}`]: await upload_file_to_spaces(
1318
- tempPath,
1319
- ref,
1320
- drive_type,
1321
- file_name
1322
- ),
1121
+ [`${process.env.XUDA_HOSTNAME}`]: await upload_file_to_spaces(tempPath, ref, drive_type, file_name),
1323
1122
  };
1324
1123
  doc._id = file_name;
1325
- save_ret = await db_module.save_app_couch_doc(
1326
- await _common.get_project_app_id(req.app_id),
1327
- doc
1328
- );
1329
- misc_module.rsync_drive_resources_to_remote_servers(
1330
- "workspace",
1331
- app_id
1332
- );
1124
+ save_ret = await db_module.save_app_couch_doc(await _common.get_project_app_id(req.app_id), doc);
1125
+ misc_module.rsync_drive_resources_to_remote_servers('workspace', app_id);
1333
1126
  break;
1334
1127
  }
1335
- case "user": {
1128
+ case 'user': {
1336
1129
  // /// check existing file
1337
1130
  // const user_drive_res = await db_module.find_couch_query("xuda_drive", {
1338
1131
  // selector: { docType: "user_drive", originalname, uid, stat: 3 },
@@ -1347,22 +1140,16 @@ exports.upload_drive_file = async function (req, job_id, headers, file_obj) {
1347
1140
  // }
1348
1141
 
1349
1142
  ref = uid;
1350
- file_name =
1351
- "drv_" + ref + "_" + (await _common.xuda_get_uuid(drive_type)) + ext;
1143
+ file_name = 'drv_' + ref + '_' + (await _common.xuda_get_uuid(drive_type)) + ext;
1352
1144
 
1353
1145
  doc.server_file_name = file_name;
1354
1146
 
1355
1147
  doc.bucket = {
1356
- [`${process.env.XUDA_HOSTNAME}`]: await upload_file_to_spaces(
1357
- tempPath,
1358
- ref,
1359
- drive_type,
1360
- file_name
1361
- ),
1148
+ [`${process.env.XUDA_HOSTNAME}`]: await upload_file_to_spaces(tempPath, ref, drive_type, file_name),
1362
1149
  };
1363
1150
  doc._id = file_name;
1364
- save_ret = await db_module.save_couch_doc("xuda_drive", doc);
1365
- misc_module.rsync_drive_resources_to_remote_servers("user", uid);
1151
+ save_ret = await db_module.save_couch_doc('xuda_drive', doc);
1152
+ misc_module.rsync_drive_resources_to_remote_servers('user', uid);
1366
1153
  break;
1367
1154
  }
1368
1155
 
@@ -1399,11 +1186,11 @@ exports.upload_drive_file = async function (req, job_id, headers, file_obj) {
1399
1186
  }
1400
1187
  };
1401
1188
  exports.check_drive_file = async function (req, job_id, headers, file_obj) {
1402
- const { type = "file", drive_type, app_id, uid, file_path } = req;
1189
+ const { type = 'file', drive_type, app_id, uid, file_path } = req;
1403
1190
 
1404
1191
  var master_app_id;
1405
1192
 
1406
- if (["studio", "workspace"].includes(drive_type)) {
1193
+ if (['studio', 'workspace'].includes(drive_type)) {
1407
1194
  master_app_id = await _common.get_project_app_id(app_id, true);
1408
1195
  }
1409
1196
 
@@ -1413,12 +1200,12 @@ exports.check_drive_file = async function (req, job_id, headers, file_obj) {
1413
1200
  stat: 3,
1414
1201
  };
1415
1202
 
1416
- if (type === "file") {
1203
+ if (type === 'file') {
1417
1204
  selector.originalname = path.basename(file_path);
1418
1205
  }
1419
1206
 
1420
1207
  switch (drive_type) {
1421
- case "studio": {
1208
+ case 'studio': {
1422
1209
  const target_dir = path.join(_conf[`studio_drive_path`], master_app_id);
1423
1210
 
1424
1211
  target_file = path.join(target_dir, file_path);
@@ -1426,14 +1213,14 @@ exports.check_drive_file = async function (req, job_id, headers, file_obj) {
1426
1213
  if (await fs_module.file_exists(target_file)) {
1427
1214
  return {
1428
1215
  code: -764,
1429
- data: "file exist",
1216
+ data: 'file exist',
1430
1217
  };
1431
1218
  }
1432
1219
 
1433
1220
  break;
1434
1221
  }
1435
- case "workspace": {
1436
- selector.docType = "workspace_drive";
1222
+ case 'workspace': {
1223
+ selector.docType = 'workspace_drive';
1437
1224
  const workspace_drive_res = await db_module.find_app_couch_query(app_id, {
1438
1225
  selector,
1439
1226
  limit: 99,
@@ -1441,23 +1228,23 @@ exports.check_drive_file = async function (req, job_id, headers, file_obj) {
1441
1228
  if (workspace_drive_res.docs.length) {
1442
1229
  return {
1443
1230
  code: -764,
1444
- data: "file exist",
1231
+ data: 'file exist',
1445
1232
  duplicates: workspace_drive_res.docs,
1446
1233
  };
1447
1234
  }
1448
1235
 
1449
1236
  break;
1450
1237
  }
1451
- case "user": {
1452
- selector.docType = "user_drive";
1453
- const user_drive_res = await db_module.find_couch_query("xuda_drive", {
1238
+ case 'user': {
1239
+ selector.docType = 'user_drive';
1240
+ const user_drive_res = await db_module.find_couch_query('xuda_drive', {
1454
1241
  selector,
1455
1242
  limit: 99,
1456
1243
  });
1457
1244
  if (user_drive_res.docs.length) {
1458
1245
  return {
1459
1246
  code: -764,
1460
- data: "file exist",
1247
+ data: 'file exist',
1461
1248
  duplicates: user_drive_res.docs,
1462
1249
  };
1463
1250
  }
@@ -1468,14 +1255,14 @@ exports.check_drive_file = async function (req, job_id, headers, file_obj) {
1468
1255
  default:
1469
1256
  return {
1470
1257
  code: -764,
1471
- data: "invalid method",
1258
+ data: 'invalid method',
1472
1259
  };
1473
1260
  break;
1474
1261
  }
1475
1262
 
1476
1263
  return {
1477
1264
  code: 764,
1478
- data: "ok",
1265
+ data: 'ok',
1479
1266
  };
1480
1267
  };
1481
1268
 
@@ -1483,13 +1270,9 @@ const ocr_drive_file = async function (app_id, doc) {
1483
1270
  const { drive_type, uid, file_path, originalname } = doc;
1484
1271
 
1485
1272
  // const _region = process.env.XUDA_HOSTNAME;
1486
- let target_file = get_file_url(
1487
- drive_type,
1488
- path.join(file_path, doc._id),
1489
- app_id || uid
1490
- ); // doc?.bucket?.[_region]?.Location;
1273
+ let target_file = get_file_url(drive_type, path.join(file_path, doc._id), app_id || uid); // doc?.bucket?.[_region]?.Location;
1491
1274
 
1492
- if (!target_file) throw "bucket not find";
1275
+ if (!target_file) throw 'bucket not find';
1493
1276
 
1494
1277
  function addQueryParam(urlString, key, value) {
1495
1278
  // Parse the URL
@@ -1502,36 +1285,22 @@ const ocr_drive_file = async function (app_id, doc) {
1502
1285
  return parsedUrl.toString();
1503
1286
  }
1504
1287
 
1505
- target_file = addQueryParam(
1506
- target_file,
1507
- "xuda_internal_request_code",
1508
- _conf.xuda_internal_request_code
1509
- );
1288
+ target_file = addQueryParam(target_file, 'xuda_internal_request_code', _conf.xuda_internal_request_code);
1510
1289
 
1511
1290
  try {
1512
1291
  doc.ocr_stat = 2;
1513
1292
  doc.ocr_stat_date = Date.now();
1514
1293
  let save_ret = await save_drive_doc(drive_type, app_id, doc, file_path);
1515
1294
 
1516
- doc = await get_drive_doc(
1517
- "file",
1518
- drive_type,
1519
- app_id,
1520
- uid,
1521
- path.join(file_path, originalname)
1522
- );
1295
+ doc = await get_drive_doc('file', drive_type, app_id, uid, path.join(file_path, originalname));
1523
1296
 
1524
- var ocr = "";
1297
+ var ocr = '';
1525
1298
 
1526
1299
  function detectLanguageByCountry(cfCountry) {
1527
1300
  const countryData = countryLanguage.getCountry(cfCountry);
1528
- let lang = "eng";
1301
+ let lang = 'eng';
1529
1302
  // Check if the country data is available
1530
- if (
1531
- countryData &&
1532
- countryData.languages &&
1533
- countryData.languages.length > 0
1534
- ) {
1303
+ if (countryData && countryData.languages && countryData.languages.length > 0) {
1535
1304
  // Return the first official language of the country
1536
1305
 
1537
1306
  for (let language of countryData.languages) {
@@ -1547,17 +1316,17 @@ const ocr_drive_file = async function (app_id, doc) {
1547
1316
 
1548
1317
  const generalConfig = { oem: 1, psm: 3, lang };
1549
1318
 
1550
- switch (doc.mime.split("/")[0]) {
1551
- case "image": {
1319
+ switch (doc.mime.split('/')[0]) {
1320
+ case 'image': {
1552
1321
  ocr = await tesseract.recognize(target_file, generalConfig);
1553
1322
 
1554
1323
  break;
1555
1324
  }
1556
1325
 
1557
- case "application": {
1558
- switch (doc.mime.split("/")[1]) {
1559
- case "pdf": {
1560
- const { convertPDF } = require("pdf2image");
1326
+ case 'application': {
1327
+ switch (doc.mime.split('/')[1]) {
1328
+ case 'pdf': {
1329
+ const { convertPDF } = require('pdf2image');
1561
1330
 
1562
1331
  const images = await convertPDF(target_file);
1563
1332
  for await (const imageData of images) {
@@ -1566,28 +1335,28 @@ const ocr_drive_file = async function (app_id, doc) {
1566
1335
  break;
1567
1336
  }
1568
1337
 
1569
- case "vnd.openxmlformats-officedocument.wordprocessingml.document": {
1570
- const mammoth = require("mammoth");
1338
+ case 'vnd.openxmlformats-officedocument.wordprocessingml.document': {
1339
+ const mammoth = require('mammoth');
1571
1340
  ocr = (await mammoth.extractRawText({ path: target_file })).value;
1572
1341
  break;
1573
1342
  }
1574
1343
 
1575
- case "nd.ms-excel":
1576
- case "vnd.openxmlformats-officedocument.spreadsheetml.sheet": {
1577
- const xlsx = require("node-xlsx").default;
1344
+ case 'nd.ms-excel':
1345
+ case 'vnd.openxmlformats-officedocument.spreadsheetml.sheet': {
1346
+ const xlsx = require('node-xlsx').default;
1578
1347
 
1579
1348
  const sheets = xlsx.parse(target_file);
1580
1349
  sheets.forEach((sheet) => {
1581
1350
  console.log(`Sheet: ${sheet.name}`);
1582
1351
  sheet.data.forEach((row) => {
1583
- ocr += row.join(" "); // Join all cells in the row
1352
+ ocr += row.join(' '); // Join all cells in the row
1584
1353
  });
1585
1354
  });
1586
1355
  break;
1587
1356
  }
1588
1357
 
1589
- case "vnd.ms-powerpoint":
1590
- case "vnd.openxmlformats-officedocument.presentationml.presentation":
1358
+ case 'vnd.ms-powerpoint':
1359
+ case 'vnd.openxmlformats-officedocument.presentationml.presentation':
1591
1360
  // tbd
1592
1361
  break;
1593
1362
 
@@ -1598,7 +1367,7 @@ const ocr_drive_file = async function (app_id, doc) {
1598
1367
  break;
1599
1368
  }
1600
1369
 
1601
- case "text": {
1370
+ case 'text': {
1602
1371
  ocr = (await fs_module.read_file(target_file)).data;
1603
1372
  break;
1604
1373
  }
@@ -1608,9 +1377,9 @@ const ocr_drive_file = async function (app_id, doc) {
1608
1377
  }
1609
1378
 
1610
1379
  doc.ocr = ocr
1611
- .replace(/<\/?[^>]+(>|$)/g, "")
1612
- .replace(/[\n]/g, " ")
1613
- .replace(/[\f\t]/g, "");
1380
+ .replace(/<\/?[^>]+(>|$)/g, '')
1381
+ .replace(/[\n]/g, ' ')
1382
+ .replace(/[\f\t]/g, '');
1614
1383
  doc.ocr_lang = lang;
1615
1384
  doc.ocr_stat = 3;
1616
1385
  } catch (err) {
@@ -1624,38 +1393,36 @@ const ocr_drive_file = async function (app_id, doc) {
1624
1393
  };
1625
1394
 
1626
1395
  const validate_drive_type = function (drive_type) {
1627
- if (!["studio", "workspace", "user"].includes(drive_type)) {
1628
- throw new Error(
1629
- "error - drive_type values allowed: studio, workspace or users"
1630
- );
1396
+ if (!['studio', 'workspace', 'user'].includes(drive_type)) {
1397
+ throw new Error('error - drive_type values allowed: studio, workspace or users');
1631
1398
  }
1632
1399
  };
1633
1400
  exports.run_drive_pending_ocr = async function (req) {
1634
1401
  try {
1635
1402
  var opt = {
1636
- selector: { docType: "user_drive", ocr_stat: 1, stat: 3 },
1403
+ selector: { docType: 'user_drive', ocr_stat: 1, stat: 3 },
1637
1404
  limit: 1,
1638
1405
  };
1639
1406
 
1640
- const user_drive_res = await db_module.find_couch_query("xuda_drive", opt);
1407
+ const user_drive_res = await db_module.find_couch_query('xuda_drive', opt);
1641
1408
  for await (let doc of user_drive_res.docs) {
1642
1409
  await ocr_drive_file(null, doc);
1643
1410
  }
1644
1411
 
1645
1412
  let selector = {
1646
- docType: "app",
1413
+ docType: 'app',
1647
1414
  app_status_code: {
1648
1415
  $eq: 3,
1649
1416
  },
1650
1417
  $or: [
1651
1418
  {
1652
- app_type: "master",
1419
+ app_type: 'master',
1653
1420
  },
1654
1421
  {
1655
- app_type: "instance",
1422
+ app_type: 'instance',
1656
1423
  },
1657
1424
  {
1658
- app_type: "datacenter",
1425
+ app_type: 'datacenter',
1659
1426
  },
1660
1427
  ],
1661
1428
  };
@@ -1665,9 +1432,9 @@ exports.run_drive_pending_ocr = async function (req) {
1665
1432
  limit: 99999,
1666
1433
  };
1667
1434
 
1668
- var app_res = await db_module.find_couch_query("xuda_master", app_opt);
1435
+ var app_res = await db_module.find_couch_query('xuda_master', app_opt);
1669
1436
 
1670
- opt.selector.docType = "workspace_drive";
1437
+ opt.selector.docType = 'workspace_drive';
1671
1438
  for (let app of app_res.docs) {
1672
1439
  const app_drive_res = await db_module.find_app_couch_query(app._id, opt);
1673
1440
  for await (let doc of app_drive_res.docs) {
@@ -1687,28 +1454,24 @@ exports.get_drive_size = async function (req) {
1687
1454
 
1688
1455
  let app_id_master, directoryPath;
1689
1456
  switch (drive_type) {
1690
- case "user":
1691
- directoryPath = "";
1457
+ case 'user':
1458
+ directoryPath = '';
1692
1459
 
1693
1460
  break;
1694
- case "workspace":
1695
- directoryPath = "";
1461
+ case 'workspace':
1462
+ directoryPath = '';
1696
1463
  app_id_master = await _common.get_project_app_id(app_id, true);
1697
1464
  break;
1698
1465
 
1699
1466
  default:
1700
1467
  app_id_master = await _common.get_project_app_id(app_id, true);
1701
- directoryPath = path.join(
1702
- _conf[`${drive_type}_drive_path`],
1703
- app_id_master,
1704
- "/"
1705
- );
1468
+ directoryPath = path.join(_conf[`${drive_type}_drive_path`], app_id_master, '/');
1706
1469
 
1707
1470
  break;
1708
1471
  }
1709
1472
 
1710
1473
  size = await get_folder_size(directoryPath, drive_type, app_id_master, uid);
1711
- if (drive_type === "workspace") {
1474
+ if (drive_type === 'workspace') {
1712
1475
  // calc datacenter
1713
1476
  size += await get_folder_size(directoryPath, drive_type, app_id);
1714
1477
  }
@@ -1719,18 +1482,11 @@ exports.get_drive_size = async function (req) {
1719
1482
  }
1720
1483
  };
1721
1484
 
1722
- const upload_file_to_spaces = async function (
1723
- tempPath,
1724
- ref,
1725
- drive_type,
1726
- file_name
1727
- ) {
1728
- const AWS = require("aws-sdk");
1485
+ const upload_file_to_spaces = async function (tempPath, ref, drive_type, file_name) {
1486
+ const AWS = require('aws-sdk');
1729
1487
 
1730
1488
  // Configure the AWS SDK to use DigitalOcean Spaces
1731
- const spacesEndpoint = new AWS.Endpoint(
1732
- _conf.storage_bucket[process.env.XUDA_HOSTNAME].endpoint
1733
- );
1489
+ const spacesEndpoint = new AWS.Endpoint(_conf.storage_bucket[process.env.XUDA_HOSTNAME].endpoint);
1734
1490
  const s3 = new AWS.S3({
1735
1491
  endpoint: spacesEndpoint,
1736
1492
  accessKeyId: _conf.digitalocean.spaces_access_key,
@@ -1738,14 +1494,14 @@ const upload_file_to_spaces = async function (
1738
1494
  region: _conf.storage_bucket[process.env.XUDA_HOSTNAME].region,
1739
1495
  });
1740
1496
 
1741
- const fs = require("fs");
1497
+ const fs = require('fs');
1742
1498
  const fileContent = fs.readFileSync(tempPath);
1743
1499
 
1744
1500
  const params = {
1745
1501
  Bucket: `${process.env.XUDA_HOSTNAME}/${drive_type}`, // Replace with your Space name
1746
- Key: path.join(ref, crypto.randomUUID() + "_" + file_name), // File name you want to save as in Spaces
1502
+ Key: path.join(ref, crypto.randomUUID() + '_' + file_name), // File name you want to save as in Spaces
1747
1503
  Body: fileContent,
1748
- ACL: "public-read", // Makes the file publicly accessible. Adjust the ACL according to your needs.
1504
+ ACL: 'public-read', // Makes the file publicly accessible. Adjust the ACL according to your needs.
1749
1505
  };
1750
1506
 
1751
1507
  try {
@@ -1753,20 +1509,14 @@ const upload_file_to_spaces = async function (
1753
1509
 
1754
1510
  return data;
1755
1511
  } catch (err) {
1756
- console.error("Error", err);
1512
+ console.error('Error', err);
1757
1513
  }
1758
1514
  };
1759
1515
 
1760
- exports.copy_file_to_another_bucket = async function (
1761
- source_region,
1762
- target_region,
1763
- file_name
1764
- ) {
1765
- const AWS = require("aws-sdk");
1516
+ exports.copy_file_to_another_bucket = async function (source_region, target_region, file_name) {
1517
+ const AWS = require('aws-sdk');
1766
1518
 
1767
- const sourceEndpoint = new AWS.Endpoint(
1768
- _conf.storage_bucket[source_region].endpoint
1769
- );
1519
+ const sourceEndpoint = new AWS.Endpoint(_conf.storage_bucket[source_region].endpoint);
1770
1520
  const sourceS3 = new AWS.S3({
1771
1521
  endpoint: sourceEndpoint,
1772
1522
  accessKeyId: _conf.digitalocean.spaces_access_key,
@@ -1774,9 +1524,7 @@ exports.copy_file_to_another_bucket = async function (
1774
1524
  region: _conf.storage_bucket[source_region].region,
1775
1525
  });
1776
1526
 
1777
- const targetEndpoint = new AWS.Endpoint(
1778
- _conf.storage_bucket[target_region].endpoint
1779
- );
1527
+ const targetEndpoint = new AWS.Endpoint(_conf.storage_bucket[target_region].endpoint);
1780
1528
  const targetS3 = new AWS.S3({
1781
1529
  endpoint: targetEndpoint,
1782
1530
  accessKeyId: _conf.digitalocean.spaces_access_key,
@@ -1799,7 +1547,7 @@ exports.copy_file_to_another_bucket = async function (
1799
1547
  Bucket: target_region,
1800
1548
  Key: file_name,
1801
1549
  Body,
1802
- ACL: "public-read",
1550
+ ACL: 'public-read',
1803
1551
  })
1804
1552
  .promise();
1805
1553
 
@@ -1810,7 +1558,7 @@ exports.copy_file_to_another_bucket = async function (
1810
1558
 
1811
1559
  return { code: 1, data: ret };
1812
1560
  } catch (err) {
1813
- console.error("Error copying file:", err);
1561
+ console.error('Error copying file:', err);
1814
1562
  return { code: -1, data: err.message };
1815
1563
  }
1816
1564
  };
@@ -1819,7 +1567,7 @@ const get_folder_size = async function (dir, drive_type, app_id, uid) {
1819
1567
  let totalSize = 0;
1820
1568
 
1821
1569
  const calculateSizeFs = async function (dirPath) {
1822
- const fs = require("fs").promises;
1570
+ const fs = require('fs').promises;
1823
1571
  const files = await fs.readdir(dirPath);
1824
1572
  for (let file of files) {
1825
1573
  const filePath = path.join(dirPath, file);
@@ -1837,28 +1585,28 @@ const get_folder_size = async function (dir, drive_type, app_id, uid) {
1837
1585
  selector: {
1838
1586
  docType: `${drive_type}_drive`,
1839
1587
  stat: 3,
1840
- file_path: path.join("/", dirPath),
1588
+ file_path: path.join('/', dirPath),
1841
1589
  },
1842
1590
  limit: 99999,
1843
1591
  };
1844
1592
 
1845
- if (drive_type === "user") {
1593
+ if (drive_type === 'user') {
1846
1594
  opt.selector.uid = uid;
1847
- ret = await db_module.find_couch_query("xuda_drive", opt);
1595
+ ret = await db_module.find_couch_query('xuda_drive', opt);
1848
1596
  } else {
1849
1597
  ret = await db_module.find_app_couch_query(app_id, opt);
1850
1598
  }
1851
1599
 
1852
1600
  for await (let doc of ret.docs) {
1853
- if (doc.type === "directory") {
1854
- await calculateSizeDocs(path.join("/", doc._id));
1601
+ if (doc.type === 'directory') {
1602
+ await calculateSizeDocs(path.join('/', doc._id));
1855
1603
  } else {
1856
1604
  totalSize += doc.size || 0;
1857
1605
  }
1858
1606
  }
1859
1607
  };
1860
1608
 
1861
- if (["user", "workspace"].includes(drive_type)) {
1609
+ if (['user', 'workspace'].includes(drive_type)) {
1862
1610
  await calculateSizeDocs(dir);
1863
1611
  } else {
1864
1612
  await calculateSizeFs(dir);
@@ -1870,20 +1618,18 @@ const get_file_info = async function (file_path) {
1870
1618
  var doc = {};
1871
1619
  doc.exif_ret = await fs_module.get_exif(file_path);
1872
1620
  try {
1873
- const sizeOf = require("image-size");
1621
+ const sizeOf = require('image-size');
1874
1622
  const dimensions = sizeOf(file_path);
1875
1623
  doc.dimensions = `${dimensions.width}x${dimensions.height}`;
1876
- } catch (error) { }
1624
+ } catch (error) {}
1877
1625
  return doc;
1878
1626
  };
1879
1627
 
1880
1628
  exports.delete_file_from_bucket = async function (bucket) {
1881
- const AWS = require("aws-sdk");
1629
+ const AWS = require('aws-sdk');
1882
1630
 
1883
1631
  // Configure the AWS SDK to use DigitalOcean Spaces
1884
- const spacesEndpoint = new AWS.Endpoint(
1885
- _conf.storage_bucket[process.env.XUDA_HOSTNAME].endpoint
1886
- );
1632
+ const spacesEndpoint = new AWS.Endpoint(_conf.storage_bucket[process.env.XUDA_HOSTNAME].endpoint);
1887
1633
  const s3 = new AWS.S3({
1888
1634
  endpoint: spacesEndpoint,
1889
1635
  accessKeyId: _conf.digitalocean.spaces_access_key,
@@ -1893,13 +1639,11 @@ exports.delete_file_from_bucket = async function (bucket) {
1893
1639
 
1894
1640
  try {
1895
1641
  for await (let [key, val] of Object.entries(bucket)) {
1896
- const data = await s3
1897
- .deleteObject({ Bucket: key, Key: val.key })
1898
- .promise();
1642
+ const data = await s3.deleteObject({ Bucket: key, Key: val.key }).promise();
1899
1643
  }
1900
1644
  // return data;
1901
1645
  } catch (err) {
1902
- console.error("Error", err);
1646
+ console.error('Error', err);
1903
1647
  }
1904
1648
  };
1905
1649
 
@@ -1910,17 +1654,14 @@ const get_folder_docs = async function (dir, drive_type, app_id) {
1910
1654
  selector: {
1911
1655
  docType: `${drive_type}_drive`,
1912
1656
  stat: 3,
1913
- file_path: path.join("/", dirPath),
1657
+ file_path: path.join('/', dirPath),
1914
1658
  },
1915
1659
  limit: 99999,
1916
1660
  };
1917
- const ret =
1918
- drive_type === "user"
1919
- ? await db_module.find_couch_query("xuda_drive", opt)
1920
- : await db_module.find_app_couch_query(app_id, opt);
1661
+ const ret = drive_type === 'user' ? await db_module.find_couch_query('xuda_drive', opt) : await db_module.find_app_couch_query(app_id, opt);
1921
1662
  for await (let doc of ret.docs) {
1922
- if (doc.type === "directory") {
1923
- await get_docs(path.join("/", doc._id));
1663
+ if (doc.type === 'directory') {
1664
+ await get_docs(path.join('/', doc._id));
1924
1665
  } else {
1925
1666
  docs.push(doc);
1926
1667
  }
@@ -1938,14 +1679,7 @@ exports.update_drive_file_tags = async function (req, job_id, headers) {
1938
1679
 
1939
1680
  validate_drive_type(drive_type);
1940
1681
 
1941
- let doc = await get_drive_doc(
1942
- type,
1943
- drive_type,
1944
- app_id,
1945
- uid,
1946
- file_path,
1947
- headers
1948
- );
1682
+ let doc = await get_drive_doc(type, drive_type, app_id, uid, file_path, headers);
1949
1683
 
1950
1684
  doc.date_modified = Date.now();
1951
1685
  doc.tags = tags;
@@ -1960,9 +1694,7 @@ exports.update_drive_file_tags = async function (req, job_id, headers) {
1960
1694
  };
1961
1695
 
1962
1696
  const get_file_url = function (drive_type, file_name, ref) {
1963
- let file_url = `https://${process.env.XUDA_HOSTNAME
1964
- }/${drive_type}-drive/${ref}${file_name?.substr(0, 1) !== "/" ? "/" : ""
1965
- }${file_name}`;
1697
+ let file_url = `https://${process.env.XUDA_HOSTNAME}/${drive_type}-drive/${ref}${file_name?.substr(0, 1) !== '/' ? '/' : ''}${file_name}`;
1966
1698
  // if (["workspace"].includes(drive_type)) {
1967
1699
  // file_url = `https://${process.env.XUDA_HOSTNAME}/workspace-drive${
1968
1700
  // file_name.substr(0, 1) !== "/" ? "/" : ""
@@ -1971,14 +1703,7 @@ const get_file_url = function (drive_type, file_name, ref) {
1971
1703
  return file_url;
1972
1704
  };
1973
1705
 
1974
- const get_drive_doc = async function (
1975
- type,
1976
- drive_type,
1977
- app_id,
1978
- uid,
1979
- file_path,
1980
- headers
1981
- ) {
1706
+ const get_drive_doc = async function (type, drive_type, app_id, uid, file_path, headers) {
1982
1707
  let check_req = {
1983
1708
  app_id,
1984
1709
  uid,
@@ -1986,40 +1711,28 @@ const get_drive_doc = async function (
1986
1711
  file_path,
1987
1712
  type,
1988
1713
  };
1989
- const check_existing_file_ret = await module.exports.check_drive_file(
1990
- check_req
1991
- );
1714
+ const check_existing_file_ret = await module.exports.check_drive_file(check_req);
1992
1715
 
1993
1716
  let doc_ret;
1994
1717
  let doc;
1995
1718
  switch (drive_type) {
1996
- case "studio": {
1719
+ case 'studio': {
1997
1720
  const app_id_reference = await _common.get_project_app_id(app_id);
1998
1721
  doc_ret = await db_module.get_app_couch_doc(app_id_reference, file_path);
1999
1722
  break;
2000
1723
  }
2001
- case "user":
2002
- doc_ret = await db_module.get_couch_doc(
2003
- "xuda_drive",
2004
- check_existing_file_ret.duplicates[0]._id
2005
- );
1724
+ case 'user':
1725
+ doc_ret = await db_module.get_couch_doc('xuda_drive', check_existing_file_ret.duplicates[0]._id);
2006
1726
  break;
2007
1727
 
2008
- case "workspace": {
1728
+ case 'workspace': {
2009
1729
  const app_id_reference = await _common.get_project_app_id(app_id);
2010
1730
  const app_id_master = await _common.get_project_app_id(app_id, true);
2011
1731
 
2012
- doc_ret = await db_module.get_app_couch_doc(
2013
- app_id_reference,
2014
- check_existing_file_ret.duplicates[0]._id
2015
- );
2016
-
2017
- if (
2018
- doc_ret?.data?.type === "file" &&
2019
- !doc_ret?.data?._id.includes(app_id) &&
2020
- !doc_ret?.data?._id.includes(app_id_master)
2021
- ) {
2022
- throw new Error("file_path unauthorized");
1732
+ doc_ret = await db_module.get_app_couch_doc(app_id_reference, check_existing_file_ret.duplicates[0]._id);
1733
+
1734
+ if (doc_ret?.data?.type === 'file' && !doc_ret?.data?._id.includes(app_id) && !doc_ret?.data?._id.includes(app_id_master)) {
1735
+ throw new Error('file_path unauthorized');
2023
1736
  }
2024
1737
 
2025
1738
  break;
@@ -2029,15 +1742,11 @@ const get_drive_doc = async function (
2029
1742
  }
2030
1743
 
2031
1744
  if (doc_ret.code < 0) {
2032
- if (drive_type === "studio") {
1745
+ if (drive_type === 'studio') {
2033
1746
  const app_id_reference = await _common.get_project_app_id(app_id);
2034
1747
  const file_name = path.basename(file_path);
2035
1748
  const ext = path.extname(file_name).toLowerCase();
2036
- const internal_file_path = path.join(
2037
- _conf.studio_drive_path,
2038
- app_id_reference,
2039
- file_path
2040
- );
1749
+ const internal_file_path = path.join(_conf.studio_drive_path, app_id_reference, file_path);
2041
1750
  const statsObj = fs.statSync(internal_file_path);
2042
1751
  doc = {
2043
1752
  _id: file_path,
@@ -2047,8 +1756,8 @@ const get_drive_doc = async function (
2047
1756
  originalname: file_name,
2048
1757
  file_path: file_path,
2049
1758
  date_created: new Date(statsObj.birthtime).valueOf(),
2050
- ip: headers?.["cf-connecting-ip"],
2051
- country: headers?.["cf-ipcountry"],
1759
+ ip: headers?.['cf-connecting-ip'],
1760
+ country: headers?.['cf-ipcountry'],
2052
1761
  file_ext: ext,
2053
1762
  is_archive: _conf.archive_drive_ext.includes(ext) ? true : false,
2054
1763
  drive_type,
@@ -2056,16 +1765,13 @@ const get_drive_doc = async function (
2056
1765
  app_id_reference: await _common.get_project_app_id(app_id, true),
2057
1766
  uid,
2058
1767
  };
2059
- const save_ret = await db_module.save_app_couch_doc(
2060
- app_id_reference,
2061
- doc
2062
- );
1768
+ const save_ret = await db_module.save_app_couch_doc(app_id_reference, doc);
2063
1769
  if (save_ret.code < 0) {
2064
- throw new Error("studio file doc save error");
1770
+ throw new Error('studio file doc save error');
2065
1771
  }
2066
1772
  doc._rev = save_ret.data.rev;
2067
1773
  } else {
2068
- throw new Error("file doc not found");
1774
+ throw new Error('file doc not found');
2069
1775
  }
2070
1776
  } else {
2071
1777
  doc = doc_ret.data;
@@ -2078,14 +1784,14 @@ const save_drive_doc = async function (drive_type, app_id, doc) {
2078
1784
  let save_ret;
2079
1785
 
2080
1786
  switch (drive_type) {
2081
- case "workspace":
2082
- case "studio":
1787
+ case 'workspace':
1788
+ case 'studio':
2083
1789
  const app_id_reference = await _common.get_project_app_id(app_id);
2084
1790
  save_ret = await db_module.save_app_couch_doc(app_id_reference, doc);
2085
1791
  break;
2086
1792
 
2087
- case "user":
2088
- save_ret = await db_module.save_couch_doc("xuda_drive", doc);
1793
+ case 'user':
1794
+ save_ret = await db_module.save_couch_doc('xuda_drive', doc);
2089
1795
  break;
2090
1796
 
2091
1797
  default:
@@ -2099,34 +1805,24 @@ const save_drive_doc = async function (drive_type, app_id, doc) {
2099
1805
  return save_ret;
2100
1806
  };
2101
1807
 
2102
- exports.compile_javascript_program_in_studio_drive = async function (
2103
- req,
2104
- job_id
2105
- ) {
1808
+ exports.compile_javascript_program_in_studio_drive = async function (req, job_id) {
2106
1809
  debugger;
2107
1810
 
2108
- const queryRegistry = require("query-registry");
1811
+ const queryRegistry = require('query-registry');
2109
1812
 
2110
1813
  const { app_id, doc } = req;
2111
1814
 
2112
1815
  try {
2113
- const studio_drive_script_folder = path.join(
2114
- _conf.studio_drive_path,
2115
- app_id,
2116
- "scripts"
2117
- );
1816
+ const studio_drive_script_folder = path.join(_conf.studio_drive_path, app_id, 'scripts');
2118
1817
  const fs_module = require(`${module_path}/fs_module`);
2119
1818
  if (!(await fs_module.file_exists(studio_drive_script_folder))) {
2120
1819
  await fs_module.mkdir(studio_drive_script_folder, 0o775);
2121
1820
  }
2122
1821
 
2123
- const studio_drive_script_program_folder = path.join(
2124
- studio_drive_script_folder,
2125
- doc._id
2126
- );
1822
+ const studio_drive_script_program_folder = path.join(studio_drive_script_folder, doc._id);
2127
1823
 
2128
- const src = path.join(studio_drive_script_program_folder, "src");
2129
- const dist = path.join(studio_drive_script_program_folder, "dist");
1824
+ const src = path.join(studio_drive_script_program_folder, 'src');
1825
+ const dist = path.join(studio_drive_script_program_folder, 'dist');
2130
1826
 
2131
1827
  if (!(await fs_module.file_exists(studio_drive_script_program_folder))) {
2132
1828
  await fs_module.mkdir(studio_drive_script_program_folder, 0o775);
@@ -2134,16 +1830,16 @@ exports.compile_javascript_program_in_studio_drive = async function (
2134
1830
  await fs_module.mkdir(dist, 0o775);
2135
1831
  }
2136
1832
 
2137
- const inputCode = doc.scriptData?.value || "";
1833
+ const inputCode = doc.scriptData?.value || '';
2138
1834
 
2139
1835
  function wrapCodeWithFunction(inputCode) {
2140
- const lines = inputCode.split("\n");
1836
+ const lines = inputCode.split('\n');
2141
1837
  const importLines = [];
2142
1838
  const codeLines = [];
2143
1839
 
2144
1840
  // Separate import statements from the rest of the code
2145
1841
  for (const line of lines) {
2146
- if (line.trim().startsWith("import ")) {
1842
+ if (line.trim().startsWith('import ')) {
2147
1843
  importLines.push(line);
2148
1844
  } else {
2149
1845
  codeLines.push(line);
@@ -2153,19 +1849,19 @@ exports.compile_javascript_program_in_studio_drive = async function (
2153
1849
  // Wrap the non-import lines with the function
2154
1850
  const wrappedCode = [
2155
1851
  ...importLines,
2156
- "",
2157
- "export default async function (params,xu, SESSION_ID, SESSION_OBJ, job_id) {",
1852
+ '',
1853
+ 'export default async function (params,xu, SESSION_ID, SESSION_OBJ, job_id) {',
2158
1854
  ...codeLines.map((line) => ` ${line}`), // Indent the code inside the function
2159
- "}",
1855
+ '}',
2160
1856
  ];
2161
1857
 
2162
1858
  // Join the lines back into a single string
2163
- return wrappedCode.join("\n");
1859
+ return wrappedCode.join('\n');
2164
1860
  }
2165
1861
 
2166
1862
  let content = wrapCodeWithFunction(inputCode);
2167
1863
  ///// insert the css
2168
- const index_css = path.join(dist, "index.css");
1864
+ const index_css = path.join(dist, 'index.css');
2169
1865
  if (await fs_module.file_exists(index_css)) {
2170
1866
  // const index_css_content = fs_module.read_file(index_css);
2171
1867
  content = ` export const css = true \n` + content;
@@ -2173,29 +1869,23 @@ exports.compile_javascript_program_in_studio_drive = async function (
2173
1869
 
2174
1870
  ////////////////////////
2175
1871
 
2176
- const index_js = path.join(src, "index.mjs");
1872
+ const index_js = path.join(src, 'index.mjs');
2177
1873
  await fs_module.save_file(index_js, content);
2178
1874
 
2179
1875
  ///////////////////////
2180
1876
 
2181
1877
  let packageJSON = {
2182
1878
  name: doc.properties?.menuName || doc._id,
2183
- version: "1.0.0",
2184
- description: "",
2185
- main: "index.js",
2186
- scripts: { dev: "vite", build: "vite build", preview: "vite preview" },
2187
- license: "ISC",
1879
+ version: '1.0.0',
1880
+ description: '',
1881
+ main: 'index.js',
1882
+ scripts: { dev: 'vite', build: 'vite build', preview: 'vite preview' },
1883
+ license: 'ISC',
2188
1884
  dependencies: {},
2189
- devDependencies: { vite: "^5.4.10" },
2190
- author:
2191
- doc?.studio_meta?.checkedInUserName ||
2192
- doc?.studio_meta?.createdByUid ||
2193
- "",
1885
+ devDependencies: { vite: '^5.4.10' },
1886
+ author: doc?.studio_meta?.checkedInUserName || doc?.studio_meta?.createdByUid || '',
2194
1887
  };
2195
- const package_json = path.join(
2196
- studio_drive_script_program_folder,
2197
- "package.json"
2198
- );
1888
+ const package_json = path.join(studio_drive_script_program_folder, 'package.json');
2199
1889
 
2200
1890
  /////////////////
2201
1891
 
@@ -2212,13 +1902,9 @@ exports.compile_javascript_program_in_studio_drive = async function (
2212
1902
  while ((match = regex.exec(fileContent)) !== null) {
2213
1903
  let dep = match[1];
2214
1904
  // Ignore relative imports and CSS files
2215
- if (
2216
- !dep.startsWith(".") &&
2217
- !dep.startsWith("/") &&
2218
- !dep.endsWith(".css")
2219
- ) {
1905
+ if (!dep.startsWith('.') && !dep.startsWith('/') && !dep.endsWith('.css')) {
2220
1906
  // handles "invalid package name: 'vue-grid-layout/dist/vue-grid-layout.umd.min.js'"
2221
- if (dep.includes("/")) dep = dep.split("/")[0]; // result vue-grid-layout
1907
+ if (dep.includes('/')) dep = dep.split('/')[0]; // result vue-grid-layout
2222
1908
 
2223
1909
  matches.push(dep);
2224
1910
  }
@@ -2242,25 +1928,17 @@ exports.compile_javascript_program_in_studio_drive = async function (
2242
1928
  }
2243
1929
  }
2244
1930
  // update versions from doc
2245
- for (const [key, val] of Object.entries(
2246
- doc?.scriptData?.dependencies || {}
2247
- )) {
1931
+ for (const [key, val] of Object.entries(doc?.scriptData?.dependencies || {})) {
2248
1932
  devDependencies[key] = val; // update version from doc
2249
1933
  }
2250
1934
 
2251
1935
  // Update package.json
2252
1936
  packageJSON.devDependencies = devDependencies;
2253
1937
 
2254
- await fs_module.save_file(
2255
- package_json,
2256
- JSON.stringify(packageJSON, null, 2)
2257
- );
1938
+ await fs_module.save_file(package_json, JSON.stringify(packageJSON, null, 2));
2258
1939
  /////////////////////////
2259
1940
 
2260
- const vite_config = path.join(
2261
- studio_drive_script_program_folder,
2262
- "vite.config.js"
2263
- );
1941
+ const vite_config = path.join(studio_drive_script_program_folder, 'vite.config.js');
2264
1942
 
2265
1943
  const code = `
2266
1944
 
@@ -2305,21 +1983,13 @@ export default {
2305
1983
 
2306
1984
  await fs_module.save_file(vite_config, code);
2307
1985
  //////////////////////
2308
- const _utils = require(path.join(
2309
- process.env.XUDA_HOME,
2310
- "common",
2311
- "xuda-cpi-utils.js"
2312
- ));
2313
- await _utils.run_ssh_script(
2314
- `npm i --prefix ${studio_drive_script_program_folder};`
2315
- );
2316
- const build_ret = await _utils.run_ssh_script(
2317
- `npm run build --prefix ${studio_drive_script_program_folder};`
2318
- );
1986
+ const _utils = require(path.join(process.env.XUDA_HOME, 'common', 'xuda-cpi-utils.js'));
1987
+ await _utils.run_ssh_script(`npm i --prefix ${studio_drive_script_program_folder};`);
1988
+ const build_ret = await _utils.run_ssh_script(`npm run build --prefix ${studio_drive_script_program_folder};`);
2319
1989
 
2320
1990
  //////////////////////
2321
1991
 
2322
- misc_module.rsync_drive_resources_to_remote_servers("studio", app_id);
1992
+ misc_module.rsync_drive_resources_to_remote_servers('studio', app_id);
2323
1993
  /////////////////////
2324
1994
  const vite_build_res = {
2325
1995
  code: 1,
@@ -2350,42 +2020,33 @@ export default {
2350
2020
  };
2351
2021
 
2352
2022
  exports.update_drive_addons = async function (req, job_id) {
2353
- const {
2354
- drive_type,
2355
- app_id,
2356
- uid,
2357
- ocr,
2358
- image_convertor,
2359
- video_ai,
2360
- ai_component_generator,
2361
- edit_image,
2362
- } = req;
2023
+ const { drive_type, app_id, uid, ocr, image_convertor, video_ai, ai_component_generator, edit_image } = req;
2363
2024
 
2364
2025
  const set_drive_addons = function (drive_addons) {
2365
- if (typeof ocr !== "undefined") {
2026
+ if (typeof ocr !== 'undefined') {
2366
2027
  drive_addons.ocr = ocr;
2367
2028
  }
2368
2029
 
2369
- if (typeof image_convertor !== "undefined") {
2030
+ if (typeof image_convertor !== 'undefined') {
2370
2031
  drive_addons.image_convertor = image_convertor;
2371
2032
  }
2372
2033
 
2373
- if (typeof video_ai !== "undefined") {
2034
+ if (typeof video_ai !== 'undefined') {
2374
2035
  drive_addons.video_ai = video_ai;
2375
2036
  }
2376
2037
 
2377
- if (typeof ai_component_generator !== "undefined") {
2038
+ if (typeof ai_component_generator !== 'undefined') {
2378
2039
  drive_addons.ai_component_generator = ocr;
2379
2040
  }
2380
2041
 
2381
- if (typeof edit_image !== "undefined") {
2042
+ if (typeof edit_image !== 'undefined') {
2382
2043
  drive_addons.edit_image = edit_image;
2383
2044
  }
2384
2045
  };
2385
2046
 
2386
2047
  let drive_addons = {};
2387
2048
  let save_ret;
2388
- if (drive_type === "workspace" || drive_type === "studio") {
2049
+ if (drive_type === 'workspace' || drive_type === 'studio') {
2389
2050
  const master_id = await _common.get_project_app_id(app_id, true);
2390
2051
  var app_obj_ret = await db_module.get_app_obj(master_id);
2391
2052
  if (app_obj_ret.data.drive_addons) {
@@ -2394,17 +2055,14 @@ exports.update_drive_addons = async function (req, job_id) {
2394
2055
  set_drive_addons(drive_addons);
2395
2056
  save_ret = await db_module.save_app_couch_doc2(master_id, app_obj_ret.data);
2396
2057
  }
2397
- if (drive_type === "user") {
2398
- let account_ret = await db_module.get_couch_doc("xuda_accounts", uid);
2058
+ if (drive_type === 'user') {
2059
+ let account_ret = await db_module.get_couch_doc('xuda_accounts', uid);
2399
2060
 
2400
2061
  if (account_ret.data.drive_addons) {
2401
2062
  drive_addons = account_ret.data.drive_addons;
2402
2063
  }
2403
2064
  set_drive_addons(drive_addons);
2404
- save_ret = await db_module.save_couch_doc(
2405
- "xuda_accounts",
2406
- account_ret.data
2407
- );
2065
+ save_ret = await db_module.save_couch_doc('xuda_accounts', account_ret.data);
2408
2066
  }
2409
2067
  return save_ret;
2410
2068
  };