![](/img/trans.png)
[英]How can I overcome the browser storage limitations and being able to store more than 500 Mb of files?
[英]How can I process more than 500 MB worth of images?
代码在处理小于 500 MB 的图像时可以正常工作。 (我正在尝试做 +25 GB 的价值)有没有办法修改代码以使其适用于大量图像?
我收到如下所示的错误:
node:internal/process/promises:279 triggerUncaughtException(err, true /* fromPromise */); ^
[错误:EIO:i/o 错误,写入] { errno:-5,代码:“EIO”,系统调用:“写入”}
或这个:
node:internal/process/promises:279 triggerUncaughtException(err, true /* fromPromise */); ^
错误:在 Socket.Readable.read (node:internal/streams/readable:487:10) 的 Socket._read (node:net:625:5) 的 tryReadStart (node:net:614:20) 处读取 ENOTCONN。在 Object.Socket (node:net:286:41) 在 ChildProcess 的 createSocket (node:internal/child_process:328:14) 处读取 (node:net:666:39) .spawn (node:internal/child_process:445:23) at Object.spawn (node:child_process:700:9) at execa (file:///mnt/d/Projects/GH_2022/imagemin-mozjpeg/node_modules/execa/ index.js:84:26) { errno: -107, code: 'ENOTCONN', syscall: 'read', originalMessage: 'read ENOTCONN', shortMessage: '命令因 ENOTCONN 失败:/mnt/d/Projects/GH_2022/ imagemin-mozjpeg/node_modules/mozjpeg/vendor/cjpeg -quality 75\n' + '读取 ENOTCONN',命令:'/mnt/d/Projects/GH_2022/imagemin-mozjpeg/node_modules/mozjpeg/vendor/cjpeg -quality 75' , escapedCommand: '"/mnt/d/Projects/GH_2022/imagemin-mozjpeg/node_modules/mozjpeg/vendor/cjpeg" -quality 75', exitCode:未定义,信号:未定义,signalDescription:未定义,标准输出:缓冲区(0)[Uint8Array] [],标准错误:缓冲区(0)[Uint8Array] [],失败:true,timedOut:false,isCanceled:false,终止:错误的 }
这是我目前拥有的:
import imagemin from 'imagemin';
import imageminMozjpeg from 'imagemin-mozjpeg';
import { promises as fsPromises } from 'node:fs';
import { promisify } from 'node:util';
import path from 'node:path';
import fs from 'graceful-fs';
const writeFile = promisify(fs.writeFile);
const srcdir = 'images/source';
const distdir = 'images/dist';
imagemin([srcdir + '/**/*.{jpg,jpeg,png}'], {
plugins: [
imageminMozjpeg({
quality: 75
})
]
}).then(files => files
.forEach(async v => {
let source = path.parse(v.sourcePath);
v.destinationPath = `${source.dir.replace(srcdir, distdir)}/${source.name}${source.ext}`;
await fsPromises.mkdir(path.dirname(v.destinationPath), { recursive: true });
await writeFile(v.destinationPath, v.data);
})
);
对于初学者来说, .forEach()
不是承诺感知的(它根本不注意回调的返回值)。 因此,您正在尝试一次并行运行所有文件操作。 我的猜测是您的文件句柄用完了(基于错误)。 您可以按顺序运行它们(这应该会减少 memory 和文件句柄要求),如下所示:
imagemin([srcdir + '/**/*.{jpg,jpeg,png}'], {
plugins: [
imageminMozjpeg({
quality: 75
})
]
}).then(async files => {
for (let v of files) {
let source = path.parse(v.sourcePath);
v.destinationPath = `${source.dir.replace(srcdir, distdir)}/${source.name}${source.ext}`;
await fsPromises.mkdir(path.dirname(v.destinationPath), { recursive: true });
await writeFile(v.destinationPath, v.data);
}
}).catch(err => {
console.log(err);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.