mirror of
https://github.com/plankanban/planka.git
synced 2025-07-18 20:59:44 +02:00
feat: Configurable file storage locations (#886)
* feat: Make logfile location customizable It may be desirable to log to a more standard location (e.g. in /var/log/), or in some cases to turn logging to file off. To support these, use a custom config property to determine the location of the output log file, and default to the previous location if it is unset. * feat: Support alternate storage locations for uploaded files This involves a couple primary changes: 1) to make Sails' temporary file-upload directory a configurable location by using a common file-upload-receiving helper; 2) to create custom static routes for the file-upload locations, so they can be outside the application's public directory; and 3) to use the file-uploading handler everywhere that receives files, so config for the helper is applied to all file uploads consistently. This is sufficient to allow the application directory to be deployed read- only, with writable storage used for file uploads. The new config property for Sails' temporary upload directory, combined with the existing settings for user-avatar and background-image locations are sufficient to handle uploads; the new custom routes handle serving those files from external locations. The default behavior of the application should be unchanged, with files uploaded to, and served from, the public directory if the relevant config properties aren't set to other values.
This commit is contained in:
parent
fca77c02b3
commit
37fc7847e8
11 changed files with 151 additions and 43 deletions
|
@ -1,6 +1,3 @@
|
|||
const util = require('util');
|
||||
const { v4: uuid } = require('uuid');
|
||||
|
||||
const Errors = {
|
||||
NOT_ENOUGH_RIGHTS: {
|
||||
notEnoughRights: 'Not enough rights',
|
||||
|
@ -61,16 +58,9 @@ module.exports = {
|
|||
throw Errors.NOT_ENOUGH_RIGHTS;
|
||||
}
|
||||
|
||||
const upload = util.promisify((options, callback) =>
|
||||
this.req.file('file').upload(options, (error, files) => callback(error, files)),
|
||||
);
|
||||
|
||||
let files;
|
||||
try {
|
||||
files = await upload({
|
||||
saveAs: uuid(),
|
||||
maxBytes: null,
|
||||
});
|
||||
files = await sails.helpers.utils.receiveFile('file', this.req);
|
||||
} catch (error) {
|
||||
return exits.uploadError(error.message); // TODO: add error
|
||||
}
|
||||
|
|
|
@ -1,6 +1,3 @@
|
|||
const util = require('util');
|
||||
const { v4: uuid } = require('uuid');
|
||||
|
||||
const Errors = {
|
||||
PROJECT_NOT_FOUND: {
|
||||
projectNotFound: 'Project not found',
|
||||
|
@ -69,16 +66,9 @@ module.exports = {
|
|||
|
||||
let boardImport;
|
||||
if (inputs.importType && Object.values(Board.ImportTypes).includes(inputs.importType)) {
|
||||
const upload = util.promisify((options, callback) =>
|
||||
this.req.file('importFile').upload(options, (error, files) => callback(error, files)),
|
||||
);
|
||||
|
||||
let files;
|
||||
try {
|
||||
files = await upload({
|
||||
saveAs: uuid(),
|
||||
maxBytes: null,
|
||||
});
|
||||
files = await sails.helpers.utils.receiveFile('importFile', this.req);
|
||||
} catch (error) {
|
||||
return exits.uploadError(error.message); // TODO: add error
|
||||
}
|
||||
|
|
|
@ -1,6 +1,4 @@
|
|||
const util = require('util');
|
||||
const rimraf = require('rimraf');
|
||||
const { v4: uuid } = require('uuid');
|
||||
|
||||
const Errors = {
|
||||
PROJECT_NOT_FOUND: {
|
||||
|
@ -53,16 +51,9 @@ module.exports = {
|
|||
throw Errors.PROJECT_NOT_FOUND; // Forbidden
|
||||
}
|
||||
|
||||
const upload = util.promisify((options, callback) =>
|
||||
this.req.file('file').upload(options, (error, files) => callback(error, files)),
|
||||
);
|
||||
|
||||
let files;
|
||||
try {
|
||||
files = await upload({
|
||||
saveAs: uuid(),
|
||||
maxBytes: null,
|
||||
});
|
||||
files = await sails.helpers.utils.receiveFile('file', this.req);
|
||||
} catch (error) {
|
||||
return exits.uploadError(error.message); // TODO: add error
|
||||
}
|
||||
|
|
|
@ -1,6 +1,4 @@
|
|||
const util = require('util');
|
||||
const rimraf = require('rimraf');
|
||||
const { v4: uuid } = require('uuid');
|
||||
|
||||
const Errors = {
|
||||
USER_NOT_FOUND: {
|
||||
|
@ -54,16 +52,9 @@ module.exports = {
|
|||
user = currentUser;
|
||||
}
|
||||
|
||||
const upload = util.promisify((options, callback) =>
|
||||
this.req.file('file').upload(options, (error, files) => callback(error, files)),
|
||||
);
|
||||
|
||||
let files;
|
||||
try {
|
||||
files = await upload({
|
||||
saveAs: uuid(),
|
||||
maxBytes: null,
|
||||
});
|
||||
files = await sails.helpers.utils.receiveFile('file', this.req);
|
||||
} catch (error) {
|
||||
return exits.uploadError(error.message); // TODO: add error
|
||||
}
|
||||
|
|
41
server/api/helpers/utils/receive-file.js
Normal file
41
server/api/helpers/utils/receive-file.js
Normal file
|
@ -0,0 +1,41 @@
|
|||
const util = require('util');
|
||||
const { v4: uuid } = require('uuid');
|
||||
|
||||
async function doUpload(paramName, req, options) {
|
||||
const uploadOptions = {
|
||||
...options,
|
||||
dirname: options.dirname || sails.config.custom.fileUploadTmpDir,
|
||||
};
|
||||
const upload = util.promisify((opts, callback) => {
|
||||
return req.file(paramName).upload(opts, (error, files) => callback(error, files));
|
||||
});
|
||||
return upload(uploadOptions);
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
friendlyName: 'Receive uploaded file from request',
|
||||
description:
|
||||
"Store a file uploaded from a MIME-multipart request part. The request part name must be 'file'; 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: {
|
||||
type: 'ref',
|
||||
required: true,
|
||||
description: 'The request to receive the file from.',
|
||||
},
|
||||
},
|
||||
|
||||
fn: async function modFn(inputs, exits) {
|
||||
exits.success(
|
||||
await doUpload(inputs.paramName, inputs.req, {
|
||||
saveAs: uuid(),
|
||||
dirname: sails.config.custom.fileUploadTmpDir,
|
||||
maxBytes: null,
|
||||
}),
|
||||
);
|
||||
},
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue