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
@@ -87,8 +87,8 @@ module.exports = {
const thumbnailsPathSegment = `${dirPathSegment}/thumbnails`; const thumbnailsPathSegment = `${dirPathSegment}/thumbnails`;
const thumbnailsExtension = metadata.format === 'jpeg' ? 'jpg' : metadata.format; const thumbnailsExtension = metadata.format === 'jpeg' ? 'jpg' : metadata.format;
try { const outside360 = image
const outside360Buffer = await image .clone()
.resize(360, 360, { .resize(360, 360, {
fit: 'outside', fit: 'outside',
withoutEnlargement: true, withoutEnlargement: true,
@@ -96,16 +96,10 @@ module.exports = {
.png({ .png({
quality: 75, quality: 75,
force: false, force: false,
}) });
.toBuffer();
await fileManager.save( const outside720 = image
`${thumbnailsPathSegment}/outside-360.${thumbnailsExtension}`, .clone()
outside360Buffer,
inputs.file.type,
);
const outside720Buffer = await image
.resize(720, 720, { .resize(720, 720, {
fit: 'outside', fit: 'outside',
withoutEnlargement: true, withoutEnlargement: true,
@@ -113,14 +107,21 @@ module.exports = {
.png({ .png({
quality: 75, quality: 75,
force: false, force: false,
}) });
.toBuffer();
await fileManager.save( try {
`${thumbnailsPathSegment}/outside-720.${thumbnailsExtension}`, await Promise.all([
outside720Buffer, fileManager.save(
`${thumbnailsPathSegment}/outside-360.${thumbnailsExtension}`,
outside360,
inputs.file.type, inputs.file.type,
); ),
fileManager.save(
`${thumbnailsPathSegment}/outside-720.${thumbnailsExtension}`,
outside720,
inputs.file.type,
),
]);
data.image = { data.image = {
width, width,
@@ -39,21 +39,17 @@ module.exports = {
}); });
let metadata; let metadata;
let originalBuffer;
try { try {
metadata = await image.metadata(); metadata = await image.metadata();
if (metadata.orientation && metadata.orientation > 4) {
image = image.rotate();
}
originalBuffer = await image.toBuffer();
} catch (error) { } catch (error) {
await rimraf(inputs.file.fd); await rimraf(inputs.file.fd);
throw 'fileIsNotImage'; throw 'fileIsNotImage';
} }
if (metadata.orientation && metadata.orientation > 4) {
image = image.rotate();
}
const { id: uploadedFileId } = await UploadedFile.qm.createOne({ const { id: uploadedFileId } = await UploadedFile.qm.createOne({
mimeType, mimeType,
size, size,
@@ -64,14 +60,8 @@ module.exports = {
const dirPathSegment = `${sails.config.custom.backgroundImagesPathSegment}/${uploadedFileId}`; const dirPathSegment = `${sails.config.custom.backgroundImagesPathSegment}/${uploadedFileId}`;
const extension = metadata.format === 'jpeg' ? 'jpg' : metadata.format; const extension = metadata.format === 'jpeg' ? 'jpg' : metadata.format;
try { const outside360 = image
await fileManager.save( .clone()
`${dirPathSegment}/original.${extension}`,
originalBuffer,
inputs.file.type,
);
const outside360Buffer = await image
.resize(360, 360, { .resize(360, 360, {
fit: 'outside', fit: 'outside',
withoutEnlargement: true, withoutEnlargement: true,
@@ -79,14 +69,17 @@ module.exports = {
.png({ .png({
quality: 75, quality: 75,
force: false, force: false,
}) });
.toBuffer();
await fileManager.save( try {
await Promise.all([
fileManager.save(`${dirPathSegment}/original.${extension}`, image, inputs.file.type),
fileManager.save(
`${dirPathSegment}/outside-360.${extension}`, `${dirPathSegment}/outside-360.${extension}`,
outside360Buffer, outside360,
inputs.file.type, inputs.file.type,
); ),
]);
} catch (error) { } catch (error) {
sails.log.warn(error.stack); sails.log.warn(error.stack);
@@ -39,21 +39,17 @@ module.exports = {
}); });
let metadata; let metadata;
let originalBuffer;
try { try {
metadata = await image.metadata(); metadata = await image.metadata();
if (metadata.orientation && metadata.orientation > 4) {
image = image.rotate();
}
originalBuffer = await image.toBuffer();
} catch (error) { } catch (error) {
await rimraf(inputs.file.fd); await rimraf(inputs.file.fd);
throw 'fileIsNotImage'; throw 'fileIsNotImage';
} }
if (metadata.orientation && metadata.orientation > 4) {
image = image.rotate();
}
const { id: uploadedFileId } = await UploadedFile.qm.createOne({ const { id: uploadedFileId } = await UploadedFile.qm.createOne({
mimeType, mimeType,
size, size,
@@ -64,28 +60,21 @@ module.exports = {
const dirPathSegment = `${sails.config.custom.userAvatarsPathSegment}/${uploadedFileId}`; const dirPathSegment = `${sails.config.custom.userAvatarsPathSegment}/${uploadedFileId}`;
const extension = metadata.format === 'jpeg' ? 'jpg' : metadata.format; const extension = metadata.format === 'jpeg' ? 'jpg' : metadata.format;
try { const cover180 = image
await fileManager.save( .clone()
`${dirPathSegment}/original.${extension}`,
originalBuffer,
inputs.file.type,
);
const cover180Buffer = await image
.resize(180, 180, { .resize(180, 180, {
withoutEnlargement: true, withoutEnlargement: true,
}) })
.png({ .png({
quality: 75, quality: 75,
force: false, force: false,
}) });
.toBuffer();
await fileManager.save( try {
`${dirPathSegment}/cover-180.${extension}`, await Promise.all([
cover180Buffer, fileManager.save(`${dirPathSegment}/original.${extension}`, image, inputs.file.type),
inputs.file.type, fileManager.save(`${dirPathSegment}/cover-180.${extension}`, cover180, inputs.file.type),
); ]);
} catch (error) { } catch (error) {
sails.log.warn(error.stack); sails.log.warn(error.stack);
+4 -5
View File
@@ -162,8 +162,7 @@ module.exports = {
const fileManager = sails.hooks['file-manager'].getInstance(); const fileManager = sails.hooks['file-manager'].getInstance();
const { width, height } = metadata; const { width, height } = metadata;
try { image = image
const buffer = await image
.resize( .resize(
32, 32,
32, 32,
@@ -173,12 +172,12 @@ module.exports = {
} }
: undefined, : undefined,
) )
.png() .png();
.toBuffer();
try {
await fileManager.save( await fileManager.save(
`${sails.config.custom.faviconsPathSegment}/${hostname}.png`, `${sails.config.custom.faviconsPathSegment}/${hostname}.png`,
buffer, image,
'image/png', 'image/png',
); );
} catch (error) { } catch (error) {
@@ -6,6 +6,7 @@
const fs = require('fs'); const fs = require('fs');
const fse = require('fs-extra'); const fse = require('fs-extra');
const path = require('path'); const path = require('path');
const { pipeline } = require('stream/promises');
const { rimraf } = require('rimraf'); const { rimraf } = require('rimraf');
const PATH_SEGMENT_TO_URL_REPLACE_REGEX = /(public|private)\//; const PATH_SEGMENT_TO_URL_REPLACE_REGEX = /(public|private)\//;
@@ -27,8 +28,12 @@ class LocalFileManager {
} }
// eslint-disable-next-line class-methods-use-this // eslint-disable-next-line class-methods-use-this
async save(filePathSegment, buffer) { async save(filePathSegment, stream) {
await fse.outputFile(buildPath(filePathSegment), buffer); 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 // eslint-disable-next-line class-methods-use-this
@@ -13,6 +13,7 @@ const {
ListObjectsV2Command, ListObjectsV2Command,
PutObjectCommand, PutObjectCommand,
} = require('@aws-sdk/client-s3'); } = require('@aws-sdk/client-s3');
const { Upload } = require('@aws-sdk/lib-storage');
class S3FileManager { class S3FileManager {
constructor(client) { constructor(client) {
@@ -31,15 +32,18 @@ class S3FileManager {
return null; return null;
} }
async save(filePathSegment, buffer, contentType) { async save(filePathSegment, stream, contentType) {
const command = new PutObjectCommand({ const upload = new Upload({
client: this.client,
params: {
Bucket: sails.config.custom.s3Bucket, Bucket: sails.config.custom.s3Bucket,
Key: filePathSegment, Key: filePathSegment,
Body: buffer, Body: stream,
ContentType: contentType, ContentType: contentType,
},
}); });
await this.client.send(command); await upload.done();
} }
async read(filePathSegment) { async read(filePathSegment) {
+71
View File
@@ -8,6 +8,7 @@
"hasInstallScript": true, "hasInstallScript": true,
"dependencies": { "dependencies": {
"@aws-sdk/client-s3": "3.726.1", "@aws-sdk/client-s3": "3.726.1",
"@aws-sdk/lib-storage": "3.726.1",
"bcrypt": "^5.1.1", "bcrypt": "^5.1.1",
"bytes": "^3.1.2", "bytes": "^3.1.2",
"cross-env": "^7.0.3", "cross-env": "^7.0.3",
@@ -695,6 +696,27 @@
"@aws-sdk/client-sts": "^3.723.0" "@aws-sdk/client-sts": "^3.723.0"
} }
}, },
"node_modules/@aws-sdk/lib-storage": {
"version": "3.726.1",
"resolved": "https://registry.npmjs.org/@aws-sdk/lib-storage/-/lib-storage-3.726.1.tgz",
"integrity": "sha512-WuDxSZ8Bfe1N7gn5eXQ02dhlKWCAwW5qQErpJ4CCddXosF+gLxhGkrP9LkaaP0CpA3PxboHyET6HbWAggOWtqA==",
"license": "Apache-2.0",
"dependencies": {
"@smithy/abort-controller": "^4.0.0",
"@smithy/middleware-endpoint": "^4.0.0",
"@smithy/smithy-client": "^4.0.0",
"buffer": "5.6.0",
"events": "3.3.0",
"stream-browserify": "3.0.0",
"tslib": "^2.6.2"
},
"engines": {
"node": ">=18.0.0"
},
"peerDependencies": {
"@aws-sdk/client-s3": "^3.726.1"
}
},
"node_modules/@aws-sdk/middleware-bucket-endpoint": { "node_modules/@aws-sdk/middleware-bucket-endpoint": {
"version": "3.726.0", "version": "3.726.0",
"resolved": "https://registry.npmjs.org/@aws-sdk/middleware-bucket-endpoint/-/middleware-bucket-endpoint-3.726.0.tgz", "resolved": "https://registry.npmjs.org/@aws-sdk/middleware-bucket-endpoint/-/middleware-bucket-endpoint-3.726.0.tgz",
@@ -3124,6 +3146,26 @@
"integrity": "sha512-3oSeUO0TMV67hN1AmbXsK4yaqU7tjiHlbxRDZOpH0KW9+CeX4bRAaX0Anxt0tx2MrpRpWwQaPwIlISEJhYU5Pw==", "integrity": "sha512-3oSeUO0TMV67hN1AmbXsK4yaqU7tjiHlbxRDZOpH0KW9+CeX4bRAaX0Anxt0tx2MrpRpWwQaPwIlISEJhYU5Pw==",
"license": "MIT" "license": "MIT"
}, },
"node_modules/base64-js": {
"version": "1.5.1",
"resolved": "https://registry.npmjs.org/base64-js/-/base64-js-1.5.1.tgz",
"integrity": "sha512-AKpaYlHn8t4SVbOHCy+b5+KKgvR4vrsD8vbvrbiQJps7fKDTkjkDry6ji0rUJjC0kzbNePLwzxq8iypo41qeWA==",
"funding": [
{
"type": "github",
"url": "https://github.com/sponsors/feross"
},
{
"type": "patreon",
"url": "https://www.patreon.com/feross"
},
{
"type": "consulting",
"url": "https://feross.org/support"
}
],
"license": "MIT"
},
"node_modules/base64id": { "node_modules/base64id": {
"version": "2.0.0", "version": "2.0.0",
"resolved": "https://registry.npmjs.org/base64id/-/base64id-2.0.0.tgz", "resolved": "https://registry.npmjs.org/base64id/-/base64id-2.0.0.tgz",
@@ -3280,6 +3322,16 @@
"dev": true, "dev": true,
"license": "ISC" "license": "ISC"
}, },
"node_modules/buffer": {
"version": "5.6.0",
"resolved": "https://registry.npmjs.org/buffer/-/buffer-5.6.0.tgz",
"integrity": "sha512-/gDYp/UtU0eA1ys8bOs9J6a+E/KWIY+DZ+Q2WESNUA0jFRsJOc0SNUO6xJ5SGA1xueg3NL65W6s+NY5l9cunuw==",
"license": "MIT",
"dependencies": {
"base64-js": "^1.0.2",
"ieee754": "^1.1.4"
}
},
"node_modules/buffer-equal-constant-time": { "node_modules/buffer-equal-constant-time": {
"version": "1.0.1", "version": "1.0.1",
"resolved": "https://registry.npmjs.org/buffer-equal-constant-time/-/buffer-equal-constant-time-1.0.1.tgz", "resolved": "https://registry.npmjs.org/buffer-equal-constant-time/-/buffer-equal-constant-time-1.0.1.tgz",
@@ -4930,6 +4982,15 @@
"node": ">= 0.6" "node": ">= 0.6"
} }
}, },
"node_modules/events": {
"version": "3.3.0",
"resolved": "https://registry.npmjs.org/events/-/events-3.3.0.tgz",
"integrity": "sha512-mQw+2fkQbALzQ7V0MY0IqdnXNOeTtP4r0lN9z7AAawCXgqea7bDii20AYrIBrFd/Hx0M2Ocz6S111CaFkUcb0Q==",
"license": "MIT",
"engines": {
"node": ">=0.8.x"
}
},
"node_modules/express": { "node_modules/express": {
"version": "4.21.2", "version": "4.21.2",
"resolved": "https://registry.npmjs.org/express/-/express-4.21.2.tgz", "resolved": "https://registry.npmjs.org/express/-/express-4.21.2.tgz",
@@ -10599,6 +10660,16 @@
"node": ">= 0.4" "node": ">= 0.4"
} }
}, },
"node_modules/stream-browserify": {
"version": "3.0.0",
"resolved": "https://registry.npmjs.org/stream-browserify/-/stream-browserify-3.0.0.tgz",
"integrity": "sha512-H73RAHsVBapbim0tU2JwwOiXUj+fikfiaoYAKHF3VJfA0pe2BCzkhAHBlLG6REzE+2WNZcxOXjK7lkso+9euLA==",
"license": "MIT",
"dependencies": {
"inherits": "~2.0.4",
"readable-stream": "^3.5.0"
}
},
"node_modules/streamifier": { "node_modules/streamifier": {
"version": "0.1.1", "version": "0.1.1",
"resolved": "https://registry.npmjs.org/streamifier/-/streamifier-0.1.1.tgz", "resolved": "https://registry.npmjs.org/streamifier/-/streamifier-0.1.1.tgz",
+1
View File
@@ -45,6 +45,7 @@
}, },
"dependencies": { "dependencies": {
"@aws-sdk/client-s3": "3.726.1", "@aws-sdk/client-s3": "3.726.1",
"@aws-sdk/lib-storage": "3.726.1",
"bcrypt": "^5.1.1", "bcrypt": "^5.1.1",
"bytes": "^3.1.2", "bytes": "^3.1.2",
"cross-env": "^7.0.3", "cross-env": "^7.0.3",