zet-lib 1.4.15 → 1.4.17

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/zAppRouter.js CHANGED
@@ -1624,13 +1624,14 @@ router.post("/zdropzone-remove", async (req, res) => {
1624
1624
  fs.mkdirSync(dir, { recursive: true });
1625
1625
  }
1626
1626
  let filename = `${dirRoot}/public/zdropzone/${userId}/${req.body.file}`;
1627
+ let arr = [];
1627
1628
  if (Util.fileExist(filename)) {
1628
1629
  await fs.unlink(filename);
1629
1630
  if (myCache.has(cacheName)) {
1630
- arr = myCache.get(name);
1631
+ arr = myCache.get(cacheName);
1631
1632
  }
1632
1633
  arr = Util.arrayDelete(arr, body.file);
1633
- myCache.set(name, arr);
1634
+ myCache.set(cacheName, arr);
1634
1635
  }
1635
1636
  res.json("ok");
1636
1637
  } catch (e) {
@@ -2007,19 +2008,14 @@ router.post("/zdropbox/:table/:field", handleTokenRefresh, async (req, res) => {
2007
2008
  router.post("/zdropbox-attributes", handleTokenRefresh, async (req, res) => {
2008
2009
  try {
2009
2010
  let userId = res.locals.userId;
2010
- let dir = `${dirRoot}/public/zdropzone/${userId}`;
2011
- if (!fs.existsSync(dir)) {
2012
- fs.mkdirSync(dir, { recursive: true });
2013
- }
2014
2011
  let body = req.body;
2012
+ console.log(body);
2015
2013
  let category = body.category;
2016
2014
  let name = `dropbox__${userId}__${body.table}__${body.field}__${body.type}`;
2017
- //dropzone__${res.locals.userId}__${table}__${key}__create
2018
2015
  let arr = myCache.has(name) ? myCache.get(name) : [];
2019
2016
  if (category === "add") {
2020
2017
  arr.push(body.file);
2021
2018
  } else {
2022
- name = `dropzone__${userId}__${body.table}__${body.field}__${body.type}`;
2023
2019
  if (myCache.has(name)) {
2024
2020
  arr = myCache.get(name);
2025
2021
  }
@@ -2034,52 +2030,77 @@ router.post("/zdropbox-attributes", handleTokenRefresh, async (req, res) => {
2034
2030
  });
2035
2031
 
2036
2032
  //get all files and meta in dropbox
2037
- router.post("/zdropbox-files", handleTokenRefresh, async (req, res) => {
2033
+ router.post("/zdropbox-files", async (req, res) => {
2038
2034
  let datas = [];
2039
2035
  const room = res.locals.token;
2040
2036
  try {
2041
2037
  const { files, table, field } = req.body;
2042
2038
  let dir = `/${table}/${field}`;
2043
2039
  for (file of files) {
2044
- let pathFile = `${dir}/${file.fileName}`;
2045
- //console.log(pathFile)
2040
+ let fileName = file.fileName;
2041
+ let ext = Util.fileExtension(fileName);
2042
+ const mockFile = {
2043
+ name: fileName,
2044
+ id: fileName,
2045
+ path: "/img/dropbox.png",
2046
+ link: "/img/dropbox.png",
2047
+ isImage: ext.image ? ["image"] : null,
2048
+ size: 1234,
2049
+ modified: "324234",
2050
+ accepted: true,
2051
+ };
2052
+ datas.push(mockFile);
2053
+ }
2054
+ } catch (e) {
2055
+ console.log(e);
2056
+ res.status(500).send(e + "");
2057
+ }
2058
+ res.json(datas);
2059
+ });
2060
+ router.post("/zdropbox-file/:index", handleTokenRefresh, async (req, res) => {
2061
+ let datas = {};
2062
+ const room = res.locals.token;
2063
+ try {
2064
+ const { fileName, table, field } = req.body;
2065
+ let dir = `/${table}/${field}`;
2066
+ let pathFile = `${dir}/${fileName}`;
2067
+ //console.log(pathFile)
2068
+ try {
2069
+ const metadata = await dbx.filesGetMetadata({
2070
+ path: pathFile,
2071
+ });
2072
+ let link = ``;
2046
2073
  try {
2047
- const metadata = await dbx.filesGetMetadata({
2074
+ const response = await dbx.filesGetTemporaryLink({
2048
2075
  path: pathFile,
2049
2076
  });
2050
- let link = ``;
2051
- try {
2052
- const response = await dbx.filesGetTemporaryLink({
2053
- path: pathFile,
2054
- });
2055
- link = response.result.link;
2056
- } catch (error) {
2057
- console.error("Error getting temporary link:", error);
2058
- io.to(room).emit(
2059
- "errormessage",
2060
- `Error getting temporary link: ${error.message}`
2061
- );
2062
- }
2063
-
2064
- const mockFile = {
2065
- name: metadata.result.name,
2066
- id: metadata.result,
2067
- path: metadata.result.path_display,
2068
- link: link,
2069
- isImage: metadata.result.name.match(/\.(jpg|jpeg|png|gif)$/i),
2070
- size: metadata.result.size,
2071
- modified: metadata.result.server_modified,
2072
- accepted: true,
2073
- };
2074
- //console.log(mockFile)
2075
- datas.push(mockFile);
2076
- } catch (e) {
2077
- console.log(e);
2077
+ link = response.result.link;
2078
+ } catch (error) {
2079
+ console.error("Error getting temporary link:", error);
2078
2080
  io.to(room).emit(
2079
2081
  "errormessage",
2080
- `${e.message} Error ${pathFile} not exist in dropbox`
2082
+ `Error getting temporary link: ${error.message}`
2081
2083
  );
2082
2084
  }
2085
+ const mockFile = {
2086
+ name: metadata.result.name,
2087
+ id: metadata.result,
2088
+ path: metadata.result.path_display,
2089
+ link: link,
2090
+ isImage: metadata.result.name.match(/\.(jpg|jpeg|png|gif)$/i),
2091
+ size: metadata.result.size,
2092
+ modified: metadata.result.server_modified,
2093
+ accepted: true,
2094
+ index: req.params.index,
2095
+ };
2096
+ //console.log(mockFile)
2097
+ datas = mockFile;
2098
+ } catch (e) {
2099
+ console.log(e);
2100
+ io.to(room).emit(
2101
+ "errormessage",
2102
+ `${e.message} Error ${pathFile} not exist in dropbox, Please reload your browser !!`
2103
+ );
2083
2104
  }
2084
2105
  } catch (e) {
2085
2106
  console.log(e);
@@ -2088,15 +2109,37 @@ router.post("/zdropbox-files", handleTokenRefresh, async (req, res) => {
2088
2109
  res.json(datas);
2089
2110
  });
2090
2111
 
2091
- // Delete file
2092
- router.post("/zdropbox-delete/", handleTokenRefresh, async (req, res) => {
2112
+ // Delete file dicache dulu kemudian submit delete dropbox file
2113
+ router.post("/zdropbox-remove/", handleTokenRefresh, async (req, res) => {
2093
2114
  try {
2094
2115
  let body = req.body;
2095
- let path = body.path;
2096
- const filePath = decodeURIComponent(req.params.path);
2097
- await dbx.filesDeleteV2({
2098
- path: filePath,
2099
- });
2116
+ console.log(body);
2117
+ const userId = res.locals.userId;
2118
+ const fileName = body.file;
2119
+ let cname = body.cname.replace("ZUSER___ID", userId);
2120
+ let cacheName = cname;
2121
+ console.log(cacheName);
2122
+ let splits = cname.split("__");
2123
+ let table = splits[2];
2124
+ let field = splits[3];
2125
+ let type = splits[4];
2126
+ const dir = `/temps/${table}/${field}/${userId}`;
2127
+ await ensureFolder(dir);
2128
+ const filePath = `${dir}/${fileName}`;
2129
+ let arr = [];
2130
+ if (myCache.has(cacheName)) {
2131
+ arr = myCache.get(cacheName);
2132
+ }
2133
+ try {
2134
+ /* await dbx.filesDeleteV2({
2135
+ path: filePath,
2136
+ });*/
2137
+ arr = Util.arrayDelete(arr, body.file);
2138
+ myCache.set(cacheName, arr);
2139
+ } catch (e) {
2140
+ console.log(e);
2141
+ }
2142
+
2100
2143
  res.json({ success: true });
2101
2144
  } catch (error) {
2102
2145
  console.error("Error deleting file:", error);
package/lib/zRoute.js CHANGED
@@ -3123,7 +3123,7 @@ zRoute.forms = (
3123
3123
  dropbox_data_arr.push({ fileName: item, size: fileSizeInBytes });
3124
3124
  });
3125
3125
  myCache.set(
3126
- `dropbox_${res.locals.userId}__${MYMODEL.table}__${key}__${data.id}`,
3126
+ `dropbox__${res.locals.userId}__${MYMODEL.table}__${key}__${data.id}`,
3127
3127
  obj.value
3128
3128
  );
3129
3129
  }
@@ -4330,10 +4330,17 @@ zRoute.generateJS = (req, res, MYMODEL, relations, zForms = "", data = {}) => {
4330
4330
  imageElement.setAttribute('data-name', mockFile.name);
4331
4331
  imageElement.setAttribute('data-link', mockFile.link);
4332
4332
  $(imageElement).attr('id', mockFile.id.content_hash);
4333
- $(imageElement).css('z-index',9999)
4333
+ $(imageElement).css('z-index',9999);
4334
+ ajaxPost('/zdropbox-file/' +index,{fileName:mockFile.name,table:"${MYMODEL.table}",field:"${item}"}, function(result) {
4335
+ imageElement.setAttribute('data-link', result.link);
4336
+ $(imageElement).attr('id', result.id.content_hash);
4337
+ $(imageElement).find("img").attr("src", result.link);
4338
+ if(+result.index == (dropbox_${item}_data.length - 1) ) {
4339
+ modaldropbox();
4340
+ }
4341
+ })
4334
4342
  })
4335
- let time = datas.length <10 ? 2000 : 4000;
4336
- setTimeout(function(){
4343
+ function modaldropbox() {
4337
4344
  $(".divzdropbox").each(function () {
4338
4345
  let ext = $(this).attr('data-ext');
4339
4346
  if (ext == "gif" || ext == "png" || ext == "jpeg" || ext == "jpg" || ext == "bmp" || ext == "webp" || ext == "jiff" || ext == "svg" || ext == "avif") {
@@ -4349,7 +4356,7 @@ zRoute.generateJS = (req, res, MYMODEL, relations, zForms = "", data = {}) => {
4349
4356
  })
4350
4357
  }
4351
4358
  })
4352
- },time);
4359
+ }
4353
4360
  })
4354
4361
  }
4355
4362
  }
@@ -4745,11 +4752,6 @@ zRoute.insertSQL = async (req, res, table, data) => {
4745
4752
  from_path: `${path_src}/${item}`,
4746
4753
  to_path: `${path_dest}/${newItem}`,
4747
4754
  });
4748
- /*await dbx.filesMoveV2({
4749
- from_path: `${path_src}/${item}`,
4750
- to_path: `${path_dest}/${newItem}`,
4751
- autorename: true
4752
- });*/
4753
4755
  newArr.push(newItem);
4754
4756
  } catch (e) {
4755
4757
  console.log(e);
@@ -4855,6 +4857,77 @@ zRoute.updateSQL = async (req, res, table, data, whereData) => {
4855
4857
  });
4856
4858
  data[key] = Util.array_to_jsonb(newArr);
4857
4859
  }
4860
+ } else if (MYMODEL.widgets[key].name === "dropbox") {
4861
+ //console.log('has dropbox')
4862
+ let path_src = `/temps/${MYMODEL.table}/${key}/${userId}`;
4863
+ let path_dest = `/${MYMODEL.table}/${key}`;
4864
+
4865
+ // Initialize Dropbox client
4866
+ let dbx = new Dropbox({
4867
+ accessToken: process.env.DROPBOX_ACCESS_TOKEN,
4868
+ refreshToken: process.env.DROPBOX_REFRESH_TOKEN,
4869
+ clientId: process.env.DROPBOX_CLIENT_ID,
4870
+ clientSecret: process.env.DROPBOX_CLIENT_SECRET,
4871
+ fetch: fetch,
4872
+ });
4873
+
4874
+ try {
4875
+ await dbx.filesCreateFolderV2({
4876
+ path: path_dest,
4877
+ autorename: false,
4878
+ });
4879
+ } catch (error) {
4880
+ if (error.status !== 409) {
4881
+ // 409 means folder already exists
4882
+ throw error;
4883
+ }
4884
+ }
4885
+ let name = `dropbox__${userId}__${MYMODEL.table}__${key}__${whereData.id}`;
4886
+ if (myCache.has(name)) {
4887
+ let arr = myCache.get(name) || [];
4888
+ let newArr = [];
4889
+ let time = new Date().getTime();
4890
+ arr.map((item) => {
4891
+ //todo
4892
+ //jika item tidak ada di dropbox
4893
+ try {
4894
+ } catch (e) {}
4895
+ if (!Util.fileExist(path_dest + item)) {
4896
+ let newItem = time + item;
4897
+ newArr.push(newItem);
4898
+ if (Util.fileExist(path_src + item)) {
4899
+ fs.rename(path_src + item, path_dest + newItem);
4900
+ }
4901
+ } else {
4902
+ newArr.push(item);
4903
+ }
4904
+ });
4905
+ data[key] = Util.array_to_jsonb(newArr);
4906
+ }
4907
+ /////
4908
+ /*let name = `dropbox__${res.locals.userId}__${table}__${key}__create`;
4909
+ if (myCache.has(name)) {
4910
+ let arr = myCache.get(name);
4911
+ let newArr = [];
4912
+ if (arr.length > 0) {
4913
+ let time = new Date().getTime();
4914
+ arr.map(async (item) => {
4915
+ //check
4916
+ try {
4917
+ let newItem = Util.cleanString(time + item);
4918
+ movDropboxArr.push({
4919
+ from_path: `${path_src}/${item}`,
4920
+ to_path: `${path_dest}/${newItem}`,
4921
+ });
4922
+ newArr.push(newItem);
4923
+ } catch (e) {
4924
+ console.log(e);
4925
+ }
4926
+ });
4927
+ data[key] = Util.array_to_jsonb(newArr);
4928
+ }
4929
+ }*/
4930
+ myCache.del(name);
4858
4931
  }
4859
4932
  }
4860
4933
  let result = await connection.result({
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zet-lib",
3
- "version": "1.4.15",
3
+ "version": "1.4.17",
4
4
  "description": "zet is a library that part of zet generator.",
5
5
  "engines": {
6
6
  "node": ">=18"