]>
Commit | Line | Data |
---|---|---|
0626e7af C |
1 | import * as express from 'express' |
2 | import * as multer from 'multer' | |
3 | import { CONFIG, REMOTE_SCHEME } from '../initializers' | |
4 | import { logger } from './logger' | |
5 | import { User } from '../../shared/models/users' | |
06215f15 | 6 | import { deleteFileAsync, generateRandomString } from './utils' |
2769e297 | 7 | import { extname } from 'path' |
06215f15 | 8 | import { isArray } from './custom-validators/misc' |
0626e7af | 9 | |
0b18f4aa C |
10 | function buildNSFWFilter (res: express.Response, paramNSFW?: string) { |
11 | if (paramNSFW === 'true') return true | |
12 | if (paramNSFW === 'false') return false | |
13 | if (paramNSFW === 'both') return undefined | |
d525fc39 | 14 | |
0626e7af C |
15 | if (res.locals.oauth) { |
16 | const user: User = res.locals.oauth.token.User | |
d525fc39 C |
17 | // User does not want NSFW videos |
18 | if (user && user.nsfwPolicy === 'do_not_list') return false | |
0626e7af C |
19 | } |
20 | ||
d525fc39 C |
21 | if (CONFIG.INSTANCE.DEFAULT_NSFW_POLICY === 'do_not_list') return false |
22 | ||
23 | // Display all | |
24 | return null | |
0626e7af C |
25 | } |
26 | ||
06215f15 C |
27 | function cleanUpReqFiles (req: { files: { [ fieldname: string ]: Express.Multer.File[] } | Express.Multer.File[] }) { |
28 | const files = req.files | |
29 | ||
30 | if (!files) return | |
31 | ||
32 | if (isArray(files)) { | |
33 | (files as Express.Multer.File[]).forEach(f => deleteFileAsync(f.path)) | |
34 | return | |
35 | } | |
36 | ||
37 | for (const key of Object.keys(files)) { | |
38 | const file = files[ key ] | |
39 | ||
40 | if (isArray(file)) file.forEach(f => deleteFileAsync(f.path)) | |
41 | else deleteFileAsync(file.path) | |
42 | } | |
43 | } | |
44 | ||
0626e7af C |
45 | function getHostWithPort (host: string) { |
46 | const splitted = host.split(':') | |
47 | ||
48 | // The port was not specified | |
49 | if (splitted.length === 1) { | |
50 | if (REMOTE_SCHEME.HTTP === 'https') return host + ':443' | |
51 | ||
52 | return host + ':80' | |
53 | } | |
54 | ||
55 | return host | |
56 | } | |
57 | ||
58 | function badRequest (req: express.Request, res: express.Response, next: express.NextFunction) { | |
59 | return res.type('json').status(400).end() | |
60 | } | |
61 | ||
62 | function createReqFiles ( | |
63 | fieldNames: string[], | |
64 | mimeTypes: { [ id: string ]: string }, | |
65 | destinations: { [ fieldName: string ]: string } | |
66 | ) { | |
67 | const storage = multer.diskStorage({ | |
68 | destination: (req, file, cb) => { | |
69 | cb(null, destinations[ file.fieldname ]) | |
70 | }, | |
71 | ||
72 | filename: async (req, file, cb) => { | |
2769e297 | 73 | const extension = mimeTypes[ file.mimetype ] || extname(file.originalname) |
0626e7af C |
74 | let randomString = '' |
75 | ||
76 | try { | |
77 | randomString = await generateRandomString(16) | |
78 | } catch (err) { | |
79 | logger.error('Cannot generate random string for file name.', { err }) | |
80 | randomString = 'fake-random-string' | |
81 | } | |
82 | ||
83 | cb(null, randomString + extension) | |
84 | } | |
85 | }) | |
86 | ||
c1e791ba | 87 | let fields: { name: string, maxCount: number }[] = [] |
0626e7af C |
88 | for (const fieldName of fieldNames) { |
89 | fields.push({ | |
90 | name: fieldName, | |
91 | maxCount: 1 | |
92 | }) | |
93 | } | |
94 | ||
95 | return multer({ storage }).fields(fields) | |
96 | } | |
97 | ||
98 | // --------------------------------------------------------------------------- | |
99 | ||
100 | export { | |
d525fc39 | 101 | buildNSFWFilter, |
0626e7af C |
102 | getHostWithPort, |
103 | badRequest, | |
06215f15 C |
104 | createReqFiles, |
105 | cleanUpReqFiles | |
0626e7af | 106 | } |