zet-lib 1.5.0 → 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.
Files changed (2) hide show
  1. package/lib/zAppRouter.js +151 -179
  2. package/package.json +1 -1
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,140 +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 = {};
1876
- try {
1877
- const room = res.locals.token;
1878
- const index = req.params.index;
1879
- const { fileName, table, field } = req.body;
1880
- let dir = `/${table}/${field}`;
1881
- let pathFile = `${dir}/${fileName}`;
1882
- console.log(pathFile);
1883
- let jsonMessage = await zDropbox.file(fileName, table, field, index);
1884
- if (jsonMessage.status == 1) {
1885
- res.json(jsonMessage.datas);
1886
- } else {
1887
- setTimeout(async () => {
1888
- jsonMessage = await zDropbox.file(fileName, table, field, index);
1889
- if (jsonMessage.status == 1) {
1890
- res.json(jsonMessage.datas);
1891
- } else {
1892
- io.to(room).emit(
1893
- "errormessage",
1894
- `Please reload your browser !! Error ${pathFile} not exist in dropbox, `
1895
- );
1896
- }
1897
- }, 4000);
1898
- }
1899
- } catch (e) {
1900
- console.log(e);
1901
- res.json(datas);
1863
+ router.post("/zdropbox-file/:index", async (req, res) => {
1864
+ let datas = {};
1865
+ try {
1866
+ const room = res.locals.token;
1867
+ const index = req.params.index;
1868
+ const { fileName, table, field } = req.body;
1869
+ let dir = `/${table}/${field}`;
1870
+ let pathFile = `${dir}/${fileName}`;
1871
+ console.log(pathFile);
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);
1902
1887
  }
1888
+ } catch (e) {
1889
+ console.log(e);
1890
+ res.json(datas);
1903
1891
  }
1904
- );
1892
+ });
1905
1893
 
1906
1894
  // Delete file dicache dulu kemudian submit delete dropbox file
1907
- router.post(
1908
- "/zdropbox-remove/",
1909
- zDropbox.handleTokenRefresh,
1910
- async (req, res) => {
1911
- try {
1912
- let body = req.body;
1913
- //console.log(body);
1914
- const userId = res.locals.userId;
1915
- const fileName = body.file;
1916
- let cname = body.cname.replace("ZUSER___ID", userId);
1917
- let cacheName = cname;
1918
- //console.log(cacheName);
1919
- let splits = cname.split("__");
1920
- let table = splits[2];
1921
- let field = splits[3];
1922
- let type = splits[4];
1923
- const dir = `/temps/${table}/${field}/${userId}`;
1924
- await zDropbox.ensureFolder(dir);
1925
- const filePath = `${dir}/${fileName}`;
1926
- let arr = [];
1927
- if (type != "create") {
1928
- cacheName = cacheName + "__remove";
1929
- }
1930
- if (myCache.has(cacheName)) {
1931
- arr = myCache.get(cacheName);
1932
- }
1933
- //console.log(cacheName)
1934
- if (type == "create") {
1935
- try {
1936
- await dbx.filesDeleteV2({
1937
- path: filePath,
1938
- });
1939
- arr = Util.arrayDelete(arr, body.file);
1940
- myCache.set(cacheName, arr);
1941
- } catch (e) {
1942
- console.log(e);
1943
- }
1944
- } else {
1945
- 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);
1946
1925
  myCache.set(cacheName, arr);
1926
+ } catch (e) {
1927
+ console.log(e);
1947
1928
  }
1948
- res.json({ success: true });
1949
- } catch (error) {
1950
- console.error("Error deleting file:", error);
1951
- res.status(500).json({ error: "Error deleting file" });
1929
+ } else {
1930
+ arr.push(body.file);
1931
+ myCache.set(cacheName, arr);
1952
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" });
1953
1937
  }
1954
- );
1938
+ });
1955
1939
 
1956
1940
  // Endpoint to check token status
1957
- router.get(
1958
- "/zdropbox-check-token",
1959
- zDropbox.handleTokenRefresh,
1960
- async (req, res) => {
1961
- try {
1962
- await dbx.usersGetCurrentAccount();
1963
- res.json({ status: "valid" });
1964
- } catch (error) {
1965
- res.status(401).json({ status: "invalid" });
1966
- }
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" });
1967
1947
  }
1968
- );
1948
+ });
1969
1949
 
1970
1950
  // Get temporary link for a file
1971
- router.post(
1972
- "/zdropbox-get-temp-link",
1973
- zDropbox.handleTokenRefresh,
1974
- async (req, res) => {
1975
- try {
1976
- const response = await dbx.filesGetTemporaryLink({
1977
- path: req.body.path,
1978
- });
1979
- res.json({ link: response.result.link });
1980
- } catch (error) {
1981
- console.error("Error getting temporary link:", error);
1982
- res.status(500).json({ error: "Error getting temporary link" });
1983
- }
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" });
1984
1960
  }
1985
- );
1961
+ });
1986
1962
 
1987
- router.get(
1988
- "/zdropbox-view/:table/:field/:item",
1989
- zDropbox.handleTokenRefresh,
1990
- async (req, res) => {
1991
- try {
1992
- let item = req.params.item;
1993
- item = item.substring(13);
1994
- let filePath = `/${req.params.table}/${req.params.field}/${req.params.item}`;
1995
- const response = await dbx.filesGetTemporaryLink({
1996
- path: filePath,
1997
- });
1998
- res.send(`<img src="${response.result.link}">`);
1999
- } catch (e) {
2000
- console.log(e);
2001
- res.send(e);
2002
- }
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);
2003
1975
  }
2004
- );
1976
+ });
2005
1977
 
2006
1978
  // Download and zip files endpoint
2007
1979
  router.get("/zdownloads-dropbox/:table/:field/:id", async (req, res) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zet-lib",
3
- "version": "1.5.0",
3
+ "version": "1.5.1",
4
4
  "description": "zet is a library that part of zet generator.",
5
5
  "engines": {
6
6
  "node": ">=18"