zet-lib 1.2.80 → 1.2.82

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/Form.js CHANGED
@@ -466,7 +466,7 @@ Form.field = (obj) => {
466
466
  if (extFile.type == 'image') {
467
467
  displayForm += `<img src="${filename}" class="boxy zoom mx-2 my-2" width="200px">`
468
468
  } else {
469
- displayForm += Util.fileView(`/uploads/${obj.table}/${obj.key}/`, filename, { withIcon: true })
469
+ displayForm += Util.fileView(`/uploads/${obj.table}/${obj.key}/`, item, { withIcon: true })
470
470
  }
471
471
  })
472
472
  }
package/lib/zAppRouter.js CHANGED
@@ -1370,14 +1370,6 @@ router.post('/zapproval-update/:table', async (req, res) => {
1370
1370
  canUpdate = false
1371
1371
  }
1372
1372
  }
1373
- /*if (approval_history.length > 0) {
1374
- let lastStatus = +approval_history[approval_history.length - 1].status
1375
- canUpdate = lastStatus > +value ? false : true
1376
- let history = approval_history.filter((item) => item.status == value && item.user_id == res.locals.userId)
1377
- if (history.length > 0) {
1378
- canUpdate = false
1379
- }
1380
- }*/
1381
1373
  if (canUpdate) {
1382
1374
  approval_history.push({
1383
1375
  status: value,
@@ -1514,22 +1506,26 @@ router.get('/addapproval-models', async (req, res) => {
1514
1506
 
1515
1507
  //post dropzone widget
1516
1508
  router.post('/zdropzone', async (req, res) => {
1517
- let userId = res.locals.userId
1518
- console.log(req.body)
1519
- if (userId) {
1520
- let dir = `${dirRoot}/public/zdropzone/${userId}`
1521
- fs.ensureDir(dir, (err) => {
1522
- //console.log('ensureDir',err); // => null
1523
- })
1524
- let filename = req.files.file.name
1525
- req.files.file.mv(dir + '/' + filename, function (err) {
1526
- if (err) {
1527
- //console.log('fileempty move',err); // => null
1528
- return res.status(500).send(err)
1509
+ try {
1510
+ let userId = res.locals.userId
1511
+ if (userId) {
1512
+ let dir = `${dirRoot}/public/zdropzone/${userId}`
1513
+ if (!fs.existsSync(dir)) {
1514
+ fs.mkdirSync(dir, { recursive: true })
1529
1515
  }
1530
- })
1516
+ let filename = req.files.file.name
1517
+ req.files.file.mv(dir + '/' + filename, function (err) {
1518
+ if (err) {
1519
+ return res.status(500).send(err + '')
1520
+ }
1521
+ })
1522
+ }
1523
+ res.json('ok')
1524
+ } catch (e) {
1525
+ console.log(e)
1526
+ res.status(500)
1527
+ res.send(e + '')
1531
1528
  }
1532
- res.json('ok')
1533
1529
  })
1534
1530
 
1535
1531
  router.post('/zdropzone-remove', async (req, res) => {
@@ -1537,9 +1533,14 @@ router.post('/zdropzone-remove', async (req, res) => {
1537
1533
  let userId = res.locals.userId
1538
1534
  console.log(req.body)
1539
1535
  let filename = `${dirRoot}/public/zdropzone/${userId}/${req.body.file}`
1540
- await fs.unlink(filename)
1541
- } catch (e) {}
1542
- res.json('ok')
1536
+ if (Util.fileExist(filename)) {
1537
+ await fs.unlink(filename)
1538
+ }
1539
+ res.json('ok')
1540
+ } catch (e) {
1541
+ console.log(e)
1542
+ res.status(500).send(e + '')
1543
+ }
1543
1544
  })
1544
1545
  router.post('/zdropzone-attributes', async (req, res) => {
1545
1546
  try {
@@ -1548,19 +1549,24 @@ router.post('/zdropzone-attributes', async (req, res) => {
1548
1549
  let category = body.category
1549
1550
  let name = `dropzone__${userId}__${body.table}__${body.field}__${body.type}`
1550
1551
  let arr = []
1551
- if (myCache.has(name)) {
1552
- arr = myCache.get(name)
1553
- }
1554
1552
  if (category === 'add') {
1553
+ if (myCache.has(name)) {
1554
+ arr = myCache.get(name)
1555
+ }
1555
1556
  arr.push(body.file)
1556
1557
  } else {
1557
- Util.arrayDelete(arr, body.file)
1558
+ name = `dropzone__${userId}__${body.table}__${body.field}__${body.type}`
1559
+ if (myCache.has(name)) {
1560
+ arr = myCache.get(name)
1561
+ }
1562
+ arr = Util.arrayDelete(arr, body.file)
1558
1563
  }
1559
1564
  myCache.set(name, arr)
1565
+ res.json('ok')
1560
1566
  } catch (e) {
1561
1567
  console.log(e)
1568
+ res.status(500).send(e + '')
1562
1569
  }
1563
- res.json('ok')
1564
1570
  })
1565
1571
 
1566
1572
  module.exports = router
package/lib/zRoute.js CHANGED
@@ -2310,6 +2310,18 @@ zRoute.forms = (req, res, MYMODEL, relations, data = {}) => {
2310
2310
  obj.type = 'email'
2311
2311
  break
2312
2312
  case 'dropzone':
2313
+ let dropzone_data_arr = []
2314
+ if (obj.value.length > 0) {
2315
+ obj.value.map((item) => {
2316
+ let filename = `${dirRoot}/public/uploads/${MYMODEL.table}/${key}/${item}`
2317
+ var stats = fs.statSync(filename)
2318
+ var fileSizeInBytes = stats.size
2319
+ dropzone_data_arr.push({ fileName: item, size: fileSizeInBytes })
2320
+ })
2321
+ myCache.set(`dropzone__${res.locals.userId}__${MYMODEL.table}__${key}__${data.id}`, obj.value)
2322
+ }
2323
+ let dropzoneValue = obj.value.length > 0 ? JSON.stringify(dropzone_data_arr) : '[]'
2324
+ script += `let dropzone_${key}_data = ${dropzoneValue};`
2313
2325
  obj.type = 'dropzone'
2314
2326
  obj.class = 'dropzone boxy-tiny d-block'
2315
2327
  break
@@ -3197,13 +3209,27 @@ zRoute.generateJS = (req, res, MYMODEL, relations, zForms = '', data = {}) => {
3197
3209
  })
3198
3210
  },
3199
3211
  init: function() {
3200
- let type = window.location.href.split("/").pop();
3201
- this.on("addedfile", function(file) {
3212
+ let dz = this;
3213
+ const type = window.location.href.split("/").pop();
3214
+ dz.on("addedfile", function(file) {
3202
3215
  ajaxPost("/zdropzone-attributes",{file:file.name,'category':'add',field:"${item}",table:"${MYMODEL.table}", type:type},() => {})
3203
3216
  });
3204
- this.on("removedfile", function(file) {
3205
- ajaxPost("/zdropzone-attributes",{file:file.name,'category':'remove',field:"${item}",table:"${MYMODEL.table}", type:type},() => {})
3217
+ dz.on("removedfile", function(file) {
3218
+ ajaxPost("/zdropzone-attributes",{file:file.name,'category':'remove',field:"${item}",table:"${MYMODEL.table}", type:type},() => {});
3219
+ $("div#${item}").find(".dz-message").remove();
3206
3220
  });
3221
+ if(dropzone_${item}_data.length > 0) {
3222
+ dropzone_${item}_data.map((item) => {
3223
+ const mockFile = {
3224
+ name: item.fileName,
3225
+ id: item,
3226
+ size: item.size,
3227
+ accepted:true
3228
+ };
3229
+ dz.options.addedfile.call(dz, mockFile);
3230
+ dz.options.thumbnail.call(dz, mockFile, "${process.env.APP_URL}/uploads/${MYMODEL.table}/${item}/"+item.fileName);
3231
+ })
3232
+ };
3207
3233
  }
3208
3234
  }); `
3209
3235
  })
@@ -3517,12 +3543,12 @@ zRoute.insertSQL = async (req, res, table, data) => {
3517
3543
  let hasDropzone = false
3518
3544
  for (let key in MYMODEL.widgets) {
3519
3545
  if (MYMODEL.widgets[key].name === 'dropzone') {
3520
- console.log('has dropzone')
3546
+ //console.log('has dropzone')
3521
3547
  let path_src = dirRoot + '/public/zdropzone/' + userId + '/'
3522
3548
  let path_dest = dirRoot + '/public/uploads/' + MYMODEL.routeName + '/' + key + '/'
3523
- fs.ensureDir(path_dest, (err) => {
3524
- //console.log('ensureDir',err); // => null
3525
- })
3549
+ if (!fs.existsSync(path_dest)) {
3550
+ fs.mkdirSync(path_dest, { recursive: true })
3551
+ }
3526
3552
  let name = `dropzone__${res.locals.userId}__${table}__${key}__create`
3527
3553
  if (myCache.has(name)) {
3528
3554
  let arr = myCache.get(name)
@@ -3549,11 +3575,12 @@ zRoute.updateSQL = async (req, res, table, data, whereData) => {
3549
3575
  const MYMODELS = myCache.get('MYMODELS')
3550
3576
  let MYMODEL = MYMODELS[table]
3551
3577
  let fields = MYMODEL.keys
3578
+ const userId = res.locals.userId
3552
3579
  if (fields.includes('updated_at')) {
3553
3580
  data.updated_at = Util.now()
3554
3581
  }
3555
3582
  if (fields.includes('updated_by')) {
3556
- data.updated_by = res.locals.userId
3583
+ data.updated_by = userId
3557
3584
  }
3558
3585
  //check if has table and image/file
3559
3586
  let hasImages = false
@@ -3571,6 +3598,32 @@ zRoute.updateSQL = async (req, res, table, data, whereData) => {
3571
3598
  }
3572
3599
  }
3573
3600
  }
3601
+ if (MYMODEL.widgets[key].name === 'dropzone') {
3602
+ //console.log('has dropzone')
3603
+ let path_src = dirRoot + '/public/zdropzone/' + userId + '/'
3604
+ let path_dest = dirRoot + '/public/uploads/' + MYMODEL.table + '/' + key + '/'
3605
+ if (!fs.existsSync(path_dest)) {
3606
+ fs.mkdirSync(path_dest, { recursive: true })
3607
+ }
3608
+ let name = `dropzone__${userId}__${MYMODEL.table}__${key}__${whereData.id}`
3609
+ if (myCache.has(name)) {
3610
+ let arr = myCache.get(name)
3611
+ let newArr = []
3612
+ if (arr.length > 0) {
3613
+ let time = new Date().getTime()
3614
+ arr.map((item) => {
3615
+ if (!Util.fileExist(path_dest + item)) {
3616
+ let newItem = time + item
3617
+ newArr.push(newItem)
3618
+ fs.rename(path_src + item, path_dest + newItem)
3619
+ } else {
3620
+ newArr.push(item)
3621
+ }
3622
+ })
3623
+ data[key] = Util.array_to_jsonb(newArr)
3624
+ }
3625
+ }
3626
+ }
3574
3627
  }
3575
3628
  let result = await connection.result({
3576
3629
  table: MYMODEL.table,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zet-lib",
3
- "version": "1.2.80",
3
+ "version": "1.2.82",
4
4
  "description": "zet is a library that part of zet generator.",
5
5
  "engines": {
6
6
  "node": ">=18"