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.ts30
1 files changed, 28 insertions, 2 deletions
diff --git a/server/helpers/utils.ts b/server/helpers/utils.ts
index 769aa83c6..7a32e286c 100644
--- a/server/helpers/utils.ts
+++ b/server/helpers/utils.ts
@@ -1,8 +1,9 @@
1import * as express from 'express' 1import * as express from 'express'
2import * as multer from 'multer'
2import { Model } from 'sequelize-typescript' 3import { Model } from 'sequelize-typescript'
3import { ResultList } from '../../shared' 4import { ResultList } from '../../shared'
4import { VideoResolution } from '../../shared/models/videos' 5import { VideoResolution } from '../../shared/models/videos'
5import { CONFIG, REMOTE_SCHEME } from '../initializers' 6import { CONFIG, REMOTE_SCHEME, VIDEO_MIMETYPE_EXT } from '../initializers'
6import { UserModel } from '../models/account/user' 7import { UserModel } from '../models/account/user'
7import { ActorModel } from '../models/activitypub/actor' 8import { ActorModel } from '../models/activitypub/actor'
8import { ApplicationModel } from '../models/application/application' 9import { ApplicationModel } from '../models/application/application'
@@ -26,6 +27,30 @@ function badRequest (req: express.Request, res: express.Response, next: express.
26 return res.type('json').status(400).end() 27 return res.type('json').status(400).end()
27} 28}
28 29
30function createReqFiles (fieldName: string, storageDir: string, mimeTypes: { [ id: string ]: string }) {
31 const storage = multer.diskStorage({
32 destination: (req, file, cb) => {
33 cb(null, storageDir)
34 },
35
36 filename: async (req, file, cb) => {
37 const extension = mimeTypes[file.mimetype]
38 let randomString = ''
39
40 try {
41 randomString = await generateRandomString(16)
42 } catch (err) {
43 logger.error('Cannot generate random string for file name.', err)
44 randomString = 'fake-random-string'
45 }
46
47 cb(null, randomString + extension)
48 }
49 })
50
51 return multer({ storage }).fields([{ name: fieldName, maxCount: 1 }])
52}
53
29async function generateRandomString (size: number) { 54async function generateRandomString (size: number) {
30 const raw = await pseudoRandomBytesPromise(size) 55 const raw = await pseudoRandomBytesPromise(size)
31 56
@@ -122,5 +147,6 @@ export {
122 resetSequelizeInstance, 147 resetSequelizeInstance,
123 getServerActor, 148 getServerActor,
124 SortType, 149 SortType,
125 getHostWithPort 150 getHostWithPort,
151 createReqFiles
126} 152}