zet-lib 1.4.11 → 1.4.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/zRoute.js CHANGED
@@ -2,6 +2,7 @@
2
2
  * For default route controller
3
3
  * service for routes
4
4
  */
5
+ require("dotenv").config();
5
6
  const excelToJson = require("convert-excel-to-json");
6
7
  const qs = require("qs");
7
8
  //for generate PDF
@@ -20,6 +21,7 @@ const debug = require("./debug");
20
21
  const moduleLib = require("./moduleLib");
21
22
  const cForm = require("./Form");
22
23
  const readXlsxFile = require("read-excel-file/node");
24
+ const { Dropbox } = require("dropbox");
23
25
 
24
26
  const zRoute = {};
25
27
 
@@ -1265,6 +1267,20 @@ zRoute.dataTableData = (
1265
1267
  }
1266
1268
  break;
1267
1269
 
1270
+ case "dropbox":
1271
+ if (myvalue && myvalue.length > 0) {
1272
+ if (typeof myvalue === "string") {
1273
+ value = [myvalue];
1274
+ }
1275
+ myvalue = value.map(
1276
+ (item) =>
1277
+ `<a class="text-success" target="_blank" href="/zdropbox-view/${
1278
+ MYMODEL.routeName
1279
+ }/${key}/${item}"> ${item.substring(13)}</a> `
1280
+ );
1281
+ }
1282
+ break;
1283
+
1268
1284
  case "password":
1269
1285
  myvalue = "xxxxxx";
1270
1286
  break;
@@ -3090,6 +3106,33 @@ zRoute.forms = (
3090
3106
  obj.type = "dropzone";
3091
3107
  obj.class = "dropzone boxy-tiny d-block";
3092
3108
  break;
3109
+ case "dropbox":
3110
+ let dropbox_data_arr = [];
3111
+ if (obj.value.length > 0) {
3112
+ let dropfiles = obj.value || [];
3113
+ if (typeof obj.value == "string") {
3114
+ dropfiles = [obj.value];
3115
+ }
3116
+ dropfiles.map((item) => {
3117
+ let filename = item; //`${dirRoot}/public/uploads/${MYMODEL.table}/${key}/${item}`;
3118
+ var fileSizeInBytes = 12345;
3119
+ if (Util.fileExist(filename)) {
3120
+ var stats = fs.statSync(filename);
3121
+ fileSizeInBytes = stats.size;
3122
+ }
3123
+ dropbox_data_arr.push({ fileName: item, size: fileSizeInBytes });
3124
+ });
3125
+ myCache.set(
3126
+ `dropbox_${res.locals.userId}__${MYMODEL.table}__${key}__${data.id}`,
3127
+ obj.value
3128
+ );
3129
+ }
3130
+ let dropboxValue =
3131
+ obj.value.length > 0 ? JSON.stringify(dropbox_data_arr) : "[]";
3132
+ script += `let dropbox_${key}_data = ${dropboxValue};`;
3133
+ obj.type = "dropbox";
3134
+ obj.class = "dropzone boxy-tiny d-block";
3135
+ break;
3093
3136
  case "lexical":
3094
3137
  obj.type = "lexical";
3095
3138
  if (obj.value) {
@@ -3953,6 +3996,7 @@ zRoute.generateJS = (req, res, MYMODEL, relations, zForms = "", data = {}) => {
3953
3996
  let hasDateTimePicker = false;
3954
3997
  let hasDropzone = false;
3955
3998
  let dropzones = [];
3999
+ let dropboxes = [];
3956
4000
  let hasTable = false;
3957
4001
  let chainsObj = {};
3958
4002
  let chainsArr = [];
@@ -3964,6 +4008,7 @@ zRoute.generateJS = (req, res, MYMODEL, relations, zForms = "", data = {}) => {
3964
4008
  let hasLexical = false;
3965
4009
  let hasLocation = false;
3966
4010
  let hasDragdrop = false;
4011
+ let hasDropbox = false;
3967
4012
  let lexicals = [];
3968
4013
  let mapKey = "";
3969
4014
  let hasAttributes = [];
@@ -4035,6 +4080,9 @@ zRoute.generateJS = (req, res, MYMODEL, relations, zForms = "", data = {}) => {
4035
4080
  } else if (widgets[key].name == "dropzone") {
4036
4081
  hasDropzone = true;
4037
4082
  dropzones.push(key);
4083
+ } else if (widgets[key].name == "dropbox") {
4084
+ hasDropbox = true;
4085
+ dropboxes.push(key);
4038
4086
  }
4039
4087
  // has chains
4040
4088
  if (widgets[key].name == "relation") {
@@ -4073,7 +4121,7 @@ zRoute.generateJS = (req, res, MYMODEL, relations, zForms = "", data = {}) => {
4073
4121
  headObj.clockpicker = clockpickerObj.head;
4074
4122
  endObj.clockpicker = clockpickerObj.end;
4075
4123
  }
4076
- if (hasDropzone) {
4124
+ if (hasDropzone || hasDropbox) {
4077
4125
  let dropzoneObj = moduleLib.dropzone(req, res);
4078
4126
  headObj.dropzone = dropzoneObj.head;
4079
4127
  endObj.dropzone = dropzoneObj.end;
@@ -4238,7 +4286,6 @@ zRoute.generateJS = (req, res, MYMODEL, relations, zForms = "", data = {}) => {
4238
4286
  window.open(src, '_blank');
4239
4287
  })
4240
4288
  }
4241
-
4242
4289
  })
4243
4290
  },1000)
4244
4291
  };
