mirror of
https://github.com/plankanban/planka.git
synced 2025-12-26 09:15:01 +03:00
feat: Optimize and parallel image processing
This commit is contained in:
@@ -87,40 +87,41 @@ module.exports = {
|
||||
const thumbnailsPathSegment = `${dirPathSegment}/thumbnails`;
|
||||
const thumbnailsExtension = metadata.format === 'jpeg' ? 'jpg' : metadata.format;
|
||||
|
||||
const outside360 = image
|
||||
.clone()
|
||||
.resize(360, 360, {
|
||||
fit: 'outside',
|
||||
withoutEnlargement: true,
|
||||
})
|
||||
.png({
|
||||
quality: 75,
|
||||
force: false,
|
||||
});
|
||||
|
||||
const outside720 = image
|
||||
.clone()
|
||||
.resize(720, 720, {
|
||||
fit: 'outside',
|
||||
withoutEnlargement: true,
|
||||
})
|
||||
.png({
|
||||
quality: 75,
|
||||
force: false,
|
||||
});
|
||||
|
||||
try {
|
||||
const outside360Buffer = await image
|
||||
.resize(360, 360, {
|
||||
fit: 'outside',
|
||||
withoutEnlargement: true,
|
||||
})
|
||||
.png({
|
||||
quality: 75,
|
||||
force: false,
|
||||
})
|
||||
.toBuffer();
|
||||
|
||||
await fileManager.save(
|
||||
`${thumbnailsPathSegment}/outside-360.${thumbnailsExtension}`,
|
||||
outside360Buffer,
|
||||
inputs.file.type,
|
||||
);
|
||||
|
||||
const outside720Buffer = await image
|
||||
.resize(720, 720, {
|
||||
fit: 'outside',
|
||||
withoutEnlargement: true,
|
||||
})
|
||||
.png({
|
||||
quality: 75,
|
||||
force: false,
|
||||
})
|
||||
.toBuffer();
|
||||
|
||||
await fileManager.save(
|
||||
`${thumbnailsPathSegment}/outside-720.${thumbnailsExtension}`,
|
||||
outside720Buffer,
|
||||
inputs.file.type,
|
||||
);
|
||||
await Promise.all([
|
||||
fileManager.save(
|
||||
`${thumbnailsPathSegment}/outside-360.${thumbnailsExtension}`,
|
||||
outside360,
|
||||
inputs.file.type,
|
||||
),
|
||||
fileManager.save(
|
||||
`${thumbnailsPathSegment}/outside-720.${thumbnailsExtension}`,
|
||||
outside720,
|
||||
inputs.file.type,
|
||||
),
|
||||
]);
|
||||
|
||||
data.image = {
|
||||
width,
|
||||
|
||||
@@ -39,21 +39,17 @@ module.exports = {
|
||||
});
|
||||
|
||||
let metadata;
|
||||
let originalBuffer;
|
||||
|
||||
try {
|
||||
metadata = await image.metadata();
|
||||
|
||||
if (metadata.orientation && metadata.orientation > 4) {
|
||||
image = image.rotate();
|
||||
}
|
||||
|
||||
originalBuffer = await image.toBuffer();
|
||||
} catch (error) {
|
||||
await rimraf(inputs.file.fd);
|
||||
throw 'fileIsNotImage';
|
||||
}
|
||||
|
||||
if (metadata.orientation && metadata.orientation > 4) {
|
||||
image = image.rotate();
|
||||
}
|
||||
|
||||
const { id: uploadedFileId } = await UploadedFile.qm.createOne({
|
||||
mimeType,
|
||||
size,
|
||||
@@ -64,29 +60,26 @@ module.exports = {
|
||||
const dirPathSegment = `${sails.config.custom.backgroundImagesPathSegment}/${uploadedFileId}`;
|
||||
const extension = metadata.format === 'jpeg' ? 'jpg' : metadata.format;
|
||||
|
||||
const outside360 = image
|
||||
.clone()
|
||||
.resize(360, 360, {
|
||||
fit: 'outside',
|
||||
withoutEnlargement: true,
|
||||
})
|
||||
.png({
|
||||
quality: 75,
|
||||
force: false,
|
||||
});
|
||||
|
||||
try {
|
||||
await fileManager.save(
|
||||
`${dirPathSegment}/original.${extension}`,
|
||||
originalBuffer,
|
||||
inputs.file.type,
|
||||
);
|
||||
|
||||
const outside360Buffer = await image
|
||||
.resize(360, 360, {
|
||||
fit: 'outside',
|
||||
withoutEnlargement: true,
|
||||
})
|
||||
.png({
|
||||
quality: 75,
|
||||
force: false,
|
||||
})
|
||||
.toBuffer();
|
||||
|
||||
await fileManager.save(
|
||||
`${dirPathSegment}/outside-360.${extension}`,
|
||||
outside360Buffer,
|
||||
inputs.file.type,
|
||||
);
|
||||
await Promise.all([
|
||||
fileManager.save(`${dirPathSegment}/original.${extension}`, image, inputs.file.type),
|
||||
fileManager.save(
|
||||
`${dirPathSegment}/outside-360.${extension}`,
|
||||
outside360,
|
||||
inputs.file.type,
|
||||
),
|
||||
]);
|
||||
} catch (error) {
|
||||
sails.log.warn(error.stack);
|
||||
|
||||
|
||||
@@ -39,21 +39,17 @@ module.exports = {
|
||||
});
|
||||
|
||||
let metadata;
|
||||
let originalBuffer;
|
||||
|
||||
try {
|
||||
metadata = await image.metadata();
|
||||
|
||||
if (metadata.orientation && metadata.orientation > 4) {
|
||||
image = image.rotate();
|
||||
}
|
||||
|
||||
originalBuffer = await image.toBuffer();
|
||||
} catch (error) {
|
||||
await rimraf(inputs.file.fd);
|
||||
throw 'fileIsNotImage';
|
||||
}
|
||||
|
||||
if (metadata.orientation && metadata.orientation > 4) {
|
||||
image = image.rotate();
|
||||
}
|
||||
|
||||
const { id: uploadedFileId } = await UploadedFile.qm.createOne({
|
||||
mimeType,
|
||||
size,
|
||||
@@ -64,28 +60,21 @@ module.exports = {
|
||||
const dirPathSegment = `${sails.config.custom.userAvatarsPathSegment}/${uploadedFileId}`;
|
||||
const extension = metadata.format === 'jpeg' ? 'jpg' : metadata.format;
|
||||
|
||||
const cover180 = image
|
||||
.clone()
|
||||
.resize(180, 180, {
|
||||
withoutEnlargement: true,
|
||||
})
|
||||
.png({
|
||||
quality: 75,
|
||||
force: false,
|
||||
});
|
||||
|
||||
try {
|
||||
await fileManager.save(
|
||||
`${dirPathSegment}/original.${extension}`,
|
||||
originalBuffer,
|
||||
inputs.file.type,
|
||||
);
|
||||
|
||||
const cover180Buffer = await image
|
||||
.resize(180, 180, {
|
||||
withoutEnlargement: true,
|
||||
})
|
||||
.png({
|
||||
quality: 75,
|
||||
force: false,
|
||||
})
|
||||
.toBuffer();
|
||||
|
||||
await fileManager.save(
|
||||
`${dirPathSegment}/cover-180.${extension}`,
|
||||
cover180Buffer,
|
||||
inputs.file.type,
|
||||
);
|
||||
await Promise.all([
|
||||
fileManager.save(`${dirPathSegment}/original.${extension}`, image, inputs.file.type),
|
||||
fileManager.save(`${dirPathSegment}/cover-180.${extension}`, cover180, inputs.file.type),
|
||||
]);
|
||||
} catch (error) {
|
||||
sails.log.warn(error.stack);
|
||||
|
||||
|
||||
@@ -162,23 +162,22 @@ module.exports = {
|
||||
const fileManager = sails.hooks['file-manager'].getInstance();
|
||||
const { width, height } = metadata;
|
||||
|
||||
try {
|
||||
const buffer = await image
|
||||
.resize(
|
||||
32,
|
||||
32,
|
||||
width < 32 || height < 32
|
||||
? {
|
||||
kernel: sharp.kernel.nearest,
|
||||
}
|
||||
: undefined,
|
||||
)
|
||||
.png()
|
||||
.toBuffer();
|
||||
image = image
|
||||
.resize(
|
||||
32,
|
||||
32,
|
||||
width < 32 || height < 32
|
||||
? {
|
||||
kernel: sharp.kernel.nearest,
|
||||
}
|
||||
: undefined,
|
||||
)
|
||||
.png();
|
||||
|
||||
try {
|
||||
await fileManager.save(
|
||||
`${sails.config.custom.faviconsPathSegment}/${hostname}.png`,
|
||||
buffer,
|
||||
image,
|
||||
'image/png',
|
||||
);
|
||||
} catch (error) {
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user