feat: Optimize and parallel image processing

This commit is contained in:
Maksim Eltyshev
2025-12-19 19:11:02 +01:00
parent fb5d5233bf
commit 208e61a272
8 changed files with 178 additions and 115 deletions

View File

@@ -6,6 +6,7 @@
const fs = require('fs');
const fse = require('fs-extra');
const path = require('path');
const { pipeline } = require('stream/promises');
const { rimraf } = require('rimraf');
const PATH_SEGMENT_TO_URL_REPLACE_REGEX = /(public|private)\//;
@@ -27,8 +28,12 @@ class LocalFileManager {
}
// eslint-disable-next-line class-methods-use-this
async save(filePathSegment, buffer) {
await fse.outputFile(buildPath(filePathSegment), buffer);
async save(filePathSegment, stream) {
const filePath = buildPath(filePathSegment);
const { dir: dirPath } = path.parse(filePath);
await fs.promises.mkdir(dirPath, { recursive: true });
await pipeline(stream, fs.createWriteStream(filePath));
}
// eslint-disable-next-line class-methods-use-this

View File

@@ -13,6 +13,7 @@ const {
ListObjectsV2Command,
PutObjectCommand,
} = require('@aws-sdk/client-s3');
const { Upload } = require('@aws-sdk/lib-storage');
class S3FileManager {
constructor(client) {
@@ -31,15 +32,18 @@ class S3FileManager {
return null;
}
async save(filePathSegment, buffer, contentType) {
const command = new PutObjectCommand({
Bucket: sails.config.custom.s3Bucket,
Key: filePathSegment,
Body: buffer,
ContentType: contentType,
async save(filePathSegment, stream, contentType) {
const upload = new Upload({
client: this.client,
params: {
Bucket: sails.config.custom.s3Bucket,
Key: filePathSegment,
Body: stream,
ContentType: contentType,
},
});
await this.client.send(command);
await upload.done();
}
async read(filePathSegment) {