@xuda.io/drive_module 1.1.1120 → 1.1.1122

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