zet-lib 1.4.28 → 1.5.1

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
@@ -1769,75 +1769,67 @@ const delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
1769
1769
 
1770
1770
  let dbx = zDropbox.dbx;
1771
1771
  // Upload endpoint
1772
- router.post(
1773
- "/zdropbox/:table/:field",
1774
- zDropbox.handleTokenRefresh,
1775
- async (req, res) => {
1776
- try {
1777
- if (!req.files || !req.files.file) {
1778
- return res.status(400).json({ error: "No file uploaded" });
1779
- }
1780
- const userId = res.locals.userId;
1781
- const table = req.params.table;
1782
- const field = req.params.field;
1783
- const file = req.files.file;
1784
- const fileName = file.name;
1785
- //ensure folder
1786
- const dir = `/temps/${table}/${field}/${userId}`;
1787
- await zDropbox.ensureFolder(dir);
1788
- const filePath = `${dir}/${fileName}`;
1789
- /*console.log("Uploading file to path:", filePath);
1772
+ router.post("/zdropbox/:table/:field", async (req, res) => {
1773
+ try {
1774
+ if (!req.files || !req.files.file) {
1775
+ return res.status(400).json({ error: "No file uploaded" });
1776
+ }
1777
+ const userId = res.locals.userId;
1778
+ const table = req.params.table;
1779
+ const field = req.params.field;
1780
+ const file = req.files.file;
1781
+ const fileName = file.name;
1782
+ //ensure folder
1783
+ const dir = `/temps/${table}/${field}/${userId}`;
1784
+ await zDropbox.ensureFolder(dir);
1785
+ const filePath = `${dir}/${fileName}`;
1786
+ /*console.log("Uploading file to path:", filePath);
1790
1787
  console.log("File size:", file.size);
1791
1788
  console.log("File name:", fileName);*/
1792
1789
 
1793
- const response = await dbx.filesUpload({
1794
- path: filePath,
1795
- contents: file.data,
1796
- mode: { ".tag": "add" },
1797
- autorename: true,
1798
- });
1799
- res.json("ok");
1800
- } catch (error) {
1801
- console.error("Error details:", {
1802
- message: error.message,
1803
- stack: error.stack,
1804
- response: error.response,
1805
- });
1806
- res.status(500).json({
1807
- error: "Failed to upload file",
1808
- details: error.message,
1809
- });
1810
- }
1790
+ const response = await dbx.filesUpload({
1791
+ path: filePath,
1792
+ contents: file.data,
1793
+ mode: { ".tag": "add" },
1794
+ autorename: true,
1795
+ });
1796
+ res.json("ok");
1797
+ } catch (error) {
1798
+ console.error("Error details:", {
1799
+ message: error.message,
1800
+ stack: error.stack,
1801
+ response: error.response,
1802
+ });
1803
+ res.status(500).json({
1804
+ error: "Failed to upload file",
1805
+ details: error.message,
1806
+ });
1811
1807
  }
1812
- );
1808
+ });
1813
1809
 
