diff options
Diffstat (limited to 'server/helpers/express-utils.ts')
-rw-r--r-- | server/helpers/express-utils.ts | 156 |
1 files changed, 0 insertions, 156 deletions
diff --git a/server/helpers/express-utils.ts b/server/helpers/express-utils.ts deleted file mode 100644 index 783097e55..000000000 --- a/server/helpers/express-utils.ts +++ /dev/null | |||
@@ -1,156 +0,0 @@ | |||
1 | import express, { RequestHandler } from 'express' | ||
2 | import multer, { diskStorage } from 'multer' | ||
3 | import { getLowercaseExtension } from '@shared/core-utils' | ||
4 | import { CONFIG } from '../initializers/config' | ||
5 | import { REMOTE_SCHEME } from '../initializers/constants' | ||
6 | import { isArray } from './custom-validators/misc' | ||
7 | import { logger } from './logger' | ||
8 | import { deleteFileAndCatch, generateRandomString } from './utils' | ||
9 | import { getExtFromMimetype } from './video' | ||
10 | |||
11 | function buildNSFWFilter (res?: express.Response, paramNSFW?: string) { | ||
12 | if (paramNSFW === 'true') return true | ||
13 | if (paramNSFW === 'false') return false | ||
14 | if (paramNSFW === 'both') return undefined | ||
15 | |||
16 | if (res?.locals.oauth) { | ||
17 | const user = res.locals.oauth.token.User | ||
18 | |||
19 | // User does not want NSFW videos | ||
20 | if (user.nsfwPolicy === 'do_not_list') return false | ||
21 | |||
22 | // Both | ||
23 | return undefined | ||
24 | } | ||
25 | |||
26 | if (CONFIG.INSTANCE.DEFAULT_NSFW_POLICY === 'do_not_list') return false | ||
27 | |||
28 | // Display all | ||
29 | return null | ||
30 | } | ||
31 | |||
32 | function cleanUpReqFiles (req: express.Request) { | ||
33 | const filesObject = req.files | ||
34 | if (!filesObject) return | ||
35 | |||
36 | if (isArray(filesObject)) { | ||
37 | filesObject.forEach(f => deleteFileAndCatch(f.path)) | ||
38 | return | ||
39 | } | ||
40 | |||
41 | for (const key of Object.keys(filesObject)) { | ||
42 | const files = filesObject[key] | ||
43 | |||
44 | files.forEach(f => deleteFileAndCatch(f.path)) | ||
45 | } | ||
46 | } | ||
47 | |||
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 createReqFiles ( | ||
62 | fieldNames: string[], | ||
63 | mimeTypes: { [id: string]: string | string[] }, | ||
64 | destination = CONFIG.STORAGE.TMP_DIR | ||
65 | ): RequestHandler { | ||
66 | const storage = diskStorage({ | ||
67 | destination: (req, file, cb) => { | ||
68 | cb(null, destination) | ||
69 | }, | ||
70 | |||
71 | filename: (req, file, cb) => { | ||
72 | return generateReqFilename(file, mimeTypes, cb) | ||
73 | } | ||
74 | }) | ||
75 | |||
76 | const fields: { name: string, maxCount: number }[] = [] | ||
77 | for (const fieldName of fieldNames) { | ||
78 | fields.push({ | ||
79 | name: fieldName, | ||
80 | maxCount: 1 | ||
81 | }) | ||
82 | } | ||
83 | |||
84 | return multer({ storage }).fields(fields) | ||
85 | } | ||
86 | |||
87 | function createAnyReqFiles ( | ||
88 | mimeTypes: { [id: string]: string | string[] }, | ||
89 | fileFilter: (req: express.Request, file: Express.Multer.File, cb: (err: Error, result: boolean) => void) => void | ||
90 | ): RequestHandler { | ||
91 | const storage = diskStorage({ | ||
92 | destination: (req, file, cb) => { | ||
93 | cb(null, CONFIG.STORAGE.TMP_DIR) | ||
94 | }, | ||
95 | |||
96 | filename: (req, file, cb) => { | ||
97 | return generateReqFilename(file, mimeTypes, cb) | ||
98 | } | ||
99 | }) | ||
100 | |||
101 | return multer({ storage, fileFilter }).any() | ||
102 | } | ||
103 | |||
104 | function isUserAbleToSearchRemoteURI (res: express.Response) { | ||
105 | const user = res.locals.oauth ? res.locals.oauth.token.User : undefined | ||
106 | |||
107 | return CONFIG.SEARCH.REMOTE_URI.ANONYMOUS === true || | ||
108 | (CONFIG.SEARCH.REMOTE_URI.USERS === true && user !== undefined) | ||
109 | } | ||
110 | |||
111 | function getCountVideos (req: express.Request) { | ||
112 | return req.query.skipCount !== true | ||
113 | } | ||
114 | |||
115 | // --------------------------------------------------------------------------- | ||
116 | |||
117 | export { | ||
118 | buildNSFWFilter, | ||
119 | getHostWithPort, | ||
120 | createAnyReqFiles, | ||
121 | isUserAbleToSearchRemoteURI, | ||
122 | createReqFiles, | ||
123 | cleanUpReqFiles, | ||
124 | getCountVideos | ||
125 | } | ||
126 | |||
127 | // --------------------------------------------------------------------------- | ||
128 | |||
129 | async function generateReqFilename ( | ||
130 | file: Express.Multer.File, | ||
131 | mimeTypes: { [id: string]: string | string[] }, | ||
132 | cb: (err: Error, name: string) => void | ||
133 | ) { | ||
134 | let extension: string | ||
135 | const fileExtension = getLowercaseExtension(file.originalname) | ||
136 | const extensionFromMimetype = getExtFromMimetype(mimeTypes, file.mimetype) | ||
137 | |||
138 | // Take the file extension if we don't understand the mime type | ||
139 | if (!extensionFromMimetype) { | ||
140 | extension = fileExtension | ||
141 | } else { | ||
142 | // Take the first available extension for this mimetype | ||
143 | extension = extensionFromMimetype | ||
144 | } | ||
145 | |||
146 | let randomString = '' | ||
147 | |||
148 | try { | ||
149 | randomString = await generateRandomString(16) | ||
150 | } catch (err) { | ||
151 | logger.error('Cannot generate random string for file name.', { err }) | ||
152 | randomString = 'fake-random-string' | ||
153 | } | ||
154 | |||
155 | cb(null, randomString + extension) | ||
156 | } | ||