Commit 2edfa954 by linjinhong

fix:修改下载文件方法

parent a6c2149b
[{"head_setting":{"gridShow":1,"gridValue":0,"unit":"inch","language":"cn","autoPrint":false,"gridSpacing":1},"version":"1.0.22","print_setting":{"printer":"","xy":"","byInk":2,"byInkVolume":1,"byDoublePrint":3,"bEcoMode":false,"bMaterialBlack":false,"byHighlight":5,"printNum":1,"bFastMode":false,"bUniPrint":false,"bDivide":false,"bPause":false,"byMask":3,"bTransColor":false,"byChoke":2,"minWhiteInkType":0,"bySaturation":5,"byBrightness":5,"byContrast":5,"iCyanBalance":0,"iMagentaBalance":0,"iBlackBalance":0,"iYellowBalance":0,"byMinWhite":1,"bMultiple":false},"production_no":"GCPSC2509272339","print_time":"2025-09-28 09:52:21","position_unit":{"x":408.8,"rate":1.396341463414634,"sx":0,"zIndex":0,"sh":0,"sw":0,"sy":0,"y":458.6,"w":594.4,"h":826.7,"r":0},"position_after_px":{"x":"1.1","rate":1.2256385998107853,"sx":0,"zIndex":0,"sh":0,"sw":0,"sy":0,"y":"0.0","w":"24.8","h":"30.4","r":0},"position_before_px":{"url":"D:\\work\\electron-printer\\print4.0\\Input\\884a8758-064b-4115-b053-bb24e13adddd.png","fileName":"884a8758-064b-4115-b053-bb24e13adddd.png","rate":1.2256385998107853,"w":748.9972983404092,"zIndex":0,"x":34.5013508297954,"y":1,"h":918,"r":0},"send_api_data":{"printer":"","xy":"","byInk":2,"byInkVolume":1,"byDoublePrint":3,"bEcoMode":false,"bMaterialBlack":false,"byHighlight":5,"printNum":1,"bFastMode":false,"bUniPrint":false,"bDivide":false,"bPause":false,"byMask":3,"bTransColor":false,"byChoke":2,"minWhiteInkType":0,"bySaturation":5,"byBrightness":5,"byContrast":5,"iCyanBalance":0,"iMagentaBalance":0,"iBlackBalance":0,"iYellowBalance":0,"byMinWhite":1,"bMultiple":false,"byPlatenSize":1,"cmd":"GTXproCMD.exe print -X \"Profile\\884a8758-064b-4115-b053-bb24e13adddd.xml\" -I \"Input\\884a8758-064b-4115-b053-bb24e13adddd.png\" -A \"Output\\884a8758-064b-4115-b053-bb24e13adddd.arxp\" -S 37094546 -L 01760010 -D 0 -W 1","fileName":"884a8758-064b-4115-b053-bb24e13adddd.png","print_cmd":"GTXproCMD.exe send -A \"Output\\884a8758-064b-4115-b053-bb24e13adddd.arxp\" -P \"Brother GTX-6 FileOutput"}}]
\ No newline at end of file
...@@ -5,7 +5,7 @@ var path = require("path"); ...@@ -5,7 +5,7 @@ var path = require("path");
var request = require("request"); var request = require("request");
var uuid = require("uuid"); var uuid = require("uuid");
const compressing = require("compressing"); const compressing = require("compressing");
import { createWriteStream } from "fs";
const { getVersion } = require("@/server/utils/store"); const { getVersion } = require("@/server/utils/store");
function getCurrentVersion() { function getCurrentVersion() {
...@@ -53,26 +53,112 @@ function zip(from, to) { ...@@ -53,26 +53,112 @@ function zip(from, to) {
}); });
} }
// 下载素材 // // 下载素材
// export const downloadImage = (list) => {
// return new Promise((resolve, reject) => {
// try {
// // console.log("Formatted Folder Name:", folderName); // 检查替换结果
// let dirPath = path.join(process.cwd(), `./${getCurrentVersion()}/Input/`);
// console.log(62, dirPath);
// console.log("list", list);
// if (!fs.existsSync(dirPath)) {
// fs.mkdirSync(dirPath);
// }
// let count = 0;
// for (let i = 0; i < list.length; i++) {
// if (list[i].url && list[i].url.includes("http")) {
// let fileName = "";
// let type = 2;
// let flag =
// [
// ".zip",
// ".gzip",
// ".tar",
// ".taz",
// ".rar",
// ".7z",
// ".gz",
// ".bz2",
// ].filter((el) => list[i].url.toLowerCase().includes(el)).length > 0;
// if (flag) {
// fileName = list[i].url.split("/")[
// list[i].url.split("/").length - 1
// ];
// type = 1;
// } else {
// fileName = uuid.v4() + ".png";
// type = 2;
// }
// let p = path.join(
// process.cwd(),
// `./${getCurrentVersion()}/Input/` + fileName
// );
// let stream = fs.createWriteStream(p);
// request(list[i].url)
// .pipe(stream)
// .on("close", async function() {
// if (type === 1) {
// list[i].list = await zip(p, dirPath);
// if (count === list.length - 1) {
// resolve(list);
// }
// } else {
// if (!list[i].list) list[i].list = [];
// const filename = list[i].url.split("/")[
// list[i].url.split("/").length - 1
// ];
// console.log(filename);
// list[i].list.push({
// fileName,
// productionFile: p,
// designId: filename.split("_GCPS")[0],
// });
// }
// if (count === list.length - 1) {
// // console.log(list);
// resolve(list);
// }
// count++;
// });
// }
// }
// } catch (err) {
// console.log(err);
// reject(err);
// }
// });
// };
export const downloadImage = (list) => { export const downloadImage = (list) => {
return new Promise((resolve, reject) => { return new Promise(async (resolve, reject) => {
try { try {
// console.log("Formatted Folder Name:", folderName); // 检查替换结果 const dirPath = path.join(
let dirPath = path.join(process.cwd(), `./${getCurrentVersion()}/Input/`); process.cwd(),
console.log(62, dirPath); `./${getCurrentVersion()}/Input/`
console.log("list", list); );
// 创建目录(如果不存在)
if (!fs.existsSync(dirPath)) { if (!fs.existsSync(dirPath)) {
fs.mkdirSync(dirPath); fs.mkdirSync(dirPath, { recursive: true });
} }
let count = 0; // 过滤出有效的下载项
for (let i = 0; i < list.length; i++) { const downloadItems = list.filter(
if (list[i].url && list[i].url.includes("http")) { (item) => item.url && item.url.includes("http")
let fileName = ""; );
let type = 2;
let flag = // 如果没有需要下载的项,直接返回
[ if (downloadItems.length === 0) {
return resolve(list);
}
// 并行处理所有下载
const downloadPromises = downloadItems.map((item) => {
return new Promise(async (itemResolve) => {
try {
const archiveExtensions = [
".zip", ".zip",
".gzip", ".gzip",
".tar", ".tar",
...@@ -81,52 +167,68 @@ export const downloadImage = (list) => { ...@@ -81,52 +167,68 @@ export const downloadImage = (list) => {
".7z", ".7z",
".gz", ".gz",
".bz2", ".bz2",
].filter((el) => list[i].url.toLowerCase().includes(el)).length > 0;
if (flag) {
fileName = list[i].url.split("/")[
list[i].url.split("/").length - 1
]; ];
type = 1; const isArchive = archiveExtensions.some((ext) =>
} else { item.url.toLowerCase().includes(ext)
fileName = uuid.v4() + ".png"; );
type = 2;
}
let p = path.join(
process.cwd(),
`./${getCurrentVersion()}/Input/` + fileName
);
let stream = fs.createWriteStream(p);
request(list[i].url)
.pipe(stream)
.on("close", async function() {
if (type === 1) {
list[i].list = await zip(p, dirPath);
if (count === list.length - 1) {
resolve(list);
}
} else {
if (!list[i].list) list[i].list = [];
const filename = list[i].url.split("/")[
list[i].url.split("/").length - 1
];
console.log(filename);
list[i].list.push({ const fileName = isArchive
fileName, ? item.url.split("/").pop()
productionFile: p, : `${uuid.v4()}.png`;
designId: filename.split("_GCPS")[0],
}); const filePath = path.join(dirPath, fileName);
}
if (count === list.length - 1) { // 使用流下载文件
// console.log(list); const writeStream = fs.createWriteStream(filePath);
resolve(list); const requestStream = request(item.url);
}
count++; // 添加超时处理
requestStream.on("response", (response) => {
response.setTimeout(30000, () => {
requestStream.abort();
console.error(`下载超时: ${item.url}`);
itemResolve();
});
}); });
}
} // 使用管道连接请求流和写入流
requestStream.pipe(writeStream);
// 等待下载完成
await new Promise((resolveStream) => {
writeStream.on("finish", resolveStream);
writeStream.on("error", (err) => {
console.error(`下载失败: ${item.url}`, err);
resolveStream();
});
});
if (isArchive) {
// 处理压缩文件
item.list = await zip(filePath, dirPath);
} else {
// 处理普通图片文件
if (!item.list) item.list = [];
const originalName = item.url.split("/").pop();
item.list.push({
fileName,
productionFile: filePath,
designId: originalName.split("_GCPS")[0],
});
}
} catch (error) {
console.error(`处理失败: ${item.url}`, error);
} finally {
itemResolve();
}
});
});
// 等待所有下载完成
await Promise.all(downloadPromises);
resolve(list);
} catch (err) { } catch (err) {
console.log(err); console.error("下载整体失败", err);
reject(err); reject(err);
} }
}); });
......
...@@ -158,11 +158,7 @@ async function processImages(inputPath, outputPath, options = {}) { ...@@ -158,11 +158,7 @@ async function processImages(inputPath, outputPath, options = {}) {
results = await cropTransparentEdges(inputPath, outputPath, options); results = await cropTransparentEdges(inputPath, outputPath, options);
} else { } else {
// 处理单个图片 // 处理单个图片
const result = await cropImageTransparentEdges( const result = await cropImage(inputPath, outputPath, options);
inputPath,
outputPath,
options
);
results = [{ file: path.basename(inputPath), ...result }]; results = [{ file: path.basename(inputPath), ...result }];
} }
......
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