Cleanup utils helper

This commit is contained in:
Chocobozzz 2018-08-14 15:28:30 +02:00
parent 59c76ffa8f
commit 06215f15e0
No known key found for this signature in database
GPG key ID: 583A612D890159BE
20 changed files with 173 additions and 166 deletions

View file

@ -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
}