zet-lib 1.4.6 → 1.4.8
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/Form.js +1 -1
- package/lib/zAppRouter.js +83 -16
- package/lib/zGeneratorRouter.js +983 -843
- package/package.json +1 -1
package/lib/Form.js
CHANGED
|
@@ -580,7 +580,7 @@ Form.field = (obj) => {
|
|
|
580
580
|
countFiles == ""
|
|
581
581
|
? ""
|
|
582
582
|
: `<div class="card-header">${countFiles} <div class="float-end"><span class="icon-small icons-light" title="Download"><img onclick="location.href= '/zdownloads-dropzone/${obj.routeName}/${obj.key}/${obj.dataId}'" class="icons-bg-black gridview icon-image" src="/assets/icons/download.svg"></span> <span class="icon-small icons-light" title="Compress Images"><img onclick="if(window.confirm('Compress Images ?')) {ajaxPost('/zcompress-dropzone',{table:'${obj.routeName}',field:'${obj.key}',id:${obj.dataId}},(data) => toastrForm(data))}" class="icons-bg-black gridextract icon-image" src="/assets/icons/file-zip.svg"></span></div></div>`;
|
|
583
|
-
displayForm = `<div class="card">
|
|
583
|
+
displayForm = `<div id="div-progress"></div><div class="card">
|
|
584
584
|
${bodydropzoneview}
|
|
585
585
|
<div class="card-body">`;
|
|
586
586
|
if (obj.value && obj.value.length > 0) {
|
package/lib/zAppRouter.js
CHANGED
|
@@ -22,6 +22,7 @@ const zFunction = require("./zFunction");
|
|
|
22
22
|
const qs = require("qs");
|
|
23
23
|
const zip = require("express-zip");
|
|
24
24
|
const sharp = require("sharp");
|
|
25
|
+
const path = require("path");
|
|
25
26
|
|
|
26
27
|
/*
|
|
27
28
|
ajax Post
|
|
@@ -1750,41 +1751,107 @@ router.post("/zcompress-dropzone", async (req, res) => {
|
|
|
1750
1751
|
let field = req.body.field;
|
|
1751
1752
|
let id = req.body.id;
|
|
1752
1753
|
const room = res.locals.token;
|
|
1754
|
+
let userId = res.locals.userId;
|
|
1753
1755
|
let result = await connection.result({
|
|
1754
1756
|
table: table,
|
|
1755
1757
|
where: {
|
|
1756
1758
|
id: id,
|
|
1757
1759
|
},
|
|
1758
1760
|
});
|
|
1761
|
+
//check for temp dir
|
|
1762
|
+
|
|
1759
1763
|
if (result.lock == 1) {
|
|
1760
1764
|
let message = `Data has been locked, please unlock first to compress images `;
|
|
1761
1765
|
io.to(room).emit("errormessage", message);
|
|
1762
1766
|
res.json(Util.flashError(message));
|
|
1763
1767
|
} else {
|
|
1764
1768
|
const config = {
|
|
1765
|
-
jpeg: { quality:
|
|
1766
|
-
webp: { quality:
|
|
1767
|
-
png: { compressionLevel:
|
|
1769
|
+
jpeg: { quality: 50, compressionLevel: 5 },
|
|
1770
|
+
webp: { quality: 50, compressionLevel: 5 },
|
|
1771
|
+
png: { quality: 50, compressionLevel: 5 },
|
|
1768
1772
|
};
|
|
1769
|
-
|
|
1773
|
+
const wait = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
1774
|
+
const timeExcecute = 200;
|
|
1775
|
+
const dir = path.join(dirRoot, "public", "uploads", table, field);
|
|
1776
|
+
console.log(dir);
|
|
1777
|
+
const timestamp = Date.now();
|
|
1778
|
+
const tempDir = path.join(
|
|
1779
|
+
dirRoot,
|
|
1780
|
+
"public",
|
|
1781
|
+
"temps",
|
|
1782
|
+
`${table}___${field}___${id}___${userId}___${timestamp}`
|
|
1783
|
+
);
|
|
1784
|
+
if (!fs.existsSync(tempDir)) {
|
|
1785
|
+
fs.mkdirSync(tempDir);
|
|
1786
|
+
}
|
|
1787
|
+
//let dir = `${dirRoot}/public/uploads/${table}/${field}/`;
|
|
1770
1788
|
let arr = [];
|
|
1771
1789
|
let files = result[field];
|
|
1790
|
+
const count = files.length;
|
|
1791
|
+
let i = 0;
|
|
1772
1792
|
for (const file of files) {
|
|
1773
|
-
|
|
1774
|
-
|
|
1775
|
-
|
|
1776
|
-
|
|
1777
|
-
|
|
1778
|
-
|
|
1779
|
-
|
|
1780
|
-
|
|
1781
|
-
|
|
1782
|
-
|
|
1793
|
+
let filename = file.substring(13);
|
|
1794
|
+
let attributes = Util.fileAttribute(dir + file);
|
|
1795
|
+
if (attributes.type == "image") {
|
|
1796
|
+
const filePath = path.join(dir, file);
|
|
1797
|
+
const tempPath = path.join(tempDir, file);
|
|
1798
|
+
// Copy file to temp directory
|
|
1799
|
+
fs.copyFileSync(filePath, tempPath);
|
|
1800
|
+
await wait(timeExcecute); // Wait for file system
|
|
1801
|
+
let image;
|
|
1802
|
+
try {
|
|
1803
|
+
image = sharp(tempPath);
|
|
1804
|
+
} catch (error) {
|
|
1805
|
+
console.log(`Skipping ${filename} - not a valid image file`);
|
|
1806
|
+
io.to(room).emit(
|
|
1807
|
+
"errormessage",
|
|
1808
|
+
`Skipping ${filename} - not a valid image file`
|
|
1809
|
+
);
|
|
1810
|
+
continue;
|
|
1811
|
+
}
|
|
1812
|
+
const meta = await image.metadata();
|
|
1813
|
+
const { format } = meta;
|
|
1814
|
+
if (config[format]) {
|
|
1815
|
+
try {
|
|
1816
|
+
// Compress the image to a buffer
|
|
1817
|
+
const compressedBuffer = await image[format](
|
|
1818
|
+
config[format]
|
|
1819
|
+
).toBuffer();
|
|
1820
|
+
await wait(timeExcecute); // Wait for file system
|
|
1821
|
+
// Write the compressed buffer to the original file
|
|
1822
|
+
fs.writeFileSync(filePath, compressedBuffer);
|
|
1823
|
+
console.log(`Compressed ${filename} successfully`);
|
|
1824
|
+
//io.to(room).emit('message',`Compressed ${filename} successfully`);
|
|
1825
|
+
io.to(room).emit("progress", {
|
|
1826
|
+
value: Math.round(((i + 1) / count) * 100),
|
|
1827
|
+
style: `progress-bar progress-bar-striped bg-success`,
|
|
1828
|
+
data: `Compressed ${filename} successfully`,
|
|
1829
|
+
});
|
|
1830
|
+
} catch (error) {
|
|
1831
|
+
console.log(`Error compressing ${filename}: ${error.message}`);
|
|
1832
|
+
//io.to(room).emit('errormessage',`Error compressing ${filename}: ${error.message}`);
|
|
1833
|
+
io.to(room).emit("progress", {
|
|
1834
|
+
value: Math.round(((i + 1) / count) * 100),
|
|
1835
|
+
data: `Error compressing ${filename}: ${error.message}`,
|
|
1836
|
+
});
|
|
1837
|
+
}
|
|
1838
|
+
} else {
|
|
1839
|
+
console.log(`Error compressing ${filename}: ${error.message}`);
|
|
1840
|
+
io.to(room).emit("progress", {
|
|
1841
|
+
value: Math.round(((i + 1) / count) * 100),
|
|
1842
|
+
data: `Skipping ${filename} - unsupported format: ${format}`,
|
|
1843
|
+
});
|
|
1844
|
+
//io.to(room).emit('errormessage',`Skipping ${filename} - unsupported format: ${format}`);
|
|
1783
1845
|
}
|
|
1784
1846
|
}
|
|
1847
|
+
i++;
|
|
1785
1848
|
}
|
|
1786
|
-
io.to(room).emit("
|
|
1787
|
-
|
|
1849
|
+
io.to(room).emit("progress", {
|
|
1850
|
+
value: 100,
|
|
1851
|
+
style: `progress-bar progress-bar-striped bg-success`,
|
|
1852
|
+
data: `Compress all images complete...`,
|
|
1853
|
+
});
|
|
1854
|
+
res.json(Util.jsonSuccess("Compress all images completed..."));
|
|
1788
1855
|
}
|
|
1789
1856
|
} catch (e) {
|
|
1790
1857
|
console.log(e);
|