more minor log improvements

pull/29/head
tycrek 3 years ago
parent 9f4801a4e3
commit 32829b0cd0
No known key found for this signature in database
GPG Key ID: 25D74F3943625263

@ -23,14 +23,14 @@ const router = express.Router();
router.post('/', (req, res, next) => { router.post('/', (req, res, next) => {
req.headers.authorization = req.headers.authorization || ''; req.headers.authorization = req.headers.authorization || '';
req.token = req.headers.authorization.replace(/[^\da-z]/gi, ''); // Strip anything that isn't a digit or ASCII letter req.token = req.headers.authorization.replace(/[^\da-z]/gi, ''); // Strip anything that isn't a digit or ASCII letter
!verify(req, users) ? log.warn('Upload blocked', 'Unauthorized').callback(res.sendStatus, CODE_UNAUTHORIZED) : next(); // skipcq: JS-0093 !verify(req, users) ? log.warn('Upload blocked', 'Unauthorized').callback(() => res.sendStatus(CODE_UNAUTHORIZED)) : next(); // skipcq: JS-0093
}); });
// Upload file // Upload file
//router.post('/', doUpload, processUploaded, ({ next }) => next()); //router.post('/', doUpload, processUploaded, ({ next }) => next());
router.post('/', (req, res, next) => doUpload(req, res, (err) => { router.post('/', (req, res, next) => doUpload(req, res, (err) => {
log.express().Header(req, 'Content-Type'); log.express().Header(req, 'Content-Type');
(err) ? log.error(`Multer encountered an ${!(err instanceof multer.MulterError) ? 'unknown ' : ''}error`, err).callback(next, err) : log.debug('Multer', 'File saved in temp dir').callback(next); (err) ? log.error(`Multer encountered an ${!(err.toString().includes('MulterError')) ? 'unknown ' : ''}error`, err).callback(next, err) : log.debug('Multer', 'File saved in temp dir').callback(next);
}), processUploaded, ({ next }) => next()); }), processUploaded, ({ next }) => next());
router.use('/', (err, _req, res, next) => err.code && err.code === 'LIMIT_FILE_SIZE' ? log.warn('Upload blocked', 'File too large').callback(() => res.status(CODE_PAYLOAD_TOO_LARGE).send(`Max upload size: ${maxUploadSize}MB`)) : next(err)); // skipcq: JS-0229 router.use('/', (err, _req, res, next) => err.code && err.code === 'LIMIT_FILE_SIZE' ? log.warn('Upload blocked', 'File too large').callback(() => res.status(CODE_PAYLOAD_TOO_LARGE).send(`Max upload size: ${maxUploadSize}MB`)) : next(err)); // skipcq: JS-0229
@ -63,6 +63,7 @@ router.post('/', (req, res, next) => {
// Save the file information // Save the file information
const resourceId = generateId(generator, resourceIdSize, req.headers['x-ass-gfycat'] || gfyIdSize, req.file.originalname); const resourceId = generateId(generator, resourceIdSize, req.headers['x-ass-gfycat'] || gfyIdSize, req.file.originalname);
log.debug('Saving data', data.name);
data.put(resourceId.split('.')[0], req.file).then(() => { data.put(resourceId.split('.')[0], req.file).then(() => {
// Log the upload // Log the upload
const logInfo = `${req.file.originalname} (${req.file.mimetype}, ${formatBytes(req.file.size)})`; const logInfo = `${req.file.originalname} (${req.file.mimetype}, ${formatBytes(req.file.size)})`;

@ -21,11 +21,11 @@ const s3 = new aws.S3({
function saveFile(req) { function saveFile(req) {
log.null(req.file, 'Unable to save req.file!') log.null(req.file, 'Unable to save req.file!')
.debug('Saving temp file to disk', req.file.path, formatBytes(req.file.size)); .debug('Temp file saving');
return new Promise((resolve, reject) => return new Promise((resolve, reject) =>
fs.ensureDir(getDatedDirname()) fs.ensureDir(getDatedDirname())
.then(() => fs.createWriteStream(req.file.path.concat('.temp'))) .then(() => fs.createWriteStream(req.file.path.concat('.temp')))
.then((stream) => req.file.stream.pipe(stream).on('finish', () => log.debug('Temp file', 'saved').callback(resolve)).on('error', reject)) .then((stream) => req.file.stream.pipe(stream).on('finish', () => log.debug('Temp file saved', req.file.path, formatBytes(req.file.size)).callback(resolve)).on('error', reject))
.catch(reject)); .catch(reject));
} }
@ -55,9 +55,17 @@ function processUploaded(req, res, next) {
// Block the resource if the mimetype is not an image or video // Block the resource if the mimetype is not an image or video
if (mediaStrict && !ALLOWED_MIMETYPES.test(req.file.mimetype)) { if (mediaStrict && !ALLOWED_MIMETYPES.test(req.file.mimetype)) {
fs.remove(req.file.path.concat('.temp')); return log
log.warn('Upload blocked', req.file.originalname, req.file.mimetype).warn('Strict media mode', 'only images, videos, & audio are file permitted'); .warn('Upload blocked', req.file.originalname, req.file.mimetype)
return res.sendStatus(CODE_UNSUPPORTED_MEDIA_TYPE); .warn('Strict media mode', 'only images, videos, & audio are file permitted')
.callback(() =>
fs.remove(req.file.path.concat('.temp'))
.then(() => log
.debug('Temp file', 'deleted')
.callback(() => res.sendStatus(CODE_UNSUPPORTED_MEDIA_TYPE)))
.catch((err) => log
.error('Temp file could not be deleted', err)
.callback(next, err)));
} }
// Remove unwanted fields // Remove unwanted fields

Loading…
Cancel
Save