diff options
Diffstat (limited to 'server/helpers/utils.ts')
-rw-r--r-- | server/helpers/utils.ts | 28 |
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' | |||
6 | import { UserModel } from '../models/account/user' | 6 | import { UserModel } from '../models/account/user' |
7 | import { ActorModel } from '../models/activitypub/actor' | 7 | import { ActorModel } from '../models/activitypub/actor' |
8 | import { ApplicationModel } from '../models/application/application' | 8 | import { ApplicationModel } from '../models/application/application' |
9 | import { pseudoRandomBytesPromise } from './core-utils' | 9 | import { pseudoRandomBytesPromise, unlinkPromise } from './core-utils' |
10 | import { logger } from './logger' | 10 | import { logger } from './logger' |
11 | import { isArray } from './custom-validators/misc' | ||
11 | 12 | ||
12 | const isCidr = require('is-cidr') | 13 | const isCidr = require('is-cidr') |
13 | 14 | ||
15 | function 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 | |||
33 | function deleteFileAsync (path: string) { | ||
34 | unlinkPromise(path) | ||
35 | .catch(err => logger.error('Cannot delete the file %s asynchronously.', path, { err })) | ||
36 | } | ||
37 | |||
14 | async function generateRandomString (size: number) { | 38 | async 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 | ||
164 | export { | 188 | export { |
189 | cleanUpReqFiles, | ||
190 | deleteFileAsync, | ||
165 | generateRandomString, | 191 | generateRandomString, |
166 | getFormattedObjects, | 192 | getFormattedObjects, |
167 | isSignupAllowed, | 193 | isSignupAllowed, |