@xuda.io/drive_module 1.1.1225 → 1.1.1227

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.
@@ -1,68 +1,43 @@
1
- const path = require('path');
2
- global._conf = require(path.join(process.env.XUDA_HOME, process.env.XUDA_CONFIG));
1
+ import { createRequire } from 'module';
2
+ import path from 'path';
3
+ import _ from 'lodash';
4
+ import { rimraf } from 'rimraf';
5
+ import fse from 'fs-extra';
6
+ import unzipper from 'unzipper';
7
+ import fs from 'fs';
8
+ import tesseract from 'node-tesseract-ocr';
9
+ import countryLanguage from 'country-language';
10
+ import url from 'url';
11
+ import AWS from 'aws-sdk';
12
+ import { convertPDF } from 'pdf2image';
13
+ import mammoth from 'mammoth';
14
+ import xlsx from 'node-xlsx';
15
+ import sizeOf from 'image-size';
16
+ import { getPackageManifest } from 'query-registry';
17
+
3
18
  console.log('Drive Module loaded...');
4
19
 
5
- const _common = require(path.join(process.env.XUDA_HOME, 'common', 'xuda_node_common.mjs'));
6
- const _utils = require(path.join(process.env.XUDA_HOME, 'common', 'xuda-cpi-utils.js'));
20
+ // Initialize require for dynamic paths
21
+ const require = createRequire(import.meta.url);
22
+
23
+ // Load Globals
24
+ global._conf = require(path.join(process.env.XUDA_HOME, process.env.XUDA_CONFIG));
7
25
 
8
- const _ = require('lodash');
26
+ const _common = require(path.join(process.env.XUDA_HOME, 'common', 'xuda_node_common.mjs'));
27
+ const _utils = require(path.join(process.env.XUDA_HOME, 'common', 'xuda-cpi-utils.mjs'));
9
28
 
10
29
  const module_path = path.join(process.env.XUDA_HOME, 'cpi') + (!_conf.is_debug ? '/node_modules/@xuda.io' : '');
11
30
  const fs_module = require(`${module_path}/fs_module`);
12
31
  const db_module = require(`${module_path}/db_module`);
13
32
  const misc_module = require(`${module_path}/misc_module`);
14
- ////////////////////////////////////////////
15
-
16
- const rimraf = require('rimraf');
17
- const fse = require('fs-extra');
18
- // const crypto = require('node:crypto');
19
- const unzipper = require('unzipper');
20
- const fs = require('fs');
21
- const tesseract = require('node-tesseract-ocr');
22
- const countryLanguage = require('country-language');
23
- const url = require('url');
24
33
 
25
34
  ////////////////////////////////////////////
26
35
 
27
- [
28
- 'get_drive_files',
29
- 'get_drive_file_info',
30
- 'delete_drive_files',
31
- 'extract_drive_file',
32
- 'upload_drive_file',
33
- 'update_drive_file',
34
- 'create_drive_folder',
35
- 'rename_drive_file',
36
- 'rename_drive_folder',
37
- 'update_drive_file_sharing_mode',
38
- 'delete_file_bulk',
39
- 'check_drive_file',
40
- 'update_drive_file_tags',
41
- 'update_drive_addons',
42
- ].forEach((method) => {
43
- ['workspace', 'studio', 'user'].forEach((type) => {
44
- module.exports[method + '_' + type] = async function (req, job_id, headers, file_obj) {
45
- req.drive_type = type;
46
-
47
- //// TBD
48
- // if (type === 'workspace' && !['get_drive_files', 'get_drive_file_info', 'check_drive_file'].includes(method)) {
49
- // const { data: app_obj } = await db_module.get_app_obj(req.app_id);
50
- // if (app_obj.app_type === 'datacenter') {
51
- // const deploy_module = require(`${module_path}/deploy_module`);
52
- // deploy_module.reset_deployment_cache(app_obj._id);
53
- // }
54
- // }
55
-
56
- return await module.exports[method](req, job_id, headers, file_obj);
57
- };
58
- });
59
- });
60
-
61
- const get_studio_path = function (raw_path, ref, file_name) {
36
+ const get_studio_path = (raw_path, ref, file_name) => {
62
37
  return raw_path.replaceAll(path.join(_conf.studio_drive_path, ref), '').replaceAll('/' + file_name, '') || '/';
63
38
  };
64
39
 
65
- exports.get_drive_files = async function (req) {
40
+ export const get_drive_files = async (req) => {
66
41
  try {
67
42
  const { from = 0, to = 99999, drive_type, app_id, uid, type, date, search_string, sort_by = 'name', sort_dir = 'desc' } = req;
68
43
 
@@ -83,7 +58,7 @@ exports.get_drive_files = async function (req) {
83
58
  }
84
59
 
85
60
  let sort = [];
86
- for (item of sort_by_obj[sort_by]) {
61
+ for (let item of sort_by_obj[sort_by]) {
87
62
  sort.push({ [item]: sort_dir });
88
63
  }
89
64
 
@@ -96,7 +71,6 @@ exports.get_drive_files = async function (req) {
96
71
  limit: to - from,
97
72
  skip: from,
98
73
  sort,
99
- // sort: [{ [sort_by_obj[sort_by]]: sort_dir }],
100
74
  };
101
75
 
102
76
  if (req.path) {
@@ -110,8 +84,8 @@ exports.get_drive_files = async function (req) {
110
84
  .filter((e) => e.includes(':'))
111
85
  .reduce(
112
86
  (ret, val) => {
113
- var tagKey = val.split(':')[0];
114
- var tagVal = val.split(':')[1];
87
+ let tagKey = val.split(':')[0];
88
+ let tagVal = val.split(':')[1];
115
89
 
116
90
  if (tagKey === 'tag') {
117
91
  tags.push(tagVal);
@@ -135,7 +109,7 @@ exports.get_drive_files = async function (req) {
135
109
  },
136
110
  );
137
111
 
138
- var new_search = search_string
112
+ let new_search = search_string
139
113
  .split(' ')
140
114
  .filter((e) => !e.includes(':'))
141
115
  .filter((e) => e)
@@ -196,7 +170,7 @@ exports.get_drive_files = async function (req) {
196
170
  else return (bytes / 1073741824).toFixed(2) + ' GB';
197
171
  }
198
172
 
199
- const fetch_drive_files = async function (ret, file_doc, app_id_reference, uid, datasource_id, domain = process.env.XUDA_HOSTNAME, app_obj) {
173
+ const fetch_drive_files = async (ret, file_doc, app_id_reference, uid, datasource_id, domain = process.env.XUDA_HOSTNAME, app_obj) => {
200
174
  let files = {
201
175
  path: req.path || '/',
202
176
  name: file_doc?.data?.folder_name || '/',
@@ -260,7 +234,7 @@ exports.get_drive_files = async function (req) {
260
234
  return files;
261
235
  };
262
236
 
263
- const get_workspace_files = async function () {
237
+ const get_workspace_files = async () => {
264
238
  const app_id_reference = await _common.get_project_app_id(app_id);
265
239
  opt.selector.docType = 'workspace_drive';
266
240
 
@@ -276,7 +250,7 @@ exports.get_drive_files = async function (req) {
276
250
  data: ret_fetch_drive_files,
277
251
  };
278
252
  };
279
- const get_user_files = async function () {
253
+ const get_user_files = async () => {
280
254
  opt.selector.docType = 'user_drive';
281
255
  opt.selector.uid = uid;
282
256
  const ret = await db_module.find_couch_query('xuda_drive', opt);
@@ -286,7 +260,7 @@ exports.get_drive_files = async function (req) {
286
260
  data: await fetch_drive_files(ret, file_doc, null, uid),
287
261
  };
288
262
  };
289
- const get_studio_files = async function () {
263
+ const get_studio_files = async () => {
290
264
  if (search_string) {
291
265
  const app_id_reference = await _common.get_project_app_id(app_id);
292
266
  opt.selector.docType = 'studio_drive';
@@ -302,11 +276,7 @@ exports.get_drive_files = async function (req) {
302
276
 
303
277
  const app_id_master = await _common.get_project_app_id(app_id, true);
304
278
 
305
- var directoryPath = path.join(_conf.studio_drive_path, app_id_master, req.path || '/');
306
- // var directoryPath =
307
- // req.path !== "/"
308
- // ? req.path || "/"
309
- // : path.join(_conf.studio_drive_path, app_id_master);
279
+ let directoryPath = path.join(_conf.studio_drive_path, app_id_master, req.path || '/');
310
280
 
311
281
  const options = {
312
282
  stat: true,
@@ -323,19 +293,19 @@ exports.get_drive_files = async function (req) {
323
293
 
324
294
  const dree_ret = await fs_module.dree_scan(directoryPath, options);
325
295
 
326
- var tree = dree_ret.data; // dree.scan(directoryPath, options);
296
+ let tree = dree_ret.data;
327
297
 
328
- const rep = async function (file_path, type) {
298
+ const rep = async (file_path, type) => {
329
299
  file_path = file_path.replace(`${_conf.studio_drive_path}/${app_id_master}/`, '');
330
300
 
331
301
  return `https://${process.env.XUDA_HOSTNAME}/studio-drive/${app_id_master}/${file_path}`;
332
302
  };
333
303
 
334
- const drill = async function (node) {
335
- var i = 0;
336
- var values_to_delete = [];
304
+ const drill = async (node) => {
305
+ let i = 0;
306
+ let values_to_delete = [];
337
307
 
338
- for await (var val of node) {
308
+ for await (let val of node) {
339
309
  i++;
340
310
  // filter by file type
341
311
  if (type && type.toLowerCase() !== val.type.toLowerCase()) {
@@ -370,16 +340,16 @@ exports.get_drive_files = async function (req) {
370
340
  val.sizeInBytes = await get_folder_size(internal_path, drive_type, app_id_master);
371
341
  }
372
342
  // supports file uploaded directly to the server
373
- val.access_link = await rep(val.path, 'studio'); //+ "?r=" + Date.now();
343
+ val.access_link = await rep(val.path, 'studio');
374
344
  }
375
345
  val.date_created = new Date(val.stat.birthtime).valueOf();
376
346
  delete val.stat;
377
347
 
378
- val.path = directory_path; // get_studio_path(val.path, app_id_master, val.relativePath);
379
- val.file_path = file_path; // path.join(val.path, val.name);
348
+ val.path = directory_path;
349
+ val.file_path = file_path;
380
350
  val.tags = ret?.data?.tags || [];
381
351
  }
382
- for await (var index of values_to_delete.reverse()) {
352
+ for await (let index of values_to_delete.reverse()) {
383
353
  node.splice(index, 1);
384
354
  }
385
355
  };
@@ -420,8 +390,6 @@ exports.get_drive_files = async function (req) {
420
390
  throw new Error('drive_type error');
421
391
  }
422
392
  } catch (err) {
423
- // return err.message;
424
-
425
393
  return {
426
394
  code: -1,
427
395
  data: err.message,
@@ -429,7 +397,7 @@ exports.get_drive_files = async function (req) {
429
397
  }
430
398
  };
431
399
 
432
- exports.get_drive_file_info = async function (req, job_id, headers) {
400
+ export const get_drive_file_info = async (req, job_id, headers) => {
433
401
  try {
434
402
  const { type, drive_type, uid, app_id, file_path } = req;
435
403
 
@@ -477,7 +445,7 @@ exports.get_drive_file_info = async function (req, job_id, headers) {
477
445
  }
478
446
  };
479
447
 
480
- exports.delete_drive_files = async function (req, job_id, headers) {
448
+ export const delete_drive_files = async (req, job_id, headers) => {
481
449
  try {
482
450
  const { drive_type, app_id, files_arr, uid } = req;
483
451
  validate_drive_type(drive_type);
@@ -492,12 +460,13 @@ exports.delete_drive_files = async function (req, job_id, headers) {
492
460
  for await (const file of files_arr) {
493
461
  let doc = await get_drive_doc('file', drive_type, app_id, uid, file.file_path, headers);
494
462
 
463
+ let save_ret;
495
464
  switch (drive_type) {
496
465
  case 'studio': {
497
466
  const app_id_reference = await _common.get_project_app_id(app_id);
498
467
  const target_dir = path.join(_conf[`studio_drive_path`], app_id_reference, file.file_path);
499
468
  console.log('target_dir', target_dir);
500
- const ret = rimraf.sync(target_dir); // delete the file
469
+ rimraf.sync(target_dir);
501
470
 
502
471
  if (doc.type === 'file') {
503
472
  doc.ts = Date.now();
@@ -514,16 +483,16 @@ exports.delete_drive_files = async function (req, job_id, headers) {
514
483
 
515
484
  if (doc.type === 'file') {
516
485
  if (doc.bucket) {
517
- await module.exports.delete_file_from_bucket(doc.bucket);
486
+ await delete_file_from_bucket(doc.bucket);
518
487
  } else {
519
488
  const { data: app_obj } = await db_module.get_app_obj(app_id);
520
489
  if (app_obj?.app_type === 'datacenter') {
521
490
  const host = `root@${app_obj.app_hosting_server.ip}`;
522
- await _utils.run_ssh_script(`ssh rm ${host}:/var/xuda/workspace_drive/${file_name}; `);
491
+ await _utils.run_ssh_script(`ssh rm ${host}:/var/xuda/workspace_drive/${doc.server_file_name}; `);
523
492
  } else if (app_obj?.is_deployment) {
524
493
  const { data: datacenter_obj } = await db_module.get_app_obj(app_obj.app_datacenter_id);
525
494
  const host = `root@${datacenter_obj.app_hosting_server.ip}`;
526
- await _utils.run_ssh_script(`ssh rm ${host}:/var/xuda/workspace_drive/${file_name}; `);
495
+ await _utils.run_ssh_script(`ssh rm ${host}:/var/xuda/workspace_drive/${doc.server_file_name}; `);
527
496
  }
528
497
  }
529
498
  }
@@ -537,7 +506,7 @@ exports.delete_drive_files = async function (req, job_id, headers) {
537
506
 
538
507
  case 'user': {
539
508
  if (doc.type === 'file') {
540
- await module.exports.delete_file_from_bucket(doc.bucket);
509
+ await delete_file_from_bucket(doc.bucket);
541
510
  }
542
511
  doc.ts = Date.now();
543
512
  doc.stat = 4;
@@ -550,20 +519,6 @@ exports.delete_drive_files = async function (req, job_id, headers) {
550
519
  }
551
520
  }
552
521
 
553
- // switch (drive_type) {
554
- // case 'workspace': {
555
- // const app_id_reference = await _common.get_project_app_id(app_id);
556
- // misc_module.rsync_drive_resources_to_remote_servers('workspace', app_id_reference);
557
- // break;
558
- // }
559
- // case 'user': {
560
- // misc_module.rsync_drive_resources_to_remote_servers('user', uid);
561
- // break;
562
- // }
563
- // default:
564
- // break;
565
- // }
566
-
567
522
  return {
568
523
  code: 200,
569
524
  data: req.files_arr,
@@ -573,7 +528,7 @@ exports.delete_drive_files = async function (req, job_id, headers) {
573
528
  }
574
529
  };
575
530
 
576
- exports.update_drive_file = async function (req, job_id, headers, file_obj) {
531
+ export const update_drive_file = async (req, job_id, headers, file_obj) => {
577
532
  try {
578
533
  const { drive_type, app_id, file_path, uid } = req;
579
534
  validate_drive_type(drive_type);
@@ -588,7 +543,7 @@ exports.update_drive_file = async function (req, job_id, headers, file_obj) {
588
543
 
589
544
  let doc = await get_drive_doc('file', drive_type, app_id, uid, file_path, headers);
590
545
 
591
- var target_file = file_path;
546
+ let target_file = file_path;
592
547
  const master_id = await _common.get_project_app_id(app_id, true);
593
548
  const tempPath = file_obj.path;
594
549
  if (drive_type === 'studio') {
@@ -602,8 +557,8 @@ exports.update_drive_file = async function (req, job_id, headers, file_obj) {
602
557
 
603
558
  doc.date_modified = Date.now();
604
559
 
605
- const fs = require('fs').promises;
606
- const stat = await fs.stat(tempPath);
560
+ const fsPromises = fs.promises;
561
+ const stat = await fsPromises.stat(tempPath);
607
562
  if (stat.isFile()) {
608
563
  doc.size = stat.size;
609
564
  }
@@ -639,14 +594,12 @@ exports.update_drive_file = async function (req, job_id, headers, file_obj) {
639
594
  }
640
595
 
641
596
  save_ret = await db_module.save_app_couch_doc(app_id_reference, doc);
642
- // misc_module.rsync_drive_resources_to_remote_servers('workspace', app_id_master);
643
597
  break;
644
598
  }
645
599
  case 'user': {
646
600
  ref = uid;
647
601
  doc.bucket[`${process.env.XUDA_HOSTNAME}`] = await upload_file_to_spaces(tempPath, uid, drive_type, file_name);
648
602
  save_ret = await db_module.save_couch_doc('xuda_drive', doc);
649
- // misc_module.rsync_drive_resources_to_remote_servers('user', doc.uid);
650
603
  break;
651
604
  }
652
605
  default:
@@ -662,7 +615,6 @@ exports.update_drive_file = async function (req, job_id, headers, file_obj) {
662
615
 
663
616
  let file_url = get_file_url(drive_type, file_name, ref);
664
617
 
665
- // misc_module.purge_cloudflare_cache(false, server_file_name);
666
618
  return {
667
619
  code: 760,
668
620
  data: {
@@ -676,17 +628,18 @@ exports.update_drive_file = async function (req, job_id, headers, file_obj) {
676
628
  }
677
629
  };
678
630
 
679
- exports.create_drive_folder = async function (req, job_id, headers) {
631
+ export const create_drive_folder = async (req, job_id, headers) => {
680
632
  try {
681
633
  const { drive_type, app_id, folder_name, uid } = req;
682
634
  validate_drive_type(drive_type);
683
635
 
684
- var folder_id;
685
- var app_id_master;
686
- var target_dir;
687
- var folder_exist;
636
+ let folder_id;
637
+ let app_id_master;
638
+ let target_dir;
639
+ let folder_exist;
640
+ let save_ret;
688
641
 
689
- var doc = {
642
+ let doc = {
690
643
  docType: `${drive_type}_drive`,
691
644
  stat: 3,
692
645
  folder_name,
@@ -723,15 +676,15 @@ exports.create_drive_folder = async function (req, job_id, headers) {
723
676
  }
724
677
 
725
678
  case 'workspace': {
726
- folder_id = 'drive_folder_' + _utils.UUID(); // crypto.randomUUID();
727
- app_id_reference = await _common.get_project_app_id(app_id);
679
+ folder_id = 'drive_folder_' + _utils.UUID();
680
+ let app_id_reference = await _common.get_project_app_id(app_id);
728
681
  doc._id = folder_id;
729
682
  save_ret = await db_module.save_app_couch_doc(app_id_reference, doc);
730
683
  break;
731
684
  }
732
685
 
733
686
  case 'user': {
734
- folder_id = 'drive_folder_' + _utils.UUID(); //crypto.randomUUID();
687
+ folder_id = 'drive_folder_' + _utils.UUID();
735
688
  doc._id = folder_id;
736
689
  save_ret = await db_module.save_couch_doc('xuda_drive', doc);
737
690
  break;
@@ -755,18 +708,20 @@ exports.create_drive_folder = async function (req, job_id, headers) {
755
708
  };
756
709
  }
757
710
  };
758
- exports.rename_drive_file = async function (req, job_id, headers) {
711
+
712
+ export const rename_drive_file = async (req, job_id, headers) => {
759
713
  try {
760
- const { drive_type, app_id, file_name_to, file_path } = req;
714
+ const { drive_type, app_id, file_name_to, file_path, uid } = req;
761
715
  validate_drive_type(drive_type);
762
716
 
763
717
  let doc = await get_drive_doc('file', drive_type, app_id, uid, file_path, headers);
764
718
 
765
719
  if (drive_type === 'studio') {
766
720
  await fs_module.rename(file_path, file_name_to);
721
+ let app_id_master = await _common.get_project_app_id(app_id, true);
767
722
 
768
723
  doc._delete = true;
769
- await db_module.save_app_couch_doc(app_id_master, doc_ret.data);
724
+ await db_module.save_app_couch_doc(app_id_master, doc);
770
725
 
771
726
  doc._id = file_name_to;
772
727
  delete doc._rev;
@@ -806,7 +761,8 @@ exports.rename_drive_file = async function (req, job_id, headers) {
806
761
  };
807
762
  }
808
763
  };
809
- exports.rename_drive_folder = async function (req) {
764
+
765
+ export const rename_drive_folder = async (req) => {
810
766
  try {
811
767
  const { drive_type, app_id, curr_path, new_path } = req;
812
768
  validate_drive_type(drive_type);
@@ -823,9 +779,9 @@ exports.rename_drive_folder = async function (req) {
823
779
  doc_ret = await db_module.get_app_couch_doc(app_id_reference, curr_path);
824
780
  await fs_module.rename(curr_path, new_path);
825
781
  doc_ret.data._delete = true;
826
- save_ret = await db_module.save_app_couch_doc(app_id_reference, doc_ret.data);
782
+ await db_module.save_app_couch_doc(app_id_reference, doc_ret.data);
827
783
 
828
- doc_ret.data._id = file_name_to;
784
+ doc_ret.data._id = new_path;
829
785
  delete doc_ret.data._rev;
830
786
 
831
787
  misc_module.rsync_drive_resources_to_remote_servers('studio', app_id_reference);
@@ -854,7 +810,7 @@ exports.rename_drive_folder = async function (req) {
854
810
  }
855
811
 
856
812
  if (doc_ret.code > -1) {
857
- var doc = doc_ret.data;
813
+ let doc = doc_ret.data;
858
814
  doc.date_modified = Date.now();
859
815
  doc.folder_name = new_path;
860
816
  let save_ret;
@@ -882,29 +838,12 @@ exports.rename_drive_folder = async function (req) {
882
838
  code: 200,
883
839
  data: new_path,
884
840
  };
885
-
886
- // if (drive_type === "studio" || drive_type === "workspace") {
887
- // const app_id_reference = await _common.get_project_app_id(app_id);
888
-
889
- // if (!new_path.includes(app_id_reference)) {
890
- // return {
891
- // code: -1,
892
- // data: "error - new_path unauthorized",
893
- // };
894
- // }
895
- // }
896
-
897
- // fse.renameSync(curr_path, new_path);
898
-
899
- return {
900
- code: 200,
901
- data: new_path,
902
- };
903
841
  } catch (err) {
904
842
  return err.message;
905
843
  }
906
844
  };
907
- exports.update_drive_file_sharing_mode = async function (req, job_id, headers) {
845
+
846
+ export const update_drive_file_sharing_mode = async (req, job_id, headers) => {
908
847
  try {
909
848
  const { type, drive_type, uid, app_id, file_path, is_public } = req;
910
849
 
@@ -921,8 +860,8 @@ exports.update_drive_file_sharing_mode = async function (req, job_id, headers) {
921
860
  }
922
861
  };
923
862
 
924
- exports.extract_drive_file = async function (req, job_id, headers) {
925
- const { app_id, file_path, uid } = req;
863
+ export const extract_drive_file = async (req, job_id, headers) => {
864
+ const { app_id, file_path, uid, drive_type } = req;
926
865
 
927
866
  const app_id_master = await _common.get_project_app_id(app_id, true);
928
867
  const app_id_reference = await _common.get_project_app_id(app_id);
@@ -935,7 +874,7 @@ exports.extract_drive_file = async function (req, job_id, headers) {
935
874
  throw new Error('error - invalid archive file');
936
875
  }
937
876
 
938
- if (!file_exists(req)) {
877
+ if (!file_exists(req.file_path)) {
939
878
  throw new Error('error - file not found');
940
879
  }
941
880
 
@@ -944,27 +883,25 @@ exports.extract_drive_file = async function (req, job_id, headers) {
944
883
 
945
884
  fs.createReadStream(file_path)
946
885
  .pipe(unzipper.Parse())
947
- .on('entry', async function (entry) {
886
+ .on('entry', async (entry) => {
948
887
  const fileName = entry.path;
949
888
  const type = entry.type;
950
- const size = entry.vars.uncompressedSize; // There is also compressedSize;
889
+ const size = entry.vars.uncompressedSize;
951
890
  const ext = path.extname(fileName).toLowerCase();
952
891
 
953
892
  if (fileName.startsWith('__MACOSX/')) {
954
893
  return;
955
894
  }
956
- // var file_id = path.join(path.dirname(file_path), fileName) + "1";
957
895
 
958
- var new_file_id = path.basename(fileName);
896
+ let new_file_id = path.basename(fileName);
959
897
 
960
898
  if (doc.drive_type !== 'studio') {
961
899
  new_file_id = 'drv_' + app_id_master + '_' + (await _common.xuda_get_uuid(doc.drive_type)) + ext;
962
900
  }
963
901
 
964
- // const folder_name = 'drive_folder_' + (doc.drive_type === 'studio' ? path.basename(fileName) : crypto.randomUUID());
965
902
  const folder_name = 'drive_folder_' + (doc.drive_type === 'studio' ? path.basename(fileName) : _utils.UUID());
966
903
 
967
- var dir = path.dirname(fileName);
904
+ let dir = path.dirname(fileName);
968
905
  if (dir && dir !== '.') {
969
906
  dir = 'drive_folder_' + dir;
970
907
  }
@@ -978,16 +915,13 @@ exports.extract_drive_file = async function (req, job_id, headers) {
978
915
  entry.pipe(fs.createWriteStream(extract_path));
979
916
  }
980
917
 
981
- // console.log(fileName, entry);
982
-
983
- var file_doc = {
918
+ let file_doc = {
984
919
  _id: file_doc_id,
985
920
 
986
- docType: `${doc.drive_type}_drive`, //=== "studio" ? "upload" : "drive",
921
+ docType: `${doc.drive_type}_drive`,
987
922
  stat: 3,
988
923
  server_file_name: file_doc_id,
989
924
  type: type.toLowerCase(),
990
- // originalname: type === "Directory" ? folder_name : new_file_id,
991
925
  uid: uid,
992
926
  app_id: app_id_reference,
993
927
  app_id_reference: app_id_master,
@@ -1028,14 +962,14 @@ exports.extract_drive_file = async function (req, job_id, headers) {
1028
962
  };
1029
963
  };
1030
964
 
1031
- const file_exists = async function (file_path) {
965
+ const file_exists = async (file_path) => {
1032
966
  return await fs_module.file_exists(file_path);
1033
967
  };
1034
- const create_directory = async function (file_path, permission) {
968
+ const create_directory = async (file_path, permission) => {
1035
969
  return await fs_module.mkdir(file_path, permission);
1036
970
  };
1037
971
 
1038
- exports.delete_file_bulk = async function (req) {
972
+ export const delete_file_bulk = async (req) => {
1039
973
  if (typeof req.server_files !== 'object') {
1040
974
  return {
1041
975
  code: -1,
@@ -1050,7 +984,7 @@ exports.delete_file_bulk = async function (req) {
1050
984
  return { code: 1, data: req.server_files };
1051
985
  };
1052
986
 
1053
- exports.upload_drive_file = async function (req, job_id, headers, file_obj) {
987
+ export const upload_drive_file = async (req, job_id, headers, file_obj) => {
1054
988
  try {
1055
989
  const { drive_type, app_id, uid, make_public, tags = '' } = req;
1056
990
 
@@ -1071,15 +1005,15 @@ exports.upload_drive_file = async function (req, job_id, headers, file_obj) {
1071
1005
 
1072
1006
  let ref, file_name, target_file, save_ret;
1073
1007
 
1074
- const fs = require('fs').promises;
1075
- const stat = await fs.stat(tempPath);
1008
+ const fsPromises = fs.promises;
1009
+ const stat = await fsPromises.stat(tempPath);
1076
1010
 
1077
1011
  let tags_arr = [];
1078
1012
  try {
1079
1013
  if (tags) tags_arr = tags.split(',');
1080
1014
  } catch (error) {}
1081
1015
  const ts = Date.now();
1082
- var doc = {
1016
+ let doc = {
1083
1017
  docType: `${drive_type}_drive`,
1084
1018
  stat: 3,
1085
1019
  originalname,
@@ -1109,43 +1043,23 @@ exports.upload_drive_file = async function (req, job_id, headers, file_obj) {
1109
1043
  file_path: path.join(doc.file_path, originalname),
1110
1044
  type: 'file',
1111
1045
  };
1112
- const check_existing_file_ret = await module.exports.check_drive_file(check_req);
1046
+ const check_existing_file_ret = await check_drive_file(check_req);
1113
1047
 
1114
1048
  if (check_existing_file_ret.code < 0) {
1115
- // const rename_file_name = async function (i) {
1116
- // if (i > 1000) {
1117
- // return { code: -1, data: 'too many retries renaming existing file' };
1118
- // }
1119
-
1120
- // let tmp_originalname = originalname + ` (${i})`;
1121
-
1122
- // // check_req.file_name = tmp_originalname;
1123
- // check_req.file_path = path.join(doc.file_path, tmp_originalname);
1124
-
1125
- // const re_check_res = await module.exports.check_drive_file(check_req);
1126
- // if (re_check_res.code < 0) {
1127
- // i++;
1128
- // return await rename_file_name(i);
1129
- // }
1130
- // return { code: 1, data: tmp_originalname };
1131
- // };
1132
-
1133
- const rename_file_name = async function (i) {
1049
+ const rename_file_name = async (i) => {
1134
1050
  if (i > 1000) {
1135
1051
  return { code: -1, data: 'too many retries renaming existing file' };
1136
1052
  }
1137
1053
 
1138
- // Extract filename parts
1139
1054
  const parsed = path.parse(originalname);
1140
- const baseName = parsed.name; // filename without extension
1141
- const extension = parsed.ext; // .webp, .jpg, etc.
1055
+ const baseName = parsed.name;
1056
+ const extension = parsed.ext;
1142
1057
 
1143
- // Insert (i) before the extension
1144
1058
  let tmp_originalname = `${baseName} (${i})${extension}`;
1145
1059
 
1146
1060
  check_req.file_path = path.join(doc.file_path, tmp_originalname);
1147
1061
 
1148
- const re_check_res = await module.exports.check_drive_file(check_req);
1062
+ const re_check_res = await check_drive_file(check_req);
1149
1063
  if (re_check_res.code < 0) {
1150
1064
  return await rename_file_name(i + 1);
1151
1065
  }
@@ -1163,7 +1077,6 @@ exports.upload_drive_file = async function (req, job_id, headers, file_obj) {
1163
1077
  case 'studio': {
1164
1078
  ref = await _common.get_project_app_id(req.app_id, true);
1165
1079
  const target_dir = path.join(_conf[`studio_drive_path`], ref);
1166
- // file_name = path.join(req.path, file_obj.originalname);
1167
1080
  file_name = path.join(req.path, doc.originalname);
1168
1081
 
1169
1082
  target_file = path.join(target_dir, file_name);
@@ -1191,11 +1104,9 @@ exports.upload_drive_file = async function (req, job_id, headers, file_obj) {
1191
1104
  break;
1192
1105
  }
1193
1106
  case 'workspace': {
1194
- debugger;
1195
1107
  ref = await _common.get_project_app_id(req.app_id, true);
1196
1108
  file_name = 'drv_' + ref + '_' + (await _common.xuda_get_uuid(drive_type)) + ext;
1197
1109
 
1198
- // keep the original filename to support migration
1199
1110
  if (file_obj.originalname.includes(`drv_${ref}`)) {
1200
1111
  file_name = file_obj.originalname;
1201
1112
  }
@@ -1205,7 +1116,6 @@ exports.upload_drive_file = async function (req, job_id, headers, file_obj) {
1205
1116
  doc.app_id = app_id;
1206
1117
  doc.app_id_reference = ref;
1207
1118
 
1208
- // if file uploaded from deployment or from datacenter then save to the datacenter server fs
1209
1119
  const { data: app_obj } = await db_module.get_app_obj(app_id);
1210
1120
  if (app_obj?.app_type === 'datacenter') {
1211
1121
  const host = `root@${app_obj.app_hosting_server.ip}`;
@@ -1221,23 +1131,9 @@ exports.upload_drive_file = async function (req, job_id, headers, file_obj) {
1221
1131
  }
1222
1132
  doc._id = file_name;
1223
1133
  save_ret = await db_module.save_app_couch_doc(await _common.get_project_app_id(req.app_id), doc);
1224
- // misc_module.rsync_drive_resources_to_remote_servers('workspace', app_id);
1225
1134
  break;
1226
1135
  }
1227
1136
  case 'user': {
1228
- // /// check existing file
1229
- // const user_drive_res = await db_module.find_couch_query("xuda_drive", {
1230
- // selector: { docType: "user_drive", originalname, uid, stat: 3 },
1231
- // limit: 99,
1232
- // });
1233
- // if (user_drive_res.docs.length) {
1234
- // return {
1235
- // code: -762,
1236
- // data: "file already exist",
1237
- // duplicates: user_drive_res.docs,
1238
- // };
1239
- // }
1240
-
1241
1137
  ref = uid;
1242
1138
  file_name = 'drv_' + ref + '_' + (await _common.xuda_get_uuid(drive_type)) + ext;
1243
1139
 
@@ -1248,7 +1144,6 @@ exports.upload_drive_file = async function (req, job_id, headers, file_obj) {
1248
1144
  };
1249
1145
  doc._id = file_name;
1250
1146
  save_ret = await db_module.save_couch_doc('xuda_drive', doc);
1251
- // misc_module.rsync_drive_resources_to_remote_servers('user', uid);
1252
1147
  break;
1253
1148
  }
1254
1149
 
@@ -1256,17 +1151,6 @@ exports.upload_drive_file = async function (req, job_id, headers, file_obj) {
1256
1151
  break;
1257
1152
  }
1258
1153
 
1259
- // let file_url = `https://${
1260
- // process.env.XUDA_HOSTNAME
1261
- // }/${drive_type}-drive/${ref}${
1262
- // file_name.substr(0, 1) !== "/" ? "/" : ""
1263
- // }${file_name}`;
1264
- // if (["workspace"].includes(req.drive_type)) {
1265
- // file_url = `https://${process.env.XUDA_HOSTNAME}/workspace-drive${
1266
- // file_name.substr(0, 1) !== "/" ? "/" : ""
1267
- // }${file_name}`;
1268
- // }
1269
-
1270
1154
  let file_url = get_file_url(drive_type, file_name, ref);
1271
1155
 
1272
1156
  await fs_module.unlink(tempPath);
@@ -1284,10 +1168,10 @@ exports.upload_drive_file = async function (req, job_id, headers, file_obj) {
1284
1168
  return { code: -400, data: err.message };
1285
1169
  }
1286
1170
  };
1287
- exports.check_drive_file = async function (req, job_id, headers, file_obj) {
1171
+ export const check_drive_file = async (req, job_id, headers, file_obj) => {
1288
1172
  const { type = 'file', drive_type, app_id, uid, file_path } = req;
1289
1173
 
1290
- var master_app_id;
1174
+ let master_app_id;
1291
1175
 
1292
1176
  if (['studio', 'workspace'].includes(drive_type)) {
1293
1177
  master_app_id = await _common.get_project_app_id(app_id, true);
@@ -1307,7 +1191,7 @@ exports.check_drive_file = async function (req, job_id, headers, file_obj) {
1307
1191
  case 'studio': {
1308
1192
  const target_dir = path.join(_conf[`studio_drive_path`], master_app_id);
1309
1193
 
1310
- target_file = path.join(target_dir, file_path);
1194
+ let target_file = path.join(target_dir, file_path);
1311
1195
 
1312
1196
  if (await fs_module.file_exists(target_file)) {
1313
1197
  return {
@@ -1356,7 +1240,6 @@ exports.check_drive_file = async function (req, job_id, headers, file_obj) {
1356
1240
  code: -764,
1357
1241
  data: 'invalid method',
1358
1242
  };
1359
- break;
1360
1243
  }
1361
1244
 
1362
1245
  return {
@@ -1365,49 +1248,37 @@ exports.check_drive_file = async function (req, job_id, headers, file_obj) {
1365
1248
  };
1366
1249
  };
1367
1250
 
1368
- const ocr_drive_file = async function (app_id, doc) {
1251
+ const ocr_drive_file = async (app_id, doc) => {
1369
1252
  const { drive_type, uid, file_path, originalname } = doc;
1370
1253
 
1371
- // const _region = process.env.XUDA_HOSTNAME;
1372
- let target_file = get_file_url(drive_type, path.join(file_path, doc._id), app_id || uid); // doc?.bucket?.[_region]?.Location;
1373
-
1374
- // if (!target_file) throw 'bucket not find';
1254
+ let target_file = get_file_url(drive_type, path.join(file_path, doc._id), app_id || uid);
1375
1255
 
1376
1256
  function addQueryParam(urlString, key, value) {
1377
- // Parse the URL
1378
1257
  const parsedUrl = new url.URL(urlString);
1379
-
1380
- // Set the query parameter
1381
1258
  parsedUrl.searchParams.set(key, value);
1382
-
1383
- // Return the updated URL
1384
1259
  return parsedUrl.toString();
1385
1260
  }
1386
1261
 
1387
1262
  target_file = addQueryParam(target_file, 'xuda_internal_request_code', _conf.xuda_internal_request_code);
1388
1263
 
1264
+ let save_ret;
1389
1265
  try {
1390
1266
  doc.ocr_stat = 2;
1391
1267
  doc.ocr_stat_date = Date.now();
1392
- let save_ret = await save_drive_doc(drive_type, app_id, doc, file_path);
1268
+ save_ret = await save_drive_doc(drive_type, app_id, doc, file_path);
1393
1269
 
1394
1270
  doc = await get_drive_doc('file', drive_type, app_id, uid, path.join(file_path, originalname));
1395
1271
 
1396
- var ocr = '';
1272
+ let ocr = '';
1397
1273
 
1398
1274
  function detectLanguageByCountry(cfCountry) {
1399
1275
  const countryData = countryLanguage.getCountry(cfCountry);
1400
1276
  let lang = 'eng';
1401
- // Check if the country data is available
1402
1277
  if (countryData && countryData.languages && countryData.languages.length > 0) {
1403
- // Return the first official language of the country
1404
-
1405
1278
  for (let language of countryData.languages) {
1406
1279
  lang += `+${language.iso639_2}`;
1407
1280
  }
1408
1281
  }
1409
-
1410
- // Default to English if no language found
1411
1282
  return lang;
1412
1283
  }
1413
1284
 
@@ -1425,8 +1296,6 @@ const ocr_drive_file = async function (app_id, doc) {
1425
1296
  case 'application': {
1426
1297
  switch (doc.mime.split('/')[1]) {
1427
1298
  case 'pdf': {
1428
- const { convertPDF } = require('pdf2image');
1429
-
1430
1299
  const images = await convertPDF(target_file);
1431
1300
  for await (const imageData of images) {
1432
1301
  ocr += await tesseract.recognize(imageData.path, generalConfig);
@@ -1435,20 +1304,17 @@ const ocr_drive_file = async function (app_id, doc) {
1435
1304
  }
1436
1305
 
1437
1306
  case 'vnd.openxmlformats-officedocument.wordprocessingml.document': {
1438
- const mammoth = require('mammoth');
1439
1307
  ocr = (await mammoth.extractRawText({ path: target_file })).value;
1440
1308
  break;
1441
1309
  }
1442
1310
 
1443
1311
  case 'nd.ms-excel':
1444
1312
  case 'vnd.openxmlformats-officedocument.spreadsheetml.sheet': {
1445
- const xlsx = require('node-xlsx').default;
1446
-
1447
1313
  const sheets = xlsx.parse(target_file);
1448
1314
  sheets.forEach((sheet) => {
1449
1315
  console.log(`Sheet: ${sheet.name}`);
1450
1316
  sheet.data.forEach((row) => {
1451
- ocr += row.join(' '); // Join all cells in the row
1317
+ ocr += row.join(' ');
1452
1318
  });
1453
1319
  });
1454
1320
  break;
@@ -1491,14 +1357,15 @@ const ocr_drive_file = async function (app_id, doc) {
1491
1357
  }
1492
1358
  };
1493
1359
 
1494
- const validate_drive_type = function (drive_type) {
1360
+ const validate_drive_type = (drive_type) => {
1495
1361
  if (!['studio', 'workspace', 'user'].includes(drive_type)) {
1496
1362
  throw new Error('error - drive_type values allowed: studio, workspace or users');
1497
1363
  }
1498
1364
  };
1499
- exports.run_drive_pending_ocr = async function (req) {
1365
+
1366
+ export const run_drive_pending_ocr = async (req) => {
1500
1367
  try {
1501
- var opt = {
1368
+ let opt = {
1502
1369
  selector: { docType: 'user_drive', ocr_stat: 1, stat: 3 },
1503
1370
  limit: 1,
1504
1371
  };
@@ -1531,7 +1398,7 @@ exports.run_drive_pending_ocr = async function (req) {
1531
1398
  limit: 99999,
1532
1399
  };
1533
1400
 
1534
- var app_res = await db_module.find_couch_query('xuda_master', app_opt);
1401
+ let app_res = await db_module.find_couch_query('xuda_master', app_opt);
1535
1402
 
1536
1403
  opt.selector.docType = 'workspace_drive';
1537
1404
  for (let app of app_res.docs) {
@@ -1545,11 +1412,10 @@ exports.run_drive_pending_ocr = async function (req) {
1545
1412
  }
1546
1413
  };
1547
1414
 
1548
- exports.get_drive_size = async function (req) {
1415
+ export const get_drive_size = async (req) => {
1549
1416
  try {
1550
1417
  const { drive_type, app_id, uid } = req;
1551
1418
  let size = 0;
1552
- // validate_drive_type(drive_type);
1553
1419
 
1554
1420
  let app_id_master, directoryPath;
1555
1421
  switch (drive_type) {
@@ -1581,10 +1447,7 @@ exports.get_drive_size = async function (req) {
1581
1447
  }
1582
1448
  };
1583
1449
 
1584
- const upload_file_to_spaces = async function (tempPath, ref, drive_type, file_name) {
1585
- const AWS = require('aws-sdk');
1586
-
1587
- // Configure the AWS SDK to use DigitalOcean Spaces
1450
+ const upload_file_to_spaces = async (tempPath, ref, drive_type, file_name) => {
1588
1451
  const spacesEndpoint = new AWS.Endpoint(_conf.storage_bucket[process.env.XUDA_HOSTNAME].endpoint);
1589
1452
  const s3 = new AWS.S3({
1590
1453
  endpoint: spacesEndpoint,
@@ -1593,15 +1456,13 @@ const upload_file_to_spaces = async function (tempPath, ref, drive_type, file_na
1593
1456
  region: _conf.storage_bucket[process.env.XUDA_HOSTNAME].region,
1594
1457
  });
1595
1458
 
1596
- const fs = require('fs');
1597
1459
  const fileContent = fs.readFileSync(tempPath);
1598
1460
 
1599
1461
  const params = {
1600
- Bucket: `${process.env.XUDA_HOSTNAME}/${drive_type}`, // Replace with your Space name
1601
- // Key: path.join(ref, crypto.randomUUID() + '_' + file_name), // File name you want to save as in Spaces
1602
- Key: path.join(ref, _utils.UUID() + '_' + file_name), // File name you want to save as in Spaces
1462
+ Bucket: `${process.env.XUDA_HOSTNAME}/${drive_type}`,
1463
+ Key: path.join(ref, _utils.UUID() + '_' + file_name),
1603
1464
  Body: fileContent,
1604
- ACL: 'public-read', // Makes the file publicly accessible. Adjust the ACL according to your needs.
1465
+ ACL: 'public-read',
1605
1466
  };
1606
1467
 
1607
1468
  try {
@@ -1613,9 +1474,7 @@ const upload_file_to_spaces = async function (tempPath, ref, drive_type, file_na
1613
1474
  }
1614
1475
  };
1615
1476
 
1616
- exports.copy_file_to_another_bucket = async function (source_region, target_region, file_name) {
1617
- const AWS = require('aws-sdk');
1618
-
1477
+ export const copy_file_to_another_bucket = async (source_region, target_region, file_name) => {
1619
1478
  const sourceEndpoint = new AWS.Endpoint(_conf.storage_bucket[source_region].endpoint);
1620
1479
  const sourceS3 = new AWS.S3({
1621
1480
  endpoint: sourceEndpoint,
@@ -1633,7 +1492,6 @@ exports.copy_file_to_another_bucket = async function (source_region, target_regi
1633
1492
  });
1634
1493
 
1635
1494
  try {
1636
- // Get the object from the source bucket
1637
1495
  const { Body } = await sourceS3
1638
1496
  .getObject({
1639
1497
  Bucket: source_region,
@@ -1641,8 +1499,7 @@ exports.copy_file_to_another_bucket = async function (source_region, target_regi
1641
1499
  })
1642
1500
  .promise();
1643
1501
 
1644
- // Upload the object to the destination bucket
1645
- var ret = await targetS3
1502
+ let ret = await targetS3
1646
1503
  .putObject({
1647
1504
  Bucket: target_region,
1648
1505
  Key: file_name,
@@ -1663,15 +1520,15 @@ exports.copy_file_to_another_bucket = async function (source_region, target_regi
1663
1520
  }
1664
1521
  };
1665
1522
 
1666
- const get_folder_size = async function (dir, drive_type, app_id, uid) {
1523
+ const get_folder_size = async (dir, drive_type, app_id, uid) => {
1667
1524
  let totalSize = 0;
1668
1525
 
1669
- const calculateSizeFs = async function (dirPath) {
1670
- const fs = require('fs').promises;
1671
- const files = await fs.readdir(dirPath);
1526
+ const calculateSizeFs = async (dirPath) => {
1527
+ const fsPromises = fs.promises;
1528
+ const files = await fsPromises.readdir(dirPath);
1672
1529
  for (let file of files) {
1673
1530
  const filePath = path.join(dirPath, file);
1674
- const stat = await fs.stat(filePath);
1531
+ const stat = await fsPromises.stat(filePath);
1675
1532
  if (stat.isFile()) {
1676
1533
  totalSize += stat.size;
1677
1534
  } else if (stat.isDirectory()) {
@@ -1680,7 +1537,7 @@ const get_folder_size = async function (dir, drive_type, app_id, uid) {
1680
1537
  }
1681
1538
  };
1682
1539
 
1683
- const calculateSizeDocs = async function (dirPath) {
1540
+ const calculateSizeDocs = async (dirPath) => {
1684
1541
  let opt = {
1685
1542
  selector: {
1686
1543
  docType: `${drive_type}_drive`,
@@ -1689,7 +1546,7 @@ const get_folder_size = async function (dir, drive_type, app_id, uid) {
1689
1546
  },
1690
1547
  limit: 99999,
1691
1548
  };
1692
-
1549
+ let ret;
1693
1550
  if (drive_type === 'user') {
1694
1551
  opt.selector.uid = uid;
1695
1552
  ret = await db_module.find_couch_query('xuda_drive', opt);
@@ -1714,21 +1571,17 @@ const get_folder_size = async function (dir, drive_type, app_id, uid) {
1714
1571
  return totalSize;
1715
1572
  };
1716
1573
 
1717
- const get_file_info = async function (file_path) {
1718
- var doc = {};
1574
+ const get_file_info = async (file_path) => {
1575
+ let doc = {};
1719
1576
  doc.exif_ret = await fs_module.get_exif(file_path);
1720
1577
  try {
1721
- const sizeOf = require('image-size');
1722
1578
  const dimensions = sizeOf(file_path);
1723
1579
  doc.dimensions = `${dimensions.width}x${dimensions.height}`;
1724
1580
  } catch (error) {}
1725
1581
  return doc;
1726
1582
  };
1727
1583
 
1728
- exports.delete_file_from_bucket = async function (bucket) {
1729
- const AWS = require('aws-sdk');
1730
-
1731
- // Configure the AWS SDK to use DigitalOcean Spaces
1584
+ export const delete_file_from_bucket = async (bucket) => {
1732
1585
  const spacesEndpoint = new AWS.Endpoint(_conf.storage_bucket[process.env.XUDA_HOSTNAME].endpoint);
1733
1586
  const s3 = new AWS.S3({
1734
1587
  endpoint: spacesEndpoint,
@@ -1741,39 +1594,12 @@ exports.delete_file_from_bucket = async function (bucket) {
1741
1594
  for await (let [key, val] of Object.entries(bucket)) {
1742
1595
  const data = await s3.deleteObject({ Bucket: key, Key: val.key }).promise();
1743
1596
  }
1744
- // return data;
1745
1597
  } catch (err) {
1746
1598
  console.error('Error', err);
1747
1599
  }
1748
1600
  };
1749
1601
 
1750
- // const get_folder_docs = async function (dir, drive_type, app_id) {
1751
- // var docs = [];
1752
- // const get_docs = async function (dirPath) {
1753
- // const opt = {
1754
- // selector: {
1755
- // docType: `${drive_type}_drive`,
1756
- // stat: 3,
1757
- // file_path: path.join('/', dirPath),
1758
- // },
1759
- // limit: 99999,
1760
- // };
1761
- // const ret = drive_type === 'user' ? await db_module.find_couch_query('xuda_drive', opt) : await db_module.find_app_couch_query(app_id, opt);
1762
- // for await (let doc of ret.docs) {
1763
- // if (doc.type === 'directory') {
1764
- // await get_docs(path.join('/', doc._id));
1765
- // } else {
1766
- // docs.push(doc);
1767
- // }
1768
- // }
1769
- // };
1770
-
1771
- // await get_docs(dir);
1772
-
1773
- // return docs;
1774
- // };
1775
-
1776
- exports.update_drive_file_tags = async function (req, job_id, headers) {
1602
+ export const update_drive_file_tags = async (req, job_id, headers) => {
1777
1603
  try {
1778
1604
  const { type, drive_type, app_id, uid, file_path, tags = [] } = req;
1779
1605
 
@@ -1793,17 +1619,12 @@ exports.update_drive_file_tags = async function (req, job_id, headers) {
1793
1619
  }
1794
1620
  };
1795
1621
 
1796
- const get_file_url = function (drive_type, file_name, ref) {
1622
+ const get_file_url = (drive_type, file_name, ref) => {
1797
1623
  let file_url = `https://${process.env.XUDA_HOSTNAME}/${drive_type}-drive/${ref}${file_name?.substr(0, 1) !== '/' ? '/' : ''}${file_name}`;
1798
- // if (["workspace"].includes(drive_type)) {
1799
- // file_url = `https://${process.env.XUDA_HOSTNAME}/workspace-drive${
1800
- // file_name.substr(0, 1) !== "/" ? "/" : ""
1801
- // }${file_name}`;
1802
- // }
1803
1624
  return file_url;
1804
1625
  };
1805
1626
 
1806
- const get_drive_doc = async function (type, drive_type, app_id, uid, file_path, headers) {
1627
+ const get_drive_doc = async (type, drive_type, app_id, uid, file_path, headers) => {
1807
1628
  let check_req = {
1808
1629
  app_id,
1809
1630
  uid,
@@ -1811,7 +1632,7 @@ const get_drive_doc = async function (type, drive_type, app_id, uid, file_path,
1811
1632
  file_path,
1812
1633
  type,
1813
1634
  };
1814
- const check_existing_file_ret = await module.exports.check_drive_file(check_req);
1635
+ const check_existing_file_ret = await check_drive_file(check_req);
1815
1636
 
1816
1637
  let doc_ret;
1817
1638
  let doc;
@@ -1881,7 +1702,7 @@ const get_drive_doc = async function (type, drive_type, app_id, uid, file_path,
1881
1702
  return doc;
1882
1703
  };
1883
1704
 
1884
- const save_drive_doc = async function (drive_type, app_id, doc) {
1705
+ const save_drive_doc = async (drive_type, app_id, doc) => {
1885
1706
  let save_ret;
1886
1707
 
1887
1708
  switch (drive_type) {
@@ -1906,10 +1727,8 @@ const save_drive_doc = async function (drive_type, app_id, doc) {
1906
1727
  return save_ret;
1907
1728
  };
1908
1729
 
1909
- exports.compile_javascript_program_in_studio_drive = async function (req, job_id) {
1910
- debugger;
1911
-
1912
- const queryRegistry = require('query-registry');
1730
+ export const compile_javascript_program_in_studio_drive = async (req, job_id) => {
1731
+ // debugger;
1913
1732
 
1914
1733
  const { app_id, doc } = req;
1915
1734
 
@@ -1938,7 +1757,6 @@ exports.compile_javascript_program_in_studio_drive = async function (req, job_id
1938
1757
  const importLines = [];
1939
1758
  const codeLines = [];
1940
1759
 
1941
- // Separate import statements from the rest of the code
1942
1760
  for (const line of lines) {
1943
1761
  if (line.trim().startsWith('import ')) {
1944
1762
  importLines.push(line);
@@ -1947,34 +1765,20 @@ exports.compile_javascript_program_in_studio_drive = async function (req, job_id
1947
1765
  }
1948
1766
  }
1949
1767
 
1950
- // Wrap the non-import lines with the function
1951
- const wrappedCode = [
1952
- ...importLines,
1953
- '',
1954
- 'export default async function (params,xu, SESSION_ID, SESSION_OBJ, job_id) {',
1955
- ...codeLines.map((line) => ` ${line}`), // Indent the code inside the function
1956
- '}',
1957
- ];
1768
+ const wrappedCode = [...importLines, '', 'export default async function (params,xu, SESSION_ID, SESSION_OBJ, job_id) {', ...codeLines.map((line) => ` ${line}`), '}'];
1958
1769
 
1959
- // Join the lines back into a single string
1960
1770
  return wrappedCode.join('\n');
1961
1771
  }
1962
1772
 
1963
1773
  let content = wrapCodeWithFunction(inputCode);
1964
- ///// insert the css
1965
1774
  const index_css = path.join(dist, 'index.css');
1966
1775
  if (await fs_module.file_exists(index_css)) {
1967
- // const index_css_content = fs_module.read_file(index_css);
1968
1776
  content = ` export const css = true \n` + content;
1969
1777
  }
1970
1778
 
1971
- ////////////////////////
1972
-
1973
1779
  const index_js = path.join(src, 'index.mjs');
1974
1780
  await fs_module.save_file(index_js, content);
1975
1781
 
1976
- ///////////////////////
1977
-
1978
1782
  let packageJSON = {
1979
1783
  name: doc.properties?.menuName || doc._id,
1980
1784
  version: '1.0.0',
@@ -1988,9 +1792,6 @@ exports.compile_javascript_program_in_studio_drive = async function (req, job_id
1988
1792
  };
1989
1793
  const package_json = path.join(studio_drive_script_program_folder, 'package.json');
1990
1794
 
1991
- /////////////////
1992
-
1993
- // Match `import` and `require` statements
1994
1795
  const importRegex = /import\s+(?:.*?from\s+)?['"](.+?)['"]/g;
1995
1796
  const requireRegex = /require\(['"](.+?)['"]\)/g;
1996
1797
 
@@ -2002,10 +1803,8 @@ exports.compile_javascript_program_in_studio_drive = async function (req, job_id
2002
1803
  let match;
2003
1804
  while ((match = regex.exec(fileContent)) !== null) {
2004
1805
  let dep = match[1];
2005
- // Ignore relative imports and CSS files
2006
1806
  if (!dep.startsWith('.') && !dep.startsWith('/') && !dep.endsWith('.css')) {
2007
- // handles "invalid package name: 'vue-grid-layout/dist/vue-grid-layout.umd.min.js'"
2008
- if (dep.includes('/')) dep = dep.split('/')[0]; // result vue-grid-layout
1807
+ if (dep.includes('/')) dep = dep.split('/')[0];
2009
1808
 
2010
1809
  matches.push(dep);
2011
1810
  }
@@ -2017,32 +1816,28 @@ exports.compile_javascript_program_in_studio_drive = async function (req, job_id
2017
1816
  const requires = extractDependencies(content, requireRegex);
2018
1817
  imports.concat(requires).forEach((dep) => allDependencies.add(dep));
2019
1818
 
2020
- // Add missing dependencies to package.json
2021
1819
  for await (const dep of allDependencies) {
2022
1820
  if (!devDependencies[dep]) {
2023
1821
  console.log(`Adding dependency: ${dep}`);
2024
- const npm_ret = await queryRegistry.getPackageManifest({
1822
+ const npm_ret = await getPackageManifest({
2025
1823
  name: dep,
2026
1824
  });
2027
1825
 
2028
- devDependencies[dep] = npm_ret.version; // Use "latest" as the version or fetch actual versions if needed
1826
+ devDependencies[dep] = npm_ret.version;
2029
1827
  }
2030
1828
  }
2031
- // update versions from doc
2032
1829
  for (const [key, val] of Object.entries(doc?.scriptData?.dependencies || {})) {
2033
- devDependencies[key] = val; // update version from doc
1830
+ devDependencies[key] = val;
2034
1831
  }
2035
1832
 
2036
- // Update package.json
2037
1833
  packageJSON.devDependencies = devDependencies;
2038
1834
 
2039
1835
  await fs_module.save_file(package_json, JSON.stringify(packageJSON, null, 2));
2040
- /////////////////////////
2041
1836
 
2042
1837
  const vite_config = path.join(studio_drive_script_program_folder, 'vite.config.js');
2043
1838
 
2044
1839
  const code = `
2045
-
1840
+
2046
1841
  export default {
2047
1842
  build: {
2048
1843
  lib: {
@@ -2066,7 +1861,7 @@ export default {
2066
1861
  }
2067
1862
  return "[name]-[hash][extname]";
2068
1863
  },
2069
-
1864
+
2070
1865
 
2071
1866
  },
2072
1867
  },
@@ -2083,15 +1878,11 @@ export default {
2083
1878
  `;
2084
1879
 
2085
1880
  await fs_module.save_file(vite_config, code);
2086
- //////////////////////
2087
1881
 
2088
1882
  await _utils.run_ssh_script(`npm i --prefix ${studio_drive_script_program_folder};`);
2089
1883
  const build_ret = await _utils.run_ssh_script(`npm run build --prefix ${studio_drive_script_program_folder};`);
2090
1884
 
2091
- //////////////////////
2092
-
2093
1885
  misc_module.rsync_drive_resources_to_remote_servers('studio', app_id);
2094
- /////////////////////
2095
1886
  const vite_build_res = {
2096
1887
  code: 1,
2097
1888
  data: {
@@ -2120,10 +1911,10 @@ export default {
2120
1911
  }
2121
1912
  };
2122
1913
 
2123
- exports.update_drive_addons = async function (req, job_id) {
1914
+ export const update_drive_addons = async (req, job_id) => {
2124
1915
  const { drive_type, app_id, uid, ocr, image_convertor, video_ai, ai_component_generator, edit_image } = req;
2125
1916
 
2126
- const set_drive_addons = function (drive_addons) {
1917
+ const set_drive_addons = (drive_addons) => {
2127
1918
  if (typeof ocr !== 'undefined') {
2128
1919
  drive_addons.ocr = ocr;
2129
1920
  }
@@ -2149,7 +1940,7 @@ exports.update_drive_addons = async function (req, job_id) {
2149
1940
  let save_ret;
2150
1941
  if (drive_type === 'workspace' || drive_type === 'studio') {
2151
1942
  const master_id = await _common.get_project_app_id(app_id, true);
2152
- var app_obj_ret = await db_module.get_app_obj(master_id);
1943
+ let app_obj_ret = await db_module.get_app_obj(master_id);
2153
1944
  if (app_obj_ret.data.drive_addons) {
2154
1945
  drive_addons = app_obj_ret.data.drive_addons;
2155
1946
  }
@@ -2168,40 +1959,218 @@ exports.update_drive_addons = async function (req, job_id) {
2168
1959
  return save_ret;
2169
1960
  };
2170
1961
 
2171
- setTimeout(async function () {
2172
- // console.log(
2173
- // await exports.search_drive_files({
2174
- // app_id: "prj3937cb6f9a31c8c7dea25055bba845b1",
2175
- // drive_type: "workspace",
2176
- // search_string: "tag:mediucal tag:school name:dw name:dawdddaw d",
2177
- // })
2178
- // );
2179
- // module.exports.run_drive_pending_ocr();
2180
- // fix_date_modified();
2181
- }, 2000);
2182
-
2183
- // const fix_date_modified = async function () {
2184
- // const app_id = 'dtcb1674f63e47ddcb11865cefdfb4483fb';
2185
- // const ret = await db_module.find_app_couch_query(app_id, { selector: { docType: 'workspace_drive' }, limit: 99999 });
2186
-
2187
- // for (doc of ret.docs) {
2188
- // if (!doc.date_modified) {
2189
- // doc.date_modified = doc.date_created;
2190
- // }
2191
- // }
2192
-
2193
- // const save_ret = await db_module.sava_app_couch_bulk_docs(app_id, ret.docs);
2194
- // console.log('fix_date_modified', save_ret, ret);
2195
-
2196
- // // const db_name = 'db_prj_3uh4q1zj0llw3mj4wgifh1';
2197
- // // const ret = await db_module.find_couch_query(db_name, { selector: { docType: 'workspace_drive' } });
2198
-
2199
- // // for (doc of ret.docs) {
2200
- // // if (!doc.date_modified) {
2201
- // // doc.date_modified = 0;
2202
- // // }
2203
- // // }
2204
-
2205
- // // const save_ret = await db_module.save_couch_bulk_docs(db_name, ret.docs);
2206
- // // console.log('fix_date_modified', save_ret, ret);
2207
- // };
1962
+ // Meta-programming to export functions dynamically
1963
+ const methods = [
1964
+ 'get_drive_files',
1965
+ 'get_drive_file_info',
1966
+ 'delete_drive_files',
1967
+ 'extract_drive_file',
1968
+ 'upload_drive_file',
1969
+ 'update_drive_file',
1970
+ 'create_drive_folder',
1971
+ 'rename_drive_file',
1972
+ 'rename_drive_folder',
1973
+ 'update_drive_file_sharing_mode',
1974
+ 'delete_file_bulk',
1975
+ 'check_drive_file',
1976
+ 'update_drive_file_tags',
1977
+ 'update_drive_addons',
1978
+ ];
1979
+
1980
+ const exportedFunctions = {};
1981
+
1982
+ methods.forEach((method) => {
1983
+ ['workspace', 'studio', 'user'].forEach((type) => {
1984
+ const funcName = method + '_' + type;
1985
+ // Dynamically creating named exports isn't straightforward in ESM like CJS.
1986
+ // We attach them to an object and export them individually if needed or export the object.
1987
+ // For true ESM named exports, we must declare them statically.
1988
+ // Since the dynamic generation is a pattern here, we'll export them explicitly if known,
1989
+ // or export an object containing them.
1990
+ // However, the request implies converting the file content.
1991
+ // I will map the dynamic exports to explicit named exports below for clarity and ESM compliance.
1992
+ });
1993
+ });
1994
+
1995
+ // Explicitly exporting dynamic functions for ESM compatibility
1996
+ export const get_drive_files_workspace = async (req, job_id, headers, file_obj) => {
1997
+ req.drive_type = 'workspace';
1998
+ return await get_drive_files(req, job_id, headers, file_obj);
1999
+ };
2000
+ export const get_drive_files_studio = async (req, job_id, headers, file_obj) => {
2001
+ req.drive_type = 'studio';
2002
+ return await get_drive_files(req, job_id, headers, file_obj);
2003
+ };
2004
+ export const get_drive_files_user = async (req, job_id, headers, file_obj) => {
2005
+ req.drive_type = 'user';
2006
+ return await get_drive_files(req, job_id, headers, file_obj);
2007
+ };
2008
+
2009
+ export const get_drive_file_info_workspace = async (req, job_id, headers, file_obj) => {
2010
+ req.drive_type = 'workspace';
2011
+ return await get_drive_file_info(req, job_id, headers, file_obj);
2012
+ };
2013
+ export const get_drive_file_info_studio = async (req, job_id, headers, file_obj) => {
2014
+ req.drive_type = 'studio';
2015
+ return await get_drive_file_info(req, job_id, headers, file_obj);
2016
+ };
2017
+ export const get_drive_file_info_user = async (req, job_id, headers, file_obj) => {
2018
+ req.drive_type = 'user';
2019
+ return await get_drive_file_info(req, job_id, headers, file_obj);
2020
+ };
2021
+
2022
+ export const delete_drive_files_workspace = async (req, job_id, headers, file_obj) => {
2023
+ req.drive_type = 'workspace';
2024
+ return await delete_drive_files(req, job_id, headers, file_obj);
2025
+ };
2026
+ export const delete_drive_files_studio = async (req, job_id, headers, file_obj) => {
2027
+ req.drive_type = 'studio';
2028
+ return await delete_drive_files(req, job_id, headers, file_obj);
2029
+ };
2030
+ export const delete_drive_files_user = async (req, job_id, headers, file_obj) => {
2031
+ req.drive_type = 'user';
2032
+ return await delete_drive_files(req, job_id, headers, file_obj);
2033
+ };
2034
+
2035
+ export const extract_drive_file_workspace = async (req, job_id, headers, file_obj) => {
2036
+ req.drive_type = 'workspace';
2037
+ return await extract_drive_file(req, job_id, headers, file_obj);
2038
+ };
2039
+ export const extract_drive_file_studio = async (req, job_id, headers, file_obj) => {
2040
+ req.drive_type = 'studio';
2041
+ return await extract_drive_file(req, job_id, headers, file_obj);
2042
+ };
2043
+ export const extract_drive_file_user = async (req, job_id, headers, file_obj) => {
2044
+ req.drive_type = 'user';
2045
+ return await extract_drive_file(req, job_id, headers, file_obj);
2046
+ };
2047
+
2048
+ export const upload_drive_file_workspace = async (req, job_id, headers, file_obj) => {
2049
+ req.drive_type = 'workspace';
2050
+ return await upload_drive_file(req, job_id, headers, file_obj);
2051
+ };
2052
+ export const upload_drive_file_studio = async (req, job_id, headers, file_obj) => {
2053
+ req.drive_type = 'studio';
2054
+ return await upload_drive_file(req, job_id, headers, file_obj);
2055
+ };
2056
+ export const upload_drive_file_user = async (req, job_id, headers, file_obj) => {
2057
+ req.drive_type = 'user';
2058
+ return await upload_drive_file(req, job_id, headers, file_obj);
2059
+ };
2060
+
2061
+ export const update_drive_file_workspace = async (req, job_id, headers, file_obj) => {
2062
+ req.drive_type = 'workspace';
2063
+ return await update_drive_file(req, job_id, headers, file_obj);
2064
+ };
2065
+ export const update_drive_file_studio = async (req, job_id, headers, file_obj) => {
2066
+ req.drive_type = 'studio';
2067
+ return await update_drive_file(req, job_id, headers, file_obj);
2068
+ };
2069
+ export const update_drive_file_user = async (req, job_id, headers, file_obj) => {
2070
+ req.drive_type = 'user';
2071
+ return await update_drive_file(req, job_id, headers, file_obj);
2072
+ };
2073
+
2074
+ export const create_drive_folder_workspace = async (req, job_id, headers, file_obj) => {
2075
+ req.drive_type = 'workspace';
2076
+ return await create_drive_folder(req, job_id, headers, file_obj);
2077
+ };
2078
+ export const create_drive_folder_studio = async (req, job_id, headers, file_obj) => {
2079
+ req.drive_type = 'studio';
2080
+ return await create_drive_folder(req, job_id, headers, file_obj);
2081
+ };
2082
+ export const create_drive_folder_user = async (req, job_id, headers, file_obj) => {
2083
+ req.drive_type = 'user';
2084
+ return await create_drive_folder(req, job_id, headers, file_obj);
2085
+ };
2086
+
2087
+ export const rename_drive_file_workspace = async (req, job_id, headers, file_obj) => {
2088
+ req.drive_type = 'workspace';
2089
+ return await rename_drive_file(req, job_id, headers, file_obj);
2090
+ };
2091
+ export const rename_drive_file_studio = async (req, job_id, headers, file_obj) => {
2092
+ req.drive_type = 'studio';
2093
+ return await rename_drive_file(req, job_id, headers, file_obj);
2094
+ };
2095
+ export const rename_drive_file_user = async (req, job_id, headers, file_obj) => {
2096
+ req.drive_type = 'user';
2097
+ return await rename_drive_file(req, job_id, headers, file_obj);
2098
+ };
2099
+
2100
+ export const rename_drive_folder_workspace = async (req, job_id, headers, file_obj) => {
2101
+ req.drive_type = 'workspace';
2102
+ return await rename_drive_folder(req, job_id, headers, file_obj);
2103
+ };
2104
+ export const rename_drive_folder_studio = async (req, job_id, headers, file_obj) => {
2105
+ req.drive_type = 'studio';
2106
+ return await rename_drive_folder(req, job_id, headers, file_obj);
2107
+ };
2108
+ export const rename_drive_folder_user = async (req, job_id, headers, file_obj) => {
2109
+ req.drive_type = 'user';
2110
+ return await rename_drive_folder(req, job_id, headers, file_obj);
2111
+ };
2112
+
2113
+ export const update_drive_file_sharing_mode_workspace = async (req, job_id, headers, file_obj) => {
2114
+ req.drive_type = 'workspace';
2115
+ return await update_drive_file_sharing_mode(req, job_id, headers, file_obj);
2116
+ };
2117
+ export const update_drive_file_sharing_mode_studio = async (req, job_id, headers, file_obj) => {
2118
+ req.drive_type = 'studio';
2119
+ return await update_drive_file_sharing_mode(req, job_id, headers, file_obj);
2120
+ };
2121
+ export const update_drive_file_sharing_mode_user = async (req, job_id, headers, file_obj) => {
2122
+ req.drive_type = 'user';
2123
+ return await update_drive_file_sharing_mode(req, job_id, headers, file_obj);
2124
+ };
2125
+
2126
+ export const delete_file_bulk_workspace = async (req, job_id, headers, file_obj) => {
2127
+ req.drive_type = 'workspace';
2128
+ return await delete_file_bulk(req, job_id, headers, file_obj);
2129
+ };
2130
+ export const delete_file_bulk_studio = async (req, job_id, headers, file_obj) => {
2131
+ req.drive_type = 'studio';
2132
+ return await delete_file_bulk(req, job_id, headers, file_obj);
2133
+ };
2134
+ export const delete_file_bulk_user = async (req, job_id, headers, file_obj) => {
2135
+ req.drive_type = 'user';
2136
+ return await delete_file_bulk(req, job_id, headers, file_obj);
2137
+ };
2138
+
2139
+ export const check_drive_file_workspace = async (req, job_id, headers, file_obj) => {
2140
+ req.drive_type = 'workspace';
2141
+ return await check_drive_file(req, job_id, headers, file_obj);
2142
+ };
2143
+ export const check_drive_file_studio = async (req, job_id, headers, file_obj) => {
2144
+ req.drive_type = 'studio';
2145
+ return await check_drive_file(req, job_id, headers, file_obj);
2146
+ };
2147
+ export const check_drive_file_user = async (req, job_id, headers, file_obj) => {
2148
+ req.drive_type = 'user';
2149
+ return await check_drive_file(req, job_id, headers, file_obj);
2150
+ };
2151
+
2152
+ export const update_drive_file_tags_workspace = async (req, job_id, headers, file_obj) => {
2153
+ req.drive_type = 'workspace';
2154
+ return await update_drive_file_tags(req, job_id, headers, file_obj);
2155
+ };
2156
+ export const update_drive_file_tags_studio = async (req, job_id, headers, file_obj) => {
2157
+ req.drive_type = 'studio';
2158
+ return await update_drive_file_tags(req, job_id, headers, file_obj);
2159
+ };
2160
+ export const update_drive_file_tags_user = async (req, job_id, headers, file_obj) => {
2161
+ req.drive_type = 'user';
2162
+ return await update_drive_file_tags(req, job_id, headers, file_obj);
2163
+ };
2164
+
2165
+ export const update_drive_addons_workspace = async (req, job_id, headers, file_obj) => {
2166
+ req.drive_type = 'workspace';
2167
+ return await update_drive_addons(req, job_id, headers, file_obj);
2168
+ };
2169
+ export const update_drive_addons_studio = async (req, job_id, headers, file_obj) => {
2170
+ req.drive_type = 'studio';
2171
+ return await update_drive_addons(req, job_id, headers, file_obj);
2172
+ };
2173
+ export const update_drive_addons_user = async (req, job_id, headers, file_obj) => {
2174
+ req.drive_type = 'user';
2175
+ return await update_drive_addons(req, job_id, headers, file_obj);
2176
+ };