zet-lib 1.4.16 → 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
@@ -2008,19 +2008,14 @@ router.post("/zdropbox/:table/:field", handleTokenRefresh, async (req, res) => {
2008
2008
  router.post("/zdropbox-attributes", handleTokenRefresh, async (req, res) => {
2009
2009
  try {
2010
2010
  let userId = res.locals.userId;
2011
- let dir = `${dirRoot}/public/zdropzone/${userId}`;
2012
- if (!fs.existsSync(dir)) {
2013
- fs.mkdirSync(dir, { recursive: true });
2014
- }
2015
2011
  let body = req.body;
2012
+ console.log(body);
2016
2013
  let category = body.category;
2017
2014
  let name = `dropbox__${userId}__${body.table}__${body.field}__${body.type}`;
2018
- //dropzone__${res.locals.userId}__${table}__${key}__create
2019
2015
  let arr = myCache.has(name) ? myCache.get(name) : [];
2020
2016
  if (category === "add") {
2021
2017
  arr.push(body.file);
2022
2018
  } else {
2023
- name = `dropzone__${userId}__${body.table}__${body.field}__${body.type}`;
2024
2019
  if (myCache.has(name)) {
2025
2020
  arr = myCache.get(name);
2026
2021
  }
@@ -2035,51 +2030,77 @@ router.post("/zdropbox-attributes", handleTokenRefresh, async (req, res) => {
2035
2030
  });
2036
2031
 
2037
2032
  //get all files and meta in dropbox
2038
- router.post("/zdropbox-files", handleTokenRefresh, async (req, res) => {
2033
+ router.post("/zdropbox-files", async (req, res) => {
2039
2034
  let datas = [];
2040
2035
  const room = res.locals.token;
2041
2036
  try {
2042
2037
  const { files, table, field } = req.body;
2043
2038
  let dir = `/${table}/${field}`;
2044
2039
  for (file of files) {
2045
- let pathFile = `${dir}/${file.fileName}`;
2046
- //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 = ``;
2047
2073
  try {
2048
- const metadata = await dbx.filesGetMetadata({
2074
+ const response = await dbx.filesGetTemporaryLink({
2049
2075
  path: pathFile,
2050
2076
  });
2051
- let link = ``;
2052
- try {
2053
- const response = await dbx.filesGetTemporaryLink({
2054
- path: pathFile,
2055
- });
2056
- link = response.result.link;
2057
- } catch (error) {
2058
- console.error("Error getting temporary link:", error);
2059
- io.to(room).emit(
2060
- "errormessage",
2061
- `Error getting temporary link: ${error.message}`
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,32 +2109,16 @@ router.post("/zdropbox-files", handleTokenRefresh, async (req, res) => {
2088
2109
  res.json(datas);
2089
2110
  });
2090
2111
 
2091
- /*
2092
- let userId = res.locals.userId;
2093
- let cacheName = req.body.cname.replace("ZUSER___ID", userId);
2094
- let dir = `${dirRoot}/public/zdropzone/${userId}`;
2095
- if (!fs.existsSync(dir)) {
2096
- fs.mkdirSync(dir, { recursive: true });
2097
- }
2098
- let filename = `${dirRoot}/public/zdropzone/${userId}/${req.body.file}`;
2099
- if (Util.fileExist(filename)) {
2100
- await fs.unlink(filename);
2101
- if (myCache.has(cacheName)) {
2102
- arr = myCache.get(name);
2103
- }
2104
- arr = Util.arrayDelete(arr, body.file);
2105
- myCache.set(name, arr);
2106
- }
2107
- res.json("ok");
2108
- */
2109
- // Delete file dropbox
2112
+ // Delete file dicache dulu kemudian submit delete dropbox file
2110
2113
  router.post("/zdropbox-remove/", handleTokenRefresh, async (req, res) => {
2111
2114
  try {
2112
2115
  let body = req.body;
2116
+ //console.log(body);
2113
2117
  const userId = res.locals.userId;
2114
2118
  const fileName = body.file;
2115
2119
  let cname = body.cname.replace("ZUSER___ID", userId);
2116
2120
  let cacheName = cname;
2121
+ //console.log(cacheName);
2117
2122
  let splits = cname.split("__");
2118
2123
  let table = splits[2];
2119
2124
  let field = splits[3];
@@ -2122,19 +2127,27 @@ router.post("/zdropbox-remove/", handleTokenRefresh, async (req, res) => {
2122
2127
  await ensureFolder(dir);
2123
2128
  const filePath = `${dir}/${fileName}`;
2124
2129
  let arr = [];
2130
+ if (type != "create") {
2131
+ cacheName = cacheName + "__remove";
2132
+ }
2125
2133
  if (myCache.has(cacheName)) {
2126
2134
  arr = myCache.get(cacheName);
2127
2135
  }
2128
- try {
2129
- await dbx.filesDeleteV2({
2130
- path: filePath,
2131
- });
2132
- 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);
2133
2149
  myCache.set(cacheName, arr);
2134
- } catch (e) {
2135
- console.log(e);
2136
2150
  }
2137
-
2138
2151
  res.json({ success: true });
2139
2152
  } catch (error) {
2140
2153
  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);
@@ -4814,6 +4816,7 @@ zRoute.updateSQL = async (req, res, table, data, whereData) => {
4814
4816
  let hasImages = false;
4815
4817
  let tables = [];
4816
4818
  let tableFields = {};
4819
+ let movDropboxArr = [];
4817
4820
  for (let key in MYMODEL.widgets) {
4818
4821
  if (MYMODEL.widgets[key].name == "table") {
4819
4822
  tableFields[key] = [];
@@ -4855,8 +4858,102 @@ zRoute.updateSQL = async (req, res, table, data, whereData) => {
4855
4858
  });
4856
4859
  data[key] = Util.array_to_jsonb(newArr);
4857
4860
  }
4861
+ } else if (MYMODEL.widgets[key].name === "dropbox") {
4862
+ // Initialize Dropbox client
4863
+ let dbx = new Dropbox({
4864
+ accessToken: process.env.DROPBOX_ACCESS_TOKEN,
4865
+ refreshToken: process.env.DROPBOX_REFRESH_TOKEN,
4866
+ clientId: process.env.DROPBOX_CLIENT_ID,
4867
+ clientSecret: process.env.DROPBOX_CLIENT_SECRET,
4868
+ fetch: fetch,
4869
+ });
4870
+ //console.log('has dropbox')
4871
+ let dir1 = `/${MYMODEL.table}/${key}`;
4872
+ let dir2 = `/temps/${MYMODEL.table}/${key}/${userId}`;
4873
+ try {
4874
+ await dbx.filesCreateFolderV2({
4875
+ path: dir1,
4876
+ autorename: false,
4877
+ });
4878
+ } catch (error) {
4879
+ if (error.status !== 409) {
4880
+ // 409 means folder already exists
4881
+ //throw error;
4882
+ }
4883
+ }
4884
+ let name = `dropbox__${userId}__${MYMODEL.table}__${key}__${whereData.id}`;
4885
+ if (myCache.has(name)) {
4886
+ let arr = myCache.get(name) || [];
4887
+ let newArr = [];
4888
+ let time = new Date().getTime();
4889
+ for (const item of arr) {
4890
+ try {
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);
4919
+ }
4920
+ }
4921
+ }
4922
+ data[key] = Util.array_to_jsonb(newArr);
4923
+ }
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);
4937
+ try {
4938
+ await dbx.filesDeleteV2({
4939
+ path: `${dir2}/${item}`,
4940
+ });
4941
+ } catch (e) {
4942
+ console.log(e);
4943
+ }
4944
+ }
4945
+ }
4946
+ }
4858
4947
  }
4859
4948
  }
4949
+
4950
+ //dropbox
4951
+ if (movDropboxArr.length > 0) {
4952
+ setTimeout(function () {
4953
+ zRoute.moveDropbox(movDropboxArr);
4954
+ }, 3000);
4955
+ }
4956
+
4860
4957
  let result = await connection.result({
4861
4958
  table: MYMODEL.table,
4862
4959
  where: whereData,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zet-lib",
3
- "version": "1.4.16",
3
+ "version": "1.4.18",
4
4
  "description": "zet is a library that part of zet generator.",
5
5
  "engines": {
6
6
  "node": ">=18"