1814
- router.post(
1815
- "/zdropbox-attributes",
1816
- zDropbox.handleTokenRefresh,
1817
- async (req, res) => {
1818
- try {
1819
- let userId = res.locals.userId;
1820
- let body = req.body;
1821
- console.log(body);
1822
- let category = body.category;
1823
- let name = `dropbox__${userId}__${body.table}__${body.field}__${body.type}`;
1824
- let arr = myCache.has(name) ? myCache.get(name) : [];
1825
- if (category === "add") {
1826
- arr.push(body.file);
1827
- } else {
1828
- if (myCache.has(name)) {
1829
- arr = myCache.get(name);
1830
- }
1831
- arr = Util.arrayDelete(arr, body.file);
1810
+ router.post("/zdropbox-attributes", async (req, res) => {
1811
+ try {
1812
+ let userId = res.locals.userId;
1813
+ let body = req.body;
1814
+ console.log(body);
1815
+ let category = body.category;
1816
+ let name = `dropbox__${userId}__${body.table}__${body.field}__${body.type}`;
1817
+ let arr = myCache.has(name) ? myCache.get(name) : [];
1818
+ if (category === "add") {
1819
+ arr.push(body.file);
1820
+ } else {
1821
+ if (myCache.has(name)) {
1822
+ arr = myCache.get(name);
1832
1823
  }
1833
- myCache.set(name, arr);
1834
- res.json("ok");
1835
- } catch (e) {
1836
- console.log(e);
1837
- res.status(500).send(e + "");
1824
+ arr = Util.arrayDelete(arr, body.file);
1838
1825
  }
1826
+ myCache.set(name, arr);
1827
+ res.json("ok");
1828
+ } catch (e) {
1829
+ console.log(e);
1830
+ res.status(500).send(e + "");
1839
1831
  }
1840
- );
1832
+ });
1841
1833
 
1842
1834
  //get all files and meta in dropbox
1843
1835
  router.post("/zdropbox-files", async (req, res) => {
@@ -1868,179 +1860,120 @@ router.post("/zdropbox-files", async (req, res) => {
1868
1860
  res.json(datas);
1869
1861
  });
1870
1862
 
1871
- router.post(
1872
- "/zdropbox-file/:index",
1873
- zDropbox.handleTokenRefresh,
1874
- async (req, res) => {
1875
- let datas = {};
1863
+ router.post("/zdropbox-file/:index", async (req, res) => {
1864
+ let datas = {};
1865
+ try {
1876
1866
  const room = res.locals.token;
1867
+ const index = req.params.index;
1877
1868
  const { fileName, table, field } = req.body;
1878
1869
  let dir = `/${table}/${field}`;
1879
1870
  let pathFile = `${dir}/${fileName}`;
1880
1871
  console.log(pathFile);
1881
- try {
1882
- const metadata = await dbx.filesGetMetadata({
1883
- path: pathFile,
1884
- });
1885
- let link = ``;
1886
- try {
1887
- const response = await dbx.filesGetTemporaryLink({
1888
- path: pathFile,
1889
- });
1890
- link = response.result.link;
1891
- } catch (error) {
1892
- setTimeout(async () => {
1893
- try {
1894
- const response = await dbx.filesGetTemporaryLink({
1895
- path: pathFile,
1896
- });
1897
- link = response.result.link;
1898
- const mockFile = {
1899
- name: metadata.result.name,
1900
- id: metadata.result,
1901
- path: metadata.result.path_display,
1902
- link: link,
1903
- isImage: metadata.result.name.match(/\.(jpg|jpeg|png|gif)$/i),
1904
- size: metadata.result.size,
1905
- modified: metadata.result.server_modified,
1906
- accepted: true,
1907
- index: req.params.index,
1908
- };
1909
- //console.log(mockFile)
1910
- datas = mockFile;
1911
- res.json(datas);
1912
- } catch (e) {
1913
- console.error("Error getting temporary link:", error);
1914
- io.to(room).emit(
1915
- "errormessage",
1916
- `Please reload your browser, Error getting temporary link: ${error.message}`
1917
- );
1918
- }
1919
- }, 2000);
1920
- }
1921
- const mockFile = {
1922
- name: metadata.result.name,
1923
- id: metadata.result,
1924
- path: metadata.result.path_display,
1925
- link: link,
1926
- isImage: metadata.result.name.match(/\.(jpg|jpeg|png|gif)$/i),
1927
- size: metadata.result.size,
1928
- modified: metadata.result.server_modified,
1929
- accepted: true,
1930
- index: req.params.index,
1931
- };
1932
- //console.log(mockFile)
1933
- datas = mockFile;
1934
- } catch (e) {
1935
- console.log(e);
1936
- io.to(room).emit(
1937
- "errormessage",
1938
- `${e.message} Error ${pathFile} not exist in dropbox, Please reload your browser !!`
1939
- );
1872
+ let jsonMessage = await zDropbox.file(fileName, table, field, index);
1873
+ if (jsonMessage.status == 1) {
1874
+ res.json(jsonMessage.datas);
1875
+ } else {
1876
+ setTimeout(async () => {
1877
+ jsonMessage = await zDropbox.file(fileName, table, field, index);
1878
+ if (jsonMessage.status == 1) {
1879
+ res.json(jsonMessage.datas);
1880
+ } else {
1881
+ io.to(room).emit(
1882
+ "errormessage",
1883
+ `Please reload your browser !! Error ${pathFile} not exist in dropbox, `
1884
+ );
1885
+ }
1886
+ }, 4000);
1940
1887
  }
1888
+ } catch (e) {
1889
+ console.log(e);
1941
1890
  res.json(datas);
1942
1891
  }
1943
- );
1892
+ });
1944
1893
 
1945
1894
  // Delete file dicache dulu kemudian submit delete dropbox file
1946
- router.post(
1947
- "/zdropbox-remove/",
1948
- zDropbox.handleTokenRefresh,
1949
- async (req, res) => {
1950
- try {
1951
- let body = req.body;
1952
- //console.log(body);
1953
- const userId = res.locals.userId;
1954
- const fileName = body.file;
1955
- let cname = body.cname.replace("ZUSER___ID", userId);
1956
- let cacheName = cname;
1957
- //console.log(cacheName);
1958
- let splits = cname.split("__");
1959
- let table = splits[2];
1960
- let field = splits[3];
1961
- let type = splits[4];
1962
- const dir = `/temps/${table}/${field}/${userId}`;
1963
- await zDropbox.ensureFolder(dir);
1964
- const filePath = `${dir}/${fileName}`;
1965
- let arr = [];
1966
- if (type != "create") {
1967
- cacheName = cacheName + "__remove";
1968
- }
1969
- if (myCache.has(cacheName)) {
1970
- arr = myCache.get(cacheName);
1971
- }
1972
- //console.log(cacheName)
1973
- if (type == "create") {
1974
- try {
1975
- await dbx.filesDeleteV2({
1976
- path: filePath,
1977
- });
1978
- arr = Util.arrayDelete(arr, body.file);
1979
- myCache.set(cacheName, arr);
1980
- } catch (e) {
1981
- console.log(e);
1982
- }
1983
- } else {
1984
- arr.push(body.file);
1895
+ router.post("/zdropbox-remove/", async (req, res) => {
1896
+ try {
1897
+ let body = req.body;
1898
+ //console.log(body);
1899
+ const userId = res.locals.userId;
1900
+ const fileName = body.file;
1901
+ let cname = body.cname.replace("ZUSER___ID", userId);
1902
+ let cacheName = cname;
1903
+ //console.log(cacheName);
1904
+ let splits = cname.split("__");
1905
+ let table = splits[2];
1906
+ let field = splits[3];
1907
+ let type = splits[4];
1908
+ const dir = `/temps/${table}/${field}/${userId}`;
1909
+ await zDropbox.ensureFolder(dir);
1910
+ const filePath = `${dir}/${fileName}`;
1911
+ let arr = [];
1912
+ if (type != "create") {
1913
+ cacheName = cacheName + "__remove";
1914
+ }
1915
+ if (myCache.has(cacheName)) {
1916
+ arr = myCache.get(cacheName);
1917
+ }
1918
+ //console.log(cacheName)
1919
+ if (type == "create") {
1920
+ try {
1921
+ await dbx.filesDeleteV2({
1922
+ path: filePath,
1923
+ });
1924
+ arr = Util.arrayDelete(arr, body.file);
1985
1925
  myCache.set(cacheName, arr);
1926
+ } catch (e) {
1927
+ console.log(e);
1986
1928
  }
1987
- res.json({ success: true });
1988
- } catch (error) {
1989
- console.error("Error deleting file:", error);
1990
- res.status(500).json({ error: "Error deleting file" });
1929
+ } else {
1930
+ arr.push(body.file);
1931
+ myCache.set(cacheName, arr);
1991
1932
  }
1933
+ res.json({ success: true });
1934
+ } catch (error) {
1935
+ console.error("Error deleting file:", error);
1936
+ res.status(500).json({ error: "Error deleting file" });
1992
1937
  }
1993
- );
1938
+ });
1994
1939
 
1995
1940
  // Endpoint to check token status
1996
- router.get(
1997
- "/zdropbox-check-token",
1998
- zDropbox.handleTokenRefresh,
1999
- async (req, res) => {
2000
- try {
2001
- await dbx.usersGetCurrentAccount();
2002
- res.json({ status: "valid" });
2003
- } catch (error) {
2004
- res.status(401).json({ status: "invalid" });
2005
- }
1941
+ router.get("/zdropbox-check-token", async (req, res) => {
1942
+ try {
1943
+ await dbx.usersGetCurrentAccount();
1944
+ res.json({ status: "valid" });
1945
+ } catch (error) {
1946
+ res.status(401).json({ status: "invalid" });
2006
1947
  }
2007
- );
1948
+ });
2008
1949
 
2009
1950
  // Get temporary link for a file
2010
- router.post(
2011
- "/zdropbox-get-temp-link",
2012
- zDropbox.handleTokenRefresh,
2013
- async (req, res) => {
2014
- try {
2015
- const response = await dbx.filesGetTemporaryLink({
2016
- path: req.body.path,
2017
- });
2018
- res.json({ link: response.result.link });
2019
- } catch (error) {
2020
- console.error("Error getting temporary link:", error);
2021
- res.status(500).json({ error: "Error getting temporary link" });
2022
- }
1951
+ router.post("/zdropbox-get-temp-link", async (req, res) => {
1952
+ try {
1953
+ const response = await dbx.filesGetTemporaryLink({
1954
+ path: req.body.path,
1955
+ });
1956
+ res.json({ link: response.result.link });
1957
+ } catch (error) {
1958
+ console.error("Error getting temporary link:", error);
1959
+ res.status(500).json({ error: "Error getting temporary link" });
2023
1960
  }
2024
- );
1961
+ });
2025
1962
 
2026
- router.get(
2027
- "/zdropbox-view/:table/:field/:item",
2028
- zDropbox.handleTokenRefresh,
2029
- async (req, res) => {
2030
- try {
2031
- let item = req.params.item;
2032
- item = item.substring(13);
2033
- let filePath = `/${req.params.table}/${req.params.field}/${req.params.item}`;
2034
- const response = await dbx.filesGetTemporaryLink({
2035
- path: filePath,
2036
- });
2037
- res.send(`<img src="${response.result.link}">`);
2038
- } catch (e) {
2039
- console.log(e);
2040
- res.send(e);
2041
- }
1963
+ router.get("/zdropbox-view/:table/:field/:item", async (req, res) => {
1964
+ try {
1965
+ let item = req.params.item;
1966
+ item = item.substring(13);
1967
+ let filePath = `/${req.params.table}/${req.params.field}/${req.params.item}`;
1968
+ const response = await dbx.filesGetTemporaryLink({
1969
+ path: filePath,
1970
+ });
1971
+ res.send(`<img src="${response.result.link}">`);
1972
+ } catch (e) {
1973
+ console.log(e);
1974
+ res.send(e);
2042
1975
  }
2043
- );
1976
+ });
2044
1977
 
2045
1978
  // Download and zip files endpoint
2046
1979
  router.get("/zdownloads-dropbox/:table/:field/:id", async (req, res) => {
package/lib/zDropbox.js CHANGED
@@ -131,4 +131,43 @@ zDropbox.deleteFolder = async (dir) => {
131
131
  }
132
132
  };
133
133
 
134
+ zDropbox.file = async (fileName, table, field, index) => {
135
+ let jsonMessage = Util.flashError("Error");
136
+ let datas = {};
137
+ jsonMessage.datas = datas;
138
+ let dir = `/${table}/${field}`;
139
+ let pathFile = `${dir}/${fileName}`;
140
+ try {
141
+ const metadata = await dbx.filesGetMetadata({
142
+ path: pathFile,
143
+ });
144
+ let link = ``;
145
+ try {
146
+ const response = await dbx.filesGetTemporaryLink({
147
+ path: pathFile,
148
+ });
149
+ link = response.result.link;
150
+ const mockFile = {
151
+ name: metadata.result.name,
152
+ id: metadata.result,
153
+ path: metadata.result.path_display,
154
+ link: link,
155
+ isImage: metadata.result.name.match(/\.(jpg|jpeg|png|gif)$/i),
156
+ size: metadata.result.size,
157
+ modified: metadata.result.server_modified,
158
+ accepted: true,
159
+ index: index,
160
+ };
161
+ //console.log(mockFile)
162
+ jsonMessage = Util.jsonSuccess("Success");
163
+ jsonMessage.datas = mockFile;
164
+ } catch (error) {
165
+ console.log(error);
166
+ }
167
+ } catch (e) {
168
+ console.log(e);
169
+ }
170
+
171
+ return jsonMessage;
172
+ };
134
173
  module.exports = zDropbox;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zet-lib",
3
- "version": "1.4.28",
3
+ "version": "1.5.1",
4
4
  "description": "zet is a library that part of zet generator.",
5
5
  "engines": {
6
6
  "node": ">=18"