zet-lib 1.4.17 → 1.4.18

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
@@ -2113,12 +2113,12 @@ router.post("/zdropbox-file/:index", handleTokenRefresh, async (req, res) => {
2113
2113
  router.post("/zdropbox-remove/", handleTokenRefresh, async (req, res) => {
2114
2114
  try {
2115
2115
  let body = req.body;
2116
- console.log(body);
2116
+ //console.log(body);
2117
2117
  const userId = res.locals.userId;
2118
2118
  const fileName = body.file;
2119
2119
  let cname = body.cname.replace("ZUSER___ID", userId);
2120
2120
  let cacheName = cname;
2121
- console.log(cacheName);
2121
+ //console.log(cacheName);
2122
2122
  let splits = cname.split("__");
2123
2123
  let table = splits[2];
2124
2124
  let field = splits[3];
@@ -2127,19 +2127,27 @@ router.post("/zdropbox-remove/", handleTokenRefresh, async (req, res) => {
2127
2127
  await ensureFolder(dir);
2128
2128
  const filePath = `${dir}/${fileName}`;
2129
2129
  let arr = [];
2130
+ if (type != "create") {
2131
+ cacheName = cacheName + "__remove";
2132
+ }
2130
2133
  if (myCache.has(cacheName)) {
2131
2134
  arr = myCache.get(cacheName);
2132
2135
  }
2133
- try {
2134
- /* await dbx.filesDeleteV2({
2135
- path: filePath,
2136
- });*/
2137
- arr = Util.arrayDelete(arr, body.file);
2136
+ //console.log(cacheName)
2137
+ if (type == "create") {
2138
+ try {
2139
+ await dbx.filesDeleteV2({
2140
+ path: filePath,
2141
+ });
2142
+ arr = Util.arrayDelete(arr, body.file);
2143
+ myCache.set(cacheName, arr);
2144
+ } catch (e) {
2145
+ console.log(e);
2146
+ }
2147
+ } else {
2148
+ arr.push(body.file);
2138
2149
  myCache.set(cacheName, arr);
2139
- } catch (e) {
2140
- console.log(e);
2141
2150
  }
2142
-
2143
2151
  res.json({ success: true });
2144
2152
  } catch (error) {
2145
2153
  console.error("Error deleting file:", error);
package/lib/zRoute.js CHANGED
@@ -4335,7 +4335,7 @@ zRoute.generateJS = (req, res, MYMODEL, relations, zForms = "", data = {}) => {
4335
4335
  imageElement.setAttribute('data-link', result.link);
4336
4336
  $(imageElement).attr('id', result.id.content_hash);
4337
4337
  $(imageElement).find("img").attr("src", result.link);
4338
- if(+result.index == (dropbox_${item}_data.length - 1) ) {
4338
+ if(+result.index == (dropbox_${item}_data.length - 1)) {
4339
4339
  modaldropbox();
4340
4340
  }
4341
4341
  })
@@ -4816,6 +4816,7 @@ zRoute.updateSQL = async (req, res, table, data, whereData) => {
4816
4816
  let hasImages = false;
4817
4817
  let tables = [];
4818
4818
  let tableFields = {};
4819
+ let movDropboxArr = [];
4819
4820
  for (let key in MYMODEL.widgets) {
4820
4821
  if (MYMODEL.widgets[key].name == "table") {
4821
4822
  tableFields[key] = [];
@@ -4858,10 +4859,6 @@ zRoute.updateSQL = async (req, res, table, data, whereData) => {
4858
4859
  data[key] = Util.array_to_jsonb(newArr);
4859
4860
  }
4860
4861
  } 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
4862
  // Initialize Dropbox client
4866
4863
  let dbx = new Dropbox({
4867
4864
  accessToken: process.env.DROPBOX_ACCESS_TOKEN,
@@ -4870,16 +4867,18 @@ zRoute.updateSQL = async (req, res, table, data, whereData) => {
4870
4867
  clientSecret: process.env.DROPBOX_CLIENT_SECRET,
4871
4868
  fetch: fetch,
4872
4869
  });
4873
-
4870
+ //console.log('has dropbox')
4871
+ let dir1 = `/${MYMODEL.table}/${key}`;
4872
+ let dir2 = `/temps/${MYMODEL.table}/${key}/${userId}`;
4874
4873
  try {
4875
4874
  await dbx.filesCreateFolderV2({
4876
- path: path_dest,
4875
+ path: dir1,
4877
4876
  autorename: false,
4878
4877
  });
4879
4878
  } catch (error) {
4880
4879
  if (error.status !== 409) {
4881
4880
  // 409 means folder already exists
4882
- throw error;
4881
+ //throw error;
4883
4882
  }
4884
4883
  }
4885
4884
  let name = `dropbox__${userId}__${MYMODEL.table}__${key}__${whereData.id}`;
@@ -4887,49 +4886,74 @@ zRoute.updateSQL = async (req, res, table, data, whereData) => {
4887
4886
  let arr = myCache.get(name) || [];
4888
4887
  let newArr = [];
4889
4888
  let time = new Date().getTime();
4890
- arr.map((item) => {
4891
- //todo
4892
- //jika item tidak ada di dropbox
4889
+ for (const item of arr) {
4893
4890
  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);
4891
+ //check in dir1
4892
+ let filepath1 = `${dir1}/${item}`;
4893
+ // Get the file metadata to ensure we have the correct path
4894
+ const metadata = await dbx.filesGetMetadata({
4895
+ path: filepath1,
4896
+ });
4897
+ const finalPath = metadata.result.path_display;
4898
+ if (finalPath) {
4899
+ newArr.push(item);
4900
+ }
4901
+ } catch (e) {
4902
+ //check in dir2 temp folder
4903
+ try {
4904
+ let filepath2 = `${dir2}/${item}`;
4905
+ const metadata = await dbx.filesGetMetadata({
4906
+ path: filepath2,
4907
+ });
4908
+ const finalPath = metadata.result.path_display;
4909
+ if (finalPath) {
4910
+ let newItem = Util.cleanString(time + item);
4911
+ newArr.push(newItem);
4912
+ movDropboxArr.push({
4913
+ from_path: `${dir2}/${item}`,
4914
+ to_path: `${dir1}/${newItem}`,
4915
+ });
4916
+ }
4917
+ } catch (e) {
4918
+ console.log(e);
4900
4919
  }
4901
- } else {
4902
- newArr.push(item);
4903
4920
  }
4904
- });
4921
+ }
4905
4922
  data[key] = Util.array_to_jsonb(newArr);
4906
4923
  }
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
4924
+ myCache.del(name);
4925
+
4926
+ let removeName = name + "__remove";
4927
+ if (myCache.has(removeName)) {
4928
+ let removesArr = myCache.get(removeName) || [];
4929
+ for (const item of removesArr) {
4930
+ //hapus item di folder dir1 atau dir2
4931
+ try {
4932
+ await dbx.filesDeleteV2({
4933
+ path: `${dir1}/${item}`,
4934
+ });
4935
+ } catch (e) {
4936
+ console.log(e);
4916
4937
  try {
4917
- let newItem = Util.cleanString(time + item);
4918
- movDropboxArr.push({
4919
- from_path: `${path_src}/${item}`,
4920
- to_path: `${path_dest}/${newItem}`,
4938
+ await dbx.filesDeleteV2({
4939
+ path: `${dir2}/${item}`,
4921
4940
  });
4922
- newArr.push(newItem);
4923
4941
  } catch (e) {
4924
4942
  console.log(e);
4925
4943
  }
4926
- });
4927
- data[key] = Util.array_to_jsonb(newArr);
4944
+ }
4928
4945
  }
4929
- }*/
4930
- myCache.del(name);
4946
+ }
4931
4947
  }
4932
4948
  }
4949
+
4950
+ //dropbox
4951
+ if (movDropboxArr.length > 0) {
4952
+ setTimeout(function () {
4953
+ zRoute.moveDropbox(movDropboxArr);
4954
+ }, 3000);
4955
+ }
4956
+
4933
4957
  let result = await connection.result({
4934
4958
  table: MYMODEL.table,
4935
4959
  where: whereData,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zet-lib",
3
- "version": "1.4.17",
3
+ "version": "1.4.18",
4
4
  "description": "zet is a library that part of zet generator.",
5
5
  "engines": {
6
6
  "node": ">=18"