From 06215f15e0a9fea2ef95b8b49cb2b5868fb64017 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Tue, 14 Aug 2018 15:28:30 +0200 Subject: Cleanup utils helper --- server/helpers/express-utils.ts | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) (limited to 'server/helpers/express-utils.ts') diff --git a/server/helpers/express-utils.ts b/server/helpers/express-utils.ts index b3cc40848..1d7bee87e 100644 --- a/server/helpers/express-utils.ts +++ b/server/helpers/express-utils.ts @@ -3,8 +3,9 @@ import * as multer from 'multer' import { CONFIG, REMOTE_SCHEME } from '../initializers' import { logger } from './logger' import { User } from '../../shared/models/users' -import { generateRandomString } from './utils' +import { deleteFileAsync, generateRandomString } from './utils' import { extname } from 'path' +import { isArray } from './custom-validators/misc' function buildNSFWFilter (res: express.Response, paramNSFW?: string) { if (paramNSFW === 'true') return true @@ -23,6 +24,24 @@ function buildNSFWFilter (res: express.Response, paramNSFW?: string) { return null } +function cleanUpReqFiles (req: { files: { [ fieldname: string ]: Express.Multer.File[] } | Express.Multer.File[] }) { + const files = req.files + + if (!files) return + + if (isArray(files)) { + (files as Express.Multer.File[]).forEach(f => deleteFileAsync(f.path)) + return + } + + for (const key of Object.keys(files)) { + const file = files[ key ] + + if (isArray(file)) file.forEach(f => deleteFileAsync(f.path)) + else deleteFileAsync(file.path) + } +} + function getHostWithPort (host: string) { const splitted = host.split(':') @@ -82,5 +101,6 @@ export { buildNSFWFilter, getHostWithPort, badRequest, - createReqFiles + createReqFiles, + cleanUpReqFiles } -- cgit v1.2.3