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