Commit 6464611d authored by haiyoucuv's avatar haiyoucuv

init

parent ecfa20a9
import {exec, execSync} from 'child_process'; import { exec, execSync } from 'child_process';
import * as path from "path"; import * as path from "path";
import {promises as fs} from "fs"; import { promises as fs } from "fs";
import * as Os from "os"; import * as Os from "os";
import * as fsSync from "fs"; import * as fsSync from "fs";
import * as crypto from "crypto";
/** 压缩引擎路径表 */ /** 压缩引擎路径表 */
const enginePathMap = { const enginePathMap = {
...@@ -38,64 +39,46 @@ export async function compressAllImage( ...@@ -38,64 +39,46 @@ export async function compressAllImage(
let completed = 0; let completed = 0;
const chunkSize = 20; const chunkSize = 20;
const totalBatches = Math.ceil(paths.length / chunkSize);
if (platform == "win32") { const userHomeDir = Os.homedir();
const cacheDir = path.join(userHomeDir, ".image_compress_cache");
const now = Date.now(); if (!fsSync.existsSync(cacheDir)) await fs.mkdir(cacheDir, { recursive: true });
const tempDir = `C:\\Temp\\duiba\\${now}`;
if (!fsSync.existsSync("C:\\Temp")) await fs.mkdir("C:\\Temp"); const now = Date.now();
if (!fsSync.existsSync("C:\\Temp\\duiba")) await fs.mkdir("C:\\Temp\\duiba"); const tempDir = path.join(userHomeDir, `.image_compress_temp/${now}`);
if (!fsSync.existsSync(tempDir)) await fs.mkdir(tempDir);
for (let i = 0; i < paths.length; i += chunkSize) { if (!fsSync.existsSync(tempDir)) await fs.mkdir(tempDir, { recursive: true });
const currentBatch = Math.floor(i / chunkSize) + 1;
// console.log(`正在处理第 ${currentBatch}/${totalBatches} 批图片压缩...\n`);
const chunk = paths.slice(i, i + chunkSize); for (let i = 0; i < paths.length; i += chunkSize) {
const chunk = paths.slice(i, i + chunkSize);
const ps = chunk.map(async (imgPath, idx) => { const ps = chunk.map(async (imgPath, idx) => {
const tempName = `${tempDir}/${i + idx}.png`; const tempName = `${tempDir}/${i + idx}.png`;
// 检查缓存
const fileBuffer = await fs.readFile(imgPath);
const md5Hash = crypto.createHash("md5").update(fileBuffer).digest("hex");
const cachedFilePath = path.join(cacheDir, `${md5Hash}.png`);
if (fsSync.existsSync(cachedFilePath)) {
// 如果缓存文件存在,直接复制到目标路径
await fs.copyFile(cachedFilePath, imgPath);
} else {
// 如果缓存文件不存在,执行压缩操作并缓存
await fs.copyFile(imgPath, tempName); await fs.copyFile(imgPath, tempName);
execSync(`"${pngquantPath}" ${compressOptions} "${tempName}"`); execSync(`"${pngquantPath}" ${compressOptions} "${tempName}"`);
await fs.copyFile(tempName, imgPath); await fs.copyFile(tempName, imgPath);
completed++; await fs.copyFile(tempName, cachedFilePath);
onProgress(completed, paths.length); }
});
await Promise.all(ps);
}
await fs.rm(tempDir, {recursive: true});
} else {
// 批次执行
for (let i = 0; i < paths.length; i += chunkSize) {
const currentBatch = Math.floor(i / chunkSize) + 1;
// console.log(`正在处理第 ${currentBatch}/${totalBatches} 批图片压缩...\n`);
const chunk = paths.slice(i, i + chunkSize); completed++;
onProgress(completed, paths.length);
let command = ""; });
chunk.forEach((imgPath) => {
command += `"${pngquantPath}" ${compressOptions} "${imgPath}" &`; // 使用分号替代&实现串行
});
await new Promise<void>((resolve) => {
exec(command, (error, stdout, stderr) => {
if (error) {
// console.error(error);
}
completed += chunk.length;
onProgress(completed, paths.length);
resolve();
});
});
}
await Promise.all(ps);
} }
await fs.rm(tempDir, { recursive: true });
} }
...@@ -56,10 +56,10 @@ ...@@ -56,10 +56,10 @@
// alert("【警告】检测到活动url中没有appID参数\n缺少该参数会导致埋点、分享、app信息获取错误。") // alert("【警告】检测到活动url中没有appID参数\n缺少该参数会导致埋点、分享、app信息获取错误。")
} }
</script> </script>
<script type="module" crossorigin src="https://yun.duiba.com.cn/db_games/spark/v3/1747620295044/assets/index-ZZ6fcdGr.js"></script> <script type="module" crossorigin src="https://yun.duiba.com.cn/db_games/spark/v3/1747621241875/assets/index-DJIH5p0R.js"></script>
<link rel="modulepreload" crossorigin href="https://yun.duiba.com.cn/db_games/spark/v3/1747620295044/assets/vendor-BQH5J874.js"> <link rel="modulepreload" crossorigin href="https://yun.duiba.com.cn/db_games/spark/v3/1747621241875/assets/vendor-CzylL8D3.js">
<link rel="stylesheet" crossorigin href="https://yun.duiba.com.cn/db_games/spark/v3/1747620295044/assets/vendor-BQh5NVNk.css"> <link rel="stylesheet" crossorigin href="https://yun.duiba.com.cn/db_games/spark/v3/1747621241875/assets/vendor-BQh5NVNk.css">
<link rel="stylesheet" crossorigin href="https://yun.duiba.com.cn/db_games/spark/v3/1747620295044/assets/index-BW0uHGZm.css"> <link rel="stylesheet" crossorigin href="https://yun.duiba.com.cn/db_games/spark/v3/1747621241875/assets/index-CR0HLAE6.css">
<script type="module">import.meta.url;import("_").catch(()=>1);(async function*(){})().next();if(location.protocol!="file:"){window.__vite_is_modern_browser=true}</script> <script type="module">import.meta.url;import("_").catch(()=>1);(async function*(){})().next();if(location.protocol!="file:"){window.__vite_is_modern_browser=true}</script>
<script type="module">!function(){if(window.__vite_is_modern_browser)return;console.warn("vite: loading legacy chunks, syntax error above and the same error below should be ignored");var e=document.getElementById("vite-legacy-polyfill"),n=document.createElement("script");n.src=e.src,n.onload=function(){System.import(document.getElementById('vite-legacy-entry').getAttribute('data-src'))},document.body.appendChild(n)}();</script> <script type="module">!function(){if(window.__vite_is_modern_browser)return;console.warn("vite: loading legacy chunks, syntax error above and the same error below should be ignored");var e=document.getElementById("vite-legacy-polyfill"),n=document.createElement("script");n.src=e.src,n.onload=function(){System.import(document.getElementById('vite-legacy-entry').getAttribute('data-src'))},document.body.appendChild(n)}();</script>
</head> </head>
...@@ -67,8 +67,8 @@ ...@@ -67,8 +67,8 @@
<body> <body>
<div id="root"></div> <div id="root"></div>
<script nomodule>!function(){var e=document,t=e.createElement("script");if(!("noModule"in t)&&"onbeforeload"in t){var n=!1;e.addEventListener("beforeload",(function(e){if(e.target===t)n=!0;else if(!e.target.hasAttribute("nomodule")||!n)return;e.preventDefault()}),!0),t.type="module",t.src=".",e.head.appendChild(t),t.remove()}}();</script> <script nomodule>!function(){var e=document,t=e.createElement("script");if(!("noModule"in t)&&"onbeforeload"in t){var n=!1;e.addEventListener("beforeload",(function(e){if(e.target===t)n=!0;else if(!e.target.hasAttribute("nomodule")||!n)return;e.preventDefault()}),!0),t.type="module",t.src=".",e.head.appendChild(t),t.remove()}}();</script>
<script nomodule crossorigin id="vite-legacy-polyfill" src="https://yun.duiba.com.cn/db_games/spark/v3/1747620295044/assets/polyfills-legacy-arANKBzQ.js"></script> <script nomodule crossorigin id="vite-legacy-polyfill" src="https://yun.duiba.com.cn/db_games/spark/v3/1747621241875/assets/polyfills-legacy-arANKBzQ.js"></script>
<script nomodule crossorigin id="vite-legacy-entry" data-src="https://yun.duiba.com.cn/db_games/spark/v3/1747620295044/assets/index-legacy-fL06-ahT.js">System.import(document.getElementById('vite-legacy-entry').getAttribute('data-src'))</script> <script nomodule crossorigin id="vite-legacy-entry" data-src="https://yun.duiba.com.cn/db_games/spark/v3/1747621241875/assets/index-legacy-DUTdIdqz.js">System.import(document.getElementById('vite-legacy-entry').getAttribute('data-src'))</script>
</body> </body>
</html> </html>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment