feat: Track storage usage

This commit is contained in:
Maksim Eltyshev
2025-08-23 00:03:20 +02:00
parent 2f4bcb0583
commit 4d77a1f596
89 changed files with 1052 additions and 304 deletions

View File

@@ -43,10 +43,10 @@ module.exports = {
}
}
const backgroundImage = await BackgroundImage.qm.deleteOne(inputs.record.id);
const { backgroundImage, uploadedFile } = await BackgroundImage.qm.deleteOne(inputs.record.id);
if (backgroundImage) {
sails.helpers.backgroundImages.removeRelatedFiles(backgroundImage);
sails.helpers.utils.removeUnreferencedUploadedFiles(uploadedFile);
const projectRelatedUserIds = await scoper.getProjectRelatedUserIds();

View File

@@ -17,10 +17,10 @@ module.exports = {
const fileManager = sails.hooks['file-manager'].getInstance();
return {
..._.omit(inputs.record, ['dirname', 'extension']),
url: `${fileManager.buildUrl(`${sails.config.custom.backgroundImagesPathSegment}/${inputs.record.dirname}/original.${inputs.record.extension}`)}`,
..._.omit(inputs.record, ['uploadedFileId', 'extension']),
url: `${fileManager.buildUrl(`${sails.config.custom.backgroundImagesPathSegment}/${inputs.record.uploadedFileId}/original.${inputs.record.extension}`)}`,
thumbnailUrls: {
outside360: `${fileManager.buildUrl(`${sails.config.custom.backgroundImagesPathSegment}/${inputs.record.dirname}/outside-360.${inputs.record.extension}`)}`,
outside360: `${fileManager.buildUrl(`${sails.config.custom.backgroundImagesPathSegment}/${inputs.record.uploadedFileId}/outside-360.${inputs.record.extension}`)}`,
},
};
},

View File

@@ -3,9 +3,9 @@
* Licensed under the Fair Use License: https://github.com/plankanban/planka/blob/master/LICENSE.md
*/
const { v4: uuid } = require('uuid');
const { rimraf } = require('rimraf');
const mime = require('mime');
const { v4: uuid } = require('uuid');
const sharp = require('sharp');
module.exports = {
@@ -32,8 +32,16 @@ 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';
@@ -41,20 +49,19 @@ module.exports = {
const fileManager = sails.hooks['file-manager'].getInstance();
const dirname = uuid();
const dirPathSegment = `${sails.config.custom.backgroundImagesPathSegment}/${dirname}`;
if (metadata.orientation && metadata.orientation > 4) {
image = image.rotate();
}
const extension = metadata.format === 'jpeg' ? 'jpg' : metadata.format;
const size = originalBuffer.length;
const { id: uploadedFileId } = await UploadedFile.qm.createOne({
mimeType,
size,
id: uuid(),
type: UploadedFile.Types.BACKGROUND_IMAGE,
});
const dirPathSegment = `${sails.config.custom.backgroundImagesPathSegment}/${uploadedFileId}`;
let sizeInBytes;
try {
const originalBuffer = await image.toBuffer();
sizeInBytes = originalBuffer.length;
await fileManager.save(
`${dirPathSegment}/original.${extension}`,
originalBuffer,
@@ -82,6 +89,7 @@ module.exports = {
await fileManager.deleteDir(dirPathSegment);
await rimraf(inputs.file.fd);
await UploadedFile.qm.deleteOne(uploadedFileId);
throw 'fileIsNotImage';
}
@@ -89,9 +97,9 @@ module.exports = {
await rimraf(inputs.file.fd);
return {
dirname,
uploadedFileId,
extension,
sizeInBytes,
size,
};
},
};

View File

@@ -1,29 +0,0 @@
/*!
* Copyright (c) 2024 PLANKA Software GmbH
* Licensed under the Fair Use License: https://github.com/plankanban/planka/blob/master/LICENSE.md
*/
module.exports = {
sync: true,
inputs: {
recordOrRecords: {
type: 'ref',
required: true,
},
},
fn(inputs) {
const backgroundImages = _.isPlainObject(inputs.recordOrRecords)
? [inputs.recordOrRecords]
: inputs.recordOrRecords;
const fileManager = sails.hooks['file-manager'].getInstance();
backgroundImages.forEach(async (backgroundImage) => {
await fileManager.deleteDir(
`${sails.config.custom.backgroundImagesPathSegment}/${backgroundImage.dirname}`,
);
});
},
};