|
|
|
@ -6,8 +6,8 @@ const aws = require('aws-sdk');
|
|
|
|
|
const Thumbnail = require('./thumbnails').default;
|
|
|
|
|
const Vibrant = require('./vibrant').default;
|
|
|
|
|
const Hash = require('./hash').default;
|
|
|
|
|
const { getDatedDirname, generateId, log } = require('./utils');
|
|
|
|
|
const { s3enabled, s3endpoint, s3bucket, s3usePathStyle, s3accessKey, s3secretKey, saveAsOriginal, mediaStrict, maxUploadSize } = require('../config.json');
|
|
|
|
|
const { generateId, log } = require('./utils');
|
|
|
|
|
const { s3enabled, s3endpoint, s3bucket, s3usePathStyle, s3accessKey, s3secretKey, diskFilePath, saveAsOriginal, saveWithDate, mediaStrict, maxUploadSize } = require('../config.json');
|
|
|
|
|
const { CODE_UNSUPPORTED_MEDIA_TYPE } = require('../MagicNumbers.json');
|
|
|
|
|
|
|
|
|
|
const ID_GEN_LENGTH = 32;
|
|
|
|
@ -19,6 +19,16 @@ const s3 = new aws.S3({
|
|
|
|
|
credentials: new aws.Credentials({ accessKeyId: s3accessKey, secretAccessKey: s3secretKey })
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
function getDatedDirname() {
|
|
|
|
|
if (!saveWithDate) return diskFilePath;
|
|
|
|
|
|
|
|
|
|
// Get current month and year
|
|
|
|
|
const [month, , year] = new Date().toLocaleDateString('en-US').split('/');
|
|
|
|
|
|
|
|
|
|
// Add 0 before single digit months (6 turns into 06)
|
|
|
|
|
return `${diskFilePath}${diskFilePath.endsWith('/') ? '' : '/'}${year}-${`0${month}`.slice(-2)}`; // skipcq: JS-0074
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getLocalFilename(req) {
|
|
|
|
|
return `${getDatedDirname()}/${saveAsOriginal ? req.file.originalname : req.file.sha1}`;
|
|
|
|
|
}
|
|
|
|
@ -103,12 +113,16 @@ function processUploaded(req, res, next) { // skipcq: JS-0045
|
|
|
|
|
.catch(reject)
|
|
|
|
|
))
|
|
|
|
|
.then(() => log.debug('File saved', req.file.originalname, s3enabled ? 'in S3' : 'on disk'))
|
|
|
|
|
.then(() => !s3enabled && (req.file.path = getLocalFilename(req))) // skipcq: JS-0090
|
|
|
|
|
.then(() => next())
|
|
|
|
|
.catch(next)
|
|
|
|
|
.finally(() => fs.remove(req.file.path))
|
|
|
|
|
|
|
|
|
|
// Delete the file
|
|
|
|
|
.then(() => fs.remove(req.file.path))
|
|
|
|
|
.then(() => log.debug('Temp file', 'deleted'))
|
|
|
|
|
.catch((err) => log.err(err));
|
|
|
|
|
|
|
|
|
|
// Fix the file path
|
|
|
|
|
.then(() => !s3enabled && (req.file.path = getLocalFilename(req))) // skipcq: JS-0090
|
|
|
|
|
.then(() => next())
|
|
|
|
|
.catch(next);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function deleteS3(file) {
|
|
|
|
|