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

@@ -8,7 +8,7 @@ const icoToPng = require('ico-to-png');
const sharp = require('sharp');
const FETCH_TIMEOUT = 4000;
const MAX_RESPONSE_LENGTH_IN_BYTES = 1024 * 1024;
const MAX_RESPONSE_LENGTH = 1024 * 1024;
const FAVICON_TAGS_REGEX = /<link [^>]*rel="([^"]* )?icon( [^"]*)?"[^>]*>/gi;
const HREF_REGEX = /href="(.*?)"/i;
@@ -39,7 +39,7 @@ const readResponse = async (response) => {
chunks.push(value);
receivedLength += value.length;
if (receivedLength > MAX_RESPONSE_LENGTH_IN_BYTES) {
if (receivedLength > MAX_RESPONSE_LENGTH) {
reader.cancel();
return {
@@ -133,6 +133,12 @@ module.exports = {
return;
}
const availableStorage = await sails.helpers.utils.getAvailableStorage();
if (availableStorage !== null && readedResponse.buffer.length >= availableStorage) {
return;
}
let image = sharp(readedResponse.buffer);
let metadata;

View File

@@ -0,0 +1,17 @@
/*!
* Copyright (c) 2024 PLANKA Software GmbH
* Licensed under the Fair Use License: https://github.com/plankanban/planka/blob/master/LICENSE.md
*/
module.exports = {
async fn() {
const { storageLimit } = sails.config.custom;
if (_.isNil(storageLimit)) {
return null;
}
const storageUsage = await StorageUsage.qm.getOneMain();
return BigInt(storageLimit) - BigInt(storageUsage.total);
},
};

View File

@@ -4,37 +4,55 @@
*/
const util = require('util');
const { v4: uuid } = require('uuid');
module.exports = {
friendlyName: 'Receive uploaded file from request',
description:
'Store a file uploaded from a MIME-multipart request part. The resulting file will have a unique UUID-based name with the same extension.',
inputs: {
paramName: {
type: 'string',
required: true,
description: 'The MIME multi-part parameter containing the file to receive.',
},
req: {
file: {
type: 'ref',
required: true,
description: 'The request to receive the file from.',
},
enforceStorageLimit: {
type: 'boolean',
defaultsTo: true,
},
},
async fn(inputs, exits) {
const { maxUploadFileSize } = sails.config.custom;
let availableStorage = null;
if (inputs.enforceStorageLimit) {
availableStorage = await sails.helpers.utils.getAvailableStorage();
}
let maxBytes = _.isNil(maxUploadFileSize) ? null : maxUploadFileSize;
if (availableStorage !== null) {
if (maxBytes) {
maxBytes = availableStorage < maxBytes ? availableStorage : maxBytes;
} else {
maxBytes = availableStorage;
}
}
const upload = util.promisify((options, callback) =>
inputs.req.file(inputs.paramName).upload(options, (error, files) => callback(error, files)),
inputs.file.upload(options, (error, files) => {
if (
error &&
error.code === 'E_EXCEEDS_UPLOAD_LIMIT' &&
availableStorage !== null &&
(_.isNil(maxUploadFileSize) || error.maxBytes < maxUploadFileSize)
) {
return callback(new Error('Storage limit reached'), files);
}
return callback(error, files);
}),
);
return exits.success(
await upload({
maxBytes,
dirname: sails.config.custom.uploadsTempPath,
saveAs: uuid(),
maxBytes: null,
}),
);
},

View File

@@ -0,0 +1,41 @@
/*!
* Copyright (c) 2024 PLANKA Software GmbH
* Licensed under the Fair Use License: https://github.com/plankanban/planka/blob/master/LICENSE.md
*/
const { Types } = require('../../models/UploadedFile');
const PATH_SEGMENT_BY_TYPE = {
[Types.USER_AVATAR]: sails.config.custom.userAvatarsPathSegment,
[Types.BACKGROUND_IMAGE]: sails.config.custom.backgroundImagesPathSegment,
[Types.ATTACHMENT]: sails.config.custom.attachmentsPathSegment,
};
module.exports = {
sync: true,
inputs: {
uploadedFileOrUploadedFiles: {
type: 'ref',
required: true,
},
},
fn(inputs) {
const uploadedFiles = _.isPlainObject(inputs.uploadedFileOrUploadedFiles)
? [inputs.uploadedFileOrUploadedFiles]
: inputs.uploadedFileOrUploadedFiles;
const fileManager = sails.hooks['file-manager'].getInstance();
// TODO: optimize?
uploadedFiles.forEach(async (uploadedFile) => {
if (uploadedFile.referencesTotal !== null) {
return;
}
await fileManager.deleteDir(`${PATH_SEGMENT_BY_TYPE[uploadedFile.type]}/${uploadedFile.id}`);
await UploadedFile.qm.deleteOne(uploadedFile.id);
});
},
};