From c158a5faabb8ef0bc5d121fda4522d63603e8bc5 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Wed, 12 May 2021 14:51:17 +0200 Subject: Refactor a little bit controllers --- server/helpers/custom-validators/misc.ts | 2 +- server/helpers/express-utils.ts | 28 ++++++++++++++-------------- 2 files changed, 15 insertions(+), 15 deletions(-) (limited to 'server/helpers') diff --git a/server/helpers/custom-validators/misc.ts b/server/helpers/custom-validators/misc.ts index fd3b45804..229e9f03c 100644 --- a/server/helpers/custom-validators/misc.ts +++ b/server/helpers/custom-validators/misc.ts @@ -14,7 +14,7 @@ function isSafePath (p: string) { }) } -function isArray (value: any) { +function isArray (value: any): value is any[] { return Array.isArray(value) } diff --git a/server/helpers/express-utils.ts b/server/helpers/express-utils.ts index ede22a3cc..010c6961a 100644 --- a/server/helpers/express-utils.ts +++ b/server/helpers/express-utils.ts @@ -1,13 +1,13 @@ import * as express from 'express' import * as multer from 'multer' +import { extname } from 'path' +import { HttpStatusCode } from '../../shared/core-utils/miscs/http-error-codes' +import { CONFIG } from '../initializers/config' import { REMOTE_SCHEME } from '../initializers/constants' +import { isArray } from './custom-validators/misc' import { logger } from './logger' import { deleteFileAndCatch, generateRandomString } from './utils' -import { extname } from 'path' -import { isArray } from './custom-validators/misc' -import { CONFIG } from '../initializers/config' import { getExtFromMimetype } from './video' -import { HttpStatusCode } from '../../shared/core-utils/miscs/http-error-codes' function buildNSFWFilter (res?: express.Response, paramNSFW?: string) { if (paramNSFW === 'true') return true @@ -30,21 +30,21 @@ 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 +function cleanUpReqFiles ( + req: { files: { [fieldname: string]: Express.Multer.File[] } | Express.Multer.File[] } +) { + const filesObject = req.files + if (!filesObject) return - if (isArray(files)) { - (files as Express.Multer.File[]).forEach(f => deleteFileAndCatch(f.path)) + if (isArray(filesObject)) { + filesObject.forEach(f => deleteFileAndCatch(f.path)) return } - for (const key of Object.keys(files)) { - const file = files[key] + for (const key of Object.keys(filesObject)) { + const files = filesObject[key] - if (isArray(file)) file.forEach(f => deleteFileAndCatch(f.path)) - else deleteFileAndCatch(file.path) + files.forEach(f => deleteFileAndCatch(f.path)) } } -- cgit v1.2.3