aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/helpers/utils.ts
diff options
context:
space:
mode:
Diffstat (limited to 'server/helpers/utils.ts')
-rw-r--r--server/helpers/utils.ts28
1 files changed, 27 insertions, 1 deletions
diff --git a/server/helpers/utils.ts b/server/helpers/utils.ts
index cfb427570..7abcec5d7 100644
--- a/server/helpers/utils.ts
+++ b/server/helpers/utils.ts
@@ -6,11 +6,35 @@ import { CONFIG } from '../initializers'
6import { UserModel } from '../models/account/user' 6import { UserModel } from '../models/account/user'
7import { ActorModel } from '../models/activitypub/actor' 7import { ActorModel } from '../models/activitypub/actor'
8import { ApplicationModel } from '../models/application/application' 8import { ApplicationModel } from '../models/application/application'
9import { pseudoRandomBytesPromise } from './core-utils' 9import { pseudoRandomBytesPromise, unlinkPromise } from './core-utils'
10import { logger } from './logger' 10import { logger } from './logger'
11import { isArray } from './custom-validators/misc'
11 12
12const isCidr = require('is-cidr') 13const isCidr = require('is-cidr')
13 14
15function cleanUpReqFiles (req: { files: { [ fieldname: string ]: Express.Multer.File[] } | Express.Multer.File[] }) {
16 const files = req.files
17
18 if (!files) return
19
20 if (isArray(files)) {
21 (files as Express.Multer.File[]).forEach(f => deleteFileAsync(f.path))
22 return
23 }
24
25 for (const key of Object.keys(files)) {
26 const file = files[key]
27
28 if (isArray(file)) file.forEach(f => deleteFileAsync(f.path))
29 else deleteFileAsync(file.path)
30 }
31}
32
33function deleteFileAsync (path: string) {
34 unlinkPromise(path)
35 .catch(err => logger.error('Cannot delete the file %s asynchronously.', path, { err }))
36}
37
14async function generateRandomString (size: number) { 38async function generateRandomString (size: number) {
15 const raw = await pseudoRandomBytesPromise(size) 39 const raw = await pseudoRandomBytesPromise(size)
16 40
@@ -162,6 +186,8 @@ type SortType = { sortModel: any, sortValue: string }
162// --------------------------------------------------------------------------- 186// ---------------------------------------------------------------------------
163 187
164export { 188export {
189 cleanUpReqFiles,
190 deleteFileAsync,
165 generateRandomString, 191 generateRandomString,
166 getFormattedObjects, 192 getFormattedObjects,
167 isSignupAllowed, 193 isSignupAllowed,