zet-lib 1.4.20 → 1.4.21

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 +117 -10
  2. package/package.json +1 -1
package/lib/zAppRouter.js CHANGED
@@ -1765,16 +1765,15 @@ router.post("/zcompress-dropzone", async (req, res) => {
1765
1765
  },
1766
1766
  });
1767
1767
  //check for temp dir
1768
-
1769
1768
  if (result.lock == 1) {
1770
1769
  let message = `Data has been locked, please unlock first to compress images `;
1771
1770
  io.to(room).emit("errormessage", message);
1772
1771
  res.json(Util.flashError(message));
1773
1772
  } else {
1774
1773
  const config = {
1775
- jpeg: { quality: 50, compressionLevel: 5 },
1776
- webp: { quality: 50, compressionLevel: 5 },
1777
- png: { quality: 50, compressionLevel: 5 },
1774
+ jpeg: { quality: 60, compressionLevel: 9 },
1775
+ webp: { quality: 60, compressionLevel: 9 },
1776
+ png: { quality: 60, compressionLevel: 9 },
1778
1777
  };
1779
1778
  const wait = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
1780
1779
  const timeExcecute = 200;
@@ -1806,7 +1805,12 @@ router.post("/zcompress-dropzone", async (req, res) => {
1806
1805
  await wait(timeExcecute); // Wait for file system
1807
1806
  let image;
1808
1807
  try {
1809
- image = sharp(tempPath);
1808
+ image = sharp(tempPath).resize({
1809
+ width: 1024,
1810
+ height: null, // Maintain aspect ratio
1811
+ fit: "inside",
1812
+ withoutEnlargement: true, // Don't enlarge if image is smaller than 1024px
1813
+ });
1810
1814
  } catch (error) {
1811
1815
  console.log(`Skipping ${filename} - not a valid image file`);
1812
1816
  io.to(room).emit(
@@ -1971,7 +1975,6 @@ router.post("/zdropbox/:table/:field", handleTokenRefresh, async (req, res) => {
1971
1975
  if (!req.files || !req.files.file) {
1972
1976
  return res.status(400).json({ error: "No file uploaded" });
1973
1977
  }
1974
-
1975
1978
  const userId = res.locals.userId;
1976
1979
  const table = req.params.table;
1977
1980
  const field = req.params.field;
@@ -1981,10 +1984,9 @@ router.post("/zdropbox/:table/:field", handleTokenRefresh, async (req, res) => {
1981
1984
  const dir = `/temps/${table}/${field}/${userId}`;
1982
1985
  await ensureFolder(dir);
1983
1986
  const filePath = `${dir}/${fileName}`;
1984
-
1985
- console.log("Uploading file to path:", filePath);
1987
+ /*console.log("Uploading file to path:", filePath);
1986
1988
  console.log("File size:", file.size);
1987
- console.log("File name:", fileName);
1989
+ console.log("File name:", fileName);*/
1988
1990
 
1989
1991
  const response = await dbx.filesUpload({
1990
1992
  path: filePath,
@@ -1994,7 +1996,6 @@ router.post("/zdropbox/:table/:field", handleTokenRefresh, async (req, res) => {
1994
1996
  });
1995
1997
  res.json("ok");
1996
1998
  } catch (error) {
1997
- console.error("Upload error:", error);
1998
1999
  console.error("Error details:", {
1999
2000
  message: error.message,
2000
2001
  stack: error.stack,
@@ -2268,4 +2269,110 @@ router.get("/zdownloads-dropbox/:table/:field/:id", async (req, res) => {
2268
2269
  }
2269
2270
  });
2270
2271
 
2272
+ //compress image di dropzone
2273
+ router.post("/zcompress-dropbox", async (req, res) => {
2274
+ let table = req.body.table;
2275
+ let field = req.body.field;
2276
+ let id = req.body.id;
2277
+ const room = res.locals.token;
2278
+ let userId = res.locals.userId;
2279
+ try {
2280
+ let result = await connection.result({
2281
+ table: table,
2282
+ where: {
2283
+ id: id,
2284
+ },
2285
+ });
2286
+ //check for temp dir
2287
+ if (result.lock == 1) {
2288
+ let message = `Data has been locked, please unlock first to compress images `;
2289
+ io.to(room).emit("errormessage", message);
2290
+ res.json(Util.flashError(message));
2291
+ } else {
2292
+ let arr = [];
2293
+ let files = result[field];
2294
+ const count = files.length || 0;
2295
+ let i = 0;
2296
+ for (const file of files) {
2297
+ const filePath = `/${table}/${field}/${file}`;
2298
+ let ext = file.toLowerCase().split(".").pop();
2299
+ if (ext == "png" || ext == "jpg" || ext == "jpeg") {
2300
+ try {
2301
+ // Download the file from Dropbox
2302
+ const response = await dbx.filesDownload({
2303
+ path: filePath,
2304
+ });
2305
+ // Get the file name from the path
2306
+ const fileName = file;
2307
+ // Process the image with sharp
2308
+ const processedImage = await sharp(response.result.fileBinary)
2309
+ .resize({
2310
+ width: 1024,
2311
+ height: null, // Maintain aspect ratio
2312
+ fit: "inside",
2313
+ withoutEnlargement: true, // Don't enlarge if image is smaller than 1024px
2314
+ })
2315
+ .toBuffer()
2316
+ .then((buffer) => {
2317
+ // Check if the file is PNG
2318
+ if (fileName.toLowerCase().endsWith(".png")) {
2319
+ return sharp(buffer)
2320
+ .png({
2321
+ quality: 60,
2322
+ compressionLevel: 9, // Maximum compression for PNG
2323
+ })
2324
+ .toBuffer();
2325
+ } else {
2326
+ // For JPEG files
2327
+ return sharp(buffer)
2328
+ .jpeg({
2329
+ quality: 60,
2330
+ mozjpeg: true, // Better compression
2331
+ })
2332
+ .toBuffer();
2333
+ }
2334
+ });
2335
+
2336
+ // Upload the compressed image back to Dropbox
2337
+ await dbx.filesUpload({
2338
+ path: filePath,
2339
+ contents: processedImage,
2340
+ mode: { ".tag": "overwrite" }, // This will replace the existing file
2341
+ });
2342
+ io.to(room).emit("progress", {
2343
+ value: Math.round(((i + 1) / count) * 100),
2344
+ style: `progress-bar progress-bar-striped bg-success`,
2345
+ data: `Compress ${fileName} Successfully..`,
2346
+ });
2347
+ } catch (error) {
2348
+ console.error(`Error processing file ${filePath}:`, error);
2349
+ io.to(room).emit("progress", {
2350
+ value: Math.round(((i + 1) / count) * 100),
2351
+ style: `progress-bar progress-bar-striped bg-danger`,
2352
+ data: `Error Compress ${fileName} ${error.message}..`,
2353
+ });
2354
+ continue;
2355
+ }
2356
+ } else {
2357
+ io.to(room).emit("progress", {
2358
+ value: Math.round(((i + 1) / count) * 100),
2359
+ style: `progress-bar progress-bar-striped bg-danger`,
2360
+ data: `Skip Compress ${fileName} skip..`,
2361
+ });
2362
+ }
2363
+ i++;
2364
+ }
2365
+ }
2366
+ } catch (e) {
2367
+ console.log(e);
2368
+ res.json(e + "");
2369
+ }
2370
+ io.to(room).emit("progress", {
2371
+ value: 100,
2372
+ style: `progress-bar progress-bar-striped bg-success`,
2373
+ data: `Compress all images complete...`,
2374
+ });
2375
+ res.json(Util.jsonSuccess("Compress all images completed..."));
2376
+ });
2377
+
2271
2378
  module.exports = router;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zet-lib",
3
- "version": "1.4.20",
3
+ "version": "1.4.21",
4
4
  "description": "zet is a library that part of zet generator.",
5
5
  "engines": {
6
6
  "node": ">=18"