From 32829b0cd0eb6c227e2f5e6011280c37c9f229f9 Mon Sep 17 00:00:00 2001 From: tycrek Date: Mon, 12 Jul 2021 12:20:35 -0600 Subject: [PATCH] more minor log improvements --- routers/upload.js | 5 +++-- storage.js | 18 +++++++++++++----- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/routers/upload.js b/routers/upload.js index a1abb2c..7898865 100644 --- a/routers/upload.js +++ b/routers/upload.js @@ -23,14 +23,14 @@ const router = express.Router(); router.post('/', (req, res, next) => { 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 - !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 //router.post('/', doUpload, processUploaded, ({ next }) => next()); router.post('/', (req, res, next) => doUpload(req, res, (err) => { 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()); 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 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(() => { // Log the upload const logInfo = `${req.file.originalname} (${req.file.mimetype}, ${formatBytes(req.file.size)})`; diff --git a/storage.js b/storage.js index 969a406..0e6b227 100644 --- a/storage.js +++ b/storage.js @@ -21,11 +21,11 @@ const s3 = new aws.S3({ function saveFile(req) { 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) => fs.ensureDir(getDatedDirname()) .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)); } @@ -55,9 +55,17 @@ function processUploaded(req, res, next) { // Block the resource if the mimetype is not an image or video if (mediaStrict && !ALLOWED_MIMETYPES.test(req.file.mimetype)) { - fs.remove(req.file.path.concat('.temp')); - log.warn('Upload blocked', req.file.originalname, req.file.mimetype).warn('Strict media mode', 'only images, videos, & audio are file permitted'); - return res.sendStatus(CODE_UNSUPPORTED_MEDIA_TYPE); + return log + .warn('Upload blocked', req.file.originalname, req.file.mimetype) + .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