@@ -4247,6 +4294,69 @@ zRoute.generateJS = (req, res, MYMODEL, relations, zForms = "", data = {}) => {
4247
4294
  });
4248
4295
  }
4249
4296
 
4297
+ if (hasDropbox) {
4298
+ dropboxes.map((item) => {
4299
+ scriptForm += `$("div#${item}").dropzone({
4300
+ url: "/zdropbox/${MYMODEL.table}/${item}",
4301
+ paramName: "file",
4302
+ addRemoveLinks: !0,
4303
+ maxFilesize: 30,
4304
+ headers: {"X-CSRF-TOKEN": $('meta[name="csrf-token"]').attr("content")},
4305
+ removedfile: function(file) {
4306
+ const type = window.location.href.split("/").pop();
4307
+ ajaxPost("/zdropbox-remove",{file:file.name, cname:"dropbox__ZUSER___ID__${MYMODEL.table}__${item}__"+type},(data) => {
4308
+ file.previewElement.remove();
4309
+ })
4310
+ },
4311
+ init: function() {
4312
+ let dz = this;
4313
+ const type = window.location.href.split("/").pop();
4314
+ dz.on("addedfile", function(file) {
4315
+ ajaxPost("/zdropbox-attributes",{file:file.name,'category':'add',field:"${item}",table:"${MYMODEL.table}", type:type},() => {})
4316
+ });
4317
+ dz.on("removedfile", function(file) {
4318
+ ajaxPost("/zdropbox-attributes",{file:file.name,'category':'remove',field:"${item}",table:"${MYMODEL.table}", type:type},() => {});
4319
+ $("div#${item}").find(".dz-message").remove();
4320
+ });
4321
+ if(dropbox_${item}_data.length > 0) {
4322
+ ajaxPost('/zdropbox-files',{files:dropbox_${item}_data,table:"${MYMODEL.table}",field:"${item}"}, function(datas) {
4323
+ datas.map((mockFile) => {
4324
+ dz.options.addedfile.call(dz, mockFile);
4325
+ dz.options.thumbnail.call(dz, mockFile, mockFile.link);
4326
+ const imageElement = mockFile.previewElement.querySelector('.dz-image');
4327
+ $(imageElement).addClass("divzdropbox")
4328
+ let ext = mockFile.name.toLowerCase().split(".").pop();
4329
+ imageElement.setAttribute('data-ext', ext);
4330
+ imageElement.setAttribute('data-name', mockFile.name);
4331
+ imageElement.setAttribute('data-link', mockFile.link);
4332
+ $(imageElement).attr('id', mockFile.id.content_hash);
4333
+ $(imageElement).css('z-index',9999)
4334
+ })
4335
+ let time = datas.length <10 ? 2000 : 4000;
4336
+ setTimeout(function(){
4337
+ $(".divzdropbox").each(function () {
4338
+ let ext = $(this).attr('data-ext');
4339
+ if (ext == "gif" || ext == "png" || ext == "jpeg" || ext == "jpg" || ext == "bmp" || ext == "webp" || ext == "jiff" || ext == "svg" || ext == "avif") {
4340
+ $(this).on("click", function () {
4341
+ $(".zimage-modal").attr('src', $(this).attr('data-link')).attr('data-name', $(this).attr('data-name'));
4342
+ $("#zmodal-image").modal("show");
4343
+ })
4344
+ } else {
4345
+ let file = nonImagesTypeObject[ext] || 'file.png';
4346
+ $(this).find("img").attr("src", "/img/" + file);
4347
+ $(this).on("click", function () {
4348
+ window.open($(this).attr('data-link'), '_blank');
4349
+ })
4350
+ }
4351
+ })
4352
+ },time);
4353
+ })
4354
+ }
4355
+ }
4356
+ }); `;
4357
+ });
4358
+ }
4359
+
4250
4360
  defaultScript = `${Util.newLine} $(function () {
4251
4361
  $(".isfile").each(function (index, value) {
4252
4362
  let filename = $(this).attr("data-filename") || "";
@@ -4261,24 +4371,7 @@ zRoute.generateJS = (req, res, MYMODEL, relations, zForms = "", data = {}) => {
4261
4371
  if (ext == "gif" || ext == "png" || ext == "jpeg" || ext == "jpg" || ext == "bmp" || ext == "webp" || ext == "jiff" || ext == "svg" || ext == "avif") {
4262
4372
  $("#file"+id).attr("src","/uploads/"+table+"/"+filename).attr("width",width+"px");
4263
4373
  } else {
4264
- let file = 'file.png';
4265
- if(ext == "docx" || ext == "doc") {
4266
- file = 'word.png'
4267
- } else if(ext == 'xls' || ext == 'xlsx') {
4268
- file = 'excel.png'
4269
- } else if(ext == 'pdf') {
4270
- file = 'pdf.png'
4271
- } else if(ext == 'ppt' || ext == 'pptx'){
4272
- file = 'ppt.png';
4273
- } else if(ext == 'txt') {
4274
- file = 'txt.png';
4275
- } else if(ext == 'zip') {
4276
- file = 'zip.jpg';
4277
- } else if(ext == 'rar') {
4278
- file = 'rar.jpg';
4279
- } else if(ext == 'rar') {
4280
- file = 'file.png';
4281
- }
4374
+ let file = nonImagesTypeObject[ext] || 'file.png';
4282
4375
  $("#file"+id).attr("src","/img/"+file).attr("height","45px").attr("width","45px").addClass("boxy-small");
4283
4376
  }
4284
4377
  $("#file"+id).on("click", function () {
@@ -4482,7 +4575,7 @@ $("#body-${keys}>tr").each(function (index, tr) {
4482
4575
  var ${keys} = $("#body-${keys}").data("value") ? $("#body-${keys}").data("value") : [];
4483
4576
  ${keys}.forEach(function (myobj, index) {
4484
4577
  build${keys}form(index, myobj);
4485
- });
4578
+ });
4486
4579
  });${Util.newLine}`*/
4487
4580
 
4488
4581
  break;
@@ -4561,6 +4654,7 @@ ${keys}.forEach(function (myobj, index) {
4561
4654
  /*
4562
4655
  MySQL CRUD with context
4563
4656
  */
4657
+
4564
4658
  zRoute.insertSQL = async (req, res, table, data) => {
4565
4659
  try {
4566
4660
  const MYMODELS = myCache.get("MYMODELS");
@@ -4578,7 +4672,9 @@ zRoute.insertSQL = async (req, res, table, data) => {
4578
4672
  delete data.approval_history;
4579
4673
 
4580
4674
  //check dropzone
4675
+ let movDropboxArr = [];
4581
4676
  let hasDropzone = false;
4677
+ let hasDropbox = false;
4582
4678
  for (let key in MYMODEL.widgets) {
4583
4679
  if (MYMODEL.widgets[key].name === "dropzone") {
4584
4680
  //console.log('has dropzone')
@@ -4604,13 +4700,75 @@ zRoute.insertSQL = async (req, res, table, data) => {
4604
4700
  data[key] = Util.array_to_jsonb(newArr);
4605
4701
  }
4606
4702
  }
4607
-
4608
4703
  //delete cache
4609
4704
  myCache.del(name);
4705
+ } else if (MYMODEL.widgets[key].name === "dropbox") {
4706
+ //console.log('has dropbox')
4707
+ let path_src = `/temps/${MYMODEL.table}/${key}/${userId}`;
4708
+ let path_dest = `/${MYMODEL.table}/${key}`;
4709
+
4710
+ // Initialize Dropbox client
4711
+ let dbx = new Dropbox({
4712
+ accessToken: process.env.DROPBOX_ACCESS_TOKEN,
4713
+ refreshToken: process.env.DROPBOX_REFRESH_TOKEN,
4714
+ clientId: process.env.DROPBOX_CLIENT_ID,
4715
+ clientSecret: process.env.DROPBOX_CLIENT_SECRET,
4716
+ fetch: fetch,
4717
+ });
4718
+
4719
+ try {
4720
+ await dbx.filesCreateFolderV2({
4721
+ path: path_dest,
4722
+ autorename: false,
4723
+ });
4724
+ } catch (error) {
4725
+ if (error.status !== 409) {
4726
+ // 409 means folder already exists
4727
+ throw error;
4728
+ }
4729
+ }
4730
+
4731
+ //get all files in folder
4732
+ //let files = await
4733
+ // First, move the file to the new location
4734
+ let name = `dropbox__${res.locals.userId}__${table}__${key}__create`;
4735
+ if (myCache.has(name)) {
4736
+ let arr = myCache.get(name);
4737
+ let newArr = [];
4738
+ if (arr.length > 0) {
4739
+ let time = new Date().getTime();
4740
+ arr.map(async (item) => {
4741
+ //check
4742
+ try {
4743
+ let newItem = Util.cleanString(time + item);
4744
+ movDropboxArr.push({
4745
+ from_path: `${path_src}/${item}`,
4746
+ to_path: `${path_dest}/${newItem}`,
4747
+ });
4748
+ /*await dbx.filesMoveV2({
4749
+ from_path: `${path_src}/${item}`,
4750
+ to_path: `${path_dest}/${newItem}`,
4751
+ autorename: true
4752
+ });*/
4753
+ newArr.push(newItem);
4754
+ } catch (e) {
4755
+ console.log(e);
4756
+ }
4757
+ });
4758
+ data[key] = Util.array_to_jsonb(newArr);
4759
+ }
4760
+ }
4761
+ myCache.del(name);
4610
4762
  }
4611
4763
  }
4612
4764
  const result = await connection.insert({ table: table, data: data });
4613
4765
  zRoute.modelsCacheRenew(table, res.locals.companyId);
4766
+ //dropbox
4767
+ if (movDropboxArr.length > 0) {
4768
+ setTimeout(function () {
4769
+ zRoute.moveDropbox(movDropboxArr);
4770
+ }, 3000);
4771
+ }
4614
4772
  return result;
4615
4773
  } catch (e) {
4616
4774
  throw Error(e + "");
@@ -4618,6 +4776,28 @@ zRoute.insertSQL = async (req, res, table, data) => {
4618
4776
  }
4619
4777
  };
4620
4778
 
4779
+ zRoute.moveDropbox = async (items) => {
4780
+ try {
4781
+ // Initialize Dropbox client
4782
+ let dbx = new Dropbox({
4783
+ accessToken: process.env.DROPBOX_ACCESS_TOKEN,
4784
+ refreshToken: process.env.DROPBOX_REFRESH_TOKEN,
4785
+ clientId: process.env.DROPBOX_CLIENT_ID,
4786
+ clientSecret: process.env.DROPBOX_CLIENT_SECRET,
4787
+ fetch: fetch,
4788
+ });
4789
+ for (item of items) {
4790
+ await dbx.filesMoveV2({
4791
+ from_path: item.from_path,
4792
+ to_path: item.to_path,
4793
+ autorename: true,
4794
+ });
4795
+ }
4796
+ } catch (e) {
4797
+ console.log(e);
4798
+ }
4799
+ };
4800
+
4621
4801
  zRoute.updateSQL = async (req, res, table, data, whereData) => {
4622
4802
  try {
4623
4803
  const MYMODELS = myCache.get("MYMODELS");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zet-lib",
3
- "version": "1.4.11",
3
+ "version": "1.4.13",
4
4
  "description": "zet is a library that part of zet generator.",
5
5
  "engines": {
6
6
  "node": ">=18"
@@ -31,25 +31,26 @@
31
31
  "axios": "^1.6.8",
32
32
  "convert-excel-to-json": "^1.7.0",
33
33
  "csurf": "^1.11.0",
34
- "dotenv": "^16.3.1",
34
+ "dotenv": "^16.5.0",
35
+ "dropbox": "^10.34.0",
35
36
  "ejs": "^3.1.9",
36
37
  "exceljs": "^4.4.0",
37
38
  "express-zip": "^3.0.0",
38
- "fs-extra": "^11.1.1",
39
+ "fs-extra": "^11.3.0",
39
40
  "html-minifier-terser": "^7.2.0",
41
+ "isomorphic-fetch": "^3.0.0",
40
42
  "js-sha256": "^0.9.0",
41
43
  "moment": "^2.30.1",
42
44
  "node-cache": "^5.1.2",
43
45
  "nodemailer": "^6.9.4",
44
46
  "pg": "^8.11.2",
45
47
  "pm2": "^5.3.1",
46
- "puppeteer": "^24.6.1",
48
+ "puppeteer": "^24.7.2",
47
49
  "qs": "^6.11.2",
48
50
  "randomstring": "^1.3.0",
49
51
  "read-excel-file": "^5.8.0",
50
52
  "sharp": "^0.34.1",
51
53
  "socket.io": "^4.8.0",
52
- "timers-promises": "^1.0.1",
53
54
  "uglify-js": "^3.17.4",
54
55
  "uuid": "^9.0.1",
55
56
  "xlsx": "^0.18.5"