]>
Commit | Line | Data |
---|---|---|
06aad801 | 1 | import express, { RequestHandler } from 'express' |
41fb13c3 | 2 | import multer, { diskStorage } from 'multer' |
c729caf6 | 3 | import { getLowercaseExtension } from '@shared/core-utils' |
c0e8b12e | 4 | import { HttpStatusCode } from '../../shared/models/http/http-error-codes' |
c158a5fa | 5 | import { CONFIG } from '../initializers/config' |
74dc3bca | 6 | import { REMOTE_SCHEME } from '../initializers/constants' |
c158a5fa | 7 | import { isArray } from './custom-validators/misc' |
0626e7af | 8 | import { logger } from './logger' |
f6d6e7f8 | 9 | import { deleteFileAndCatch, generateRandomString } from './utils' |
30bc55c8 | 10 | import { getExtFromMimetype } from './video' |
0626e7af | 11 | |
2feebf3e | 12 | function buildNSFWFilter (res?: express.Response, paramNSFW?: string) { |
0b18f4aa C |
13 | if (paramNSFW === 'true') return true |
14 | if (paramNSFW === 'false') return false | |
15 | if (paramNSFW === 'both') return undefined | |
d525fc39 | 16 | |
a1587156 | 17 | if (res?.locals.oauth) { |
dae86118 | 18 | const user = res.locals.oauth.token.User |
eb87f9a4 | 19 | |
d525fc39 | 20 | // User does not want NSFW videos |
eb87f9a4 C |
21 | if (user.nsfwPolicy === 'do_not_list') return false |
22 | ||
23 | // Both | |
24 | return undefined | |
0626e7af C |
25 | } |
26 | ||
d525fc39 C |
27 | if (CONFIG.INSTANCE.DEFAULT_NSFW_POLICY === 'do_not_list') return false |
28 | ||
29 | // Display all | |
30 | return null | |
0626e7af C |
31 | } |
32 | ||
8cc61201 | 33 | function cleanUpReqFiles (req: express.Request) { |
c158a5fa C |
34 | const filesObject = req.files |
35 | if (!filesObject) return | |
06215f15 | 36 | |
c158a5fa C |
37 | if (isArray(filesObject)) { |
38 | filesObject.forEach(f => deleteFileAndCatch(f.path)) | |
06215f15 C |
39 | return |
40 | } | |
41 | ||
c158a5fa C |
42 | for (const key of Object.keys(filesObject)) { |
43 | const files = filesObject[key] | |
06215f15 | 44 | |
c158a5fa | 45 | files.forEach(f => deleteFileAndCatch(f.path)) |
06215f15 C |
46 | } |
47 | } | |
48 | ||
0626e7af C |
49 | function getHostWithPort (host: string) { |
50 | const splitted = host.split(':') | |
51 | ||
52 | // The port was not specified | |
53 | if (splitted.length === 1) { | |
54 | if (REMOTE_SCHEME.HTTP === 'https') return host + ':443' | |
55 | ||
56 | return host + ':80' | |
57 | } | |
58 | ||
59 | return host | |
60 | } | |
61 | ||
400043b1 | 62 | function badRequest (_req: express.Request, res: express.Response) { |
2d53be02 RK |
63 | return res.type('json') |
64 | .status(HttpStatusCode.BAD_REQUEST_400) | |
65 | .end() | |
0626e7af C |
66 | } |
67 | ||
68 | function createReqFiles ( | |
69 | fieldNames: string[], | |
30bc55c8 | 70 | mimeTypes: { [id: string]: string | string[] }, |
d3d3deaa | 71 | destination = CONFIG.STORAGE.TMP_DIR |
06aad801 | 72 | ): RequestHandler { |
41fb13c3 | 73 | const storage = diskStorage({ |
0626e7af | 74 | destination: (req, file, cb) => { |
d3d3deaa | 75 | cb(null, destination) |
0626e7af C |
76 | }, |
77 | ||
c729caf6 C |
78 | filename: (req, file, cb) => { |
79 | return generateReqFilename(file, mimeTypes, cb) | |
0626e7af C |
80 | } |
81 | }) | |
82 | ||
a1587156 | 83 | const fields: { name: string, maxCount: number }[] = [] |
0626e7af C |
84 | for (const fieldName of fieldNames) { |
85 | fields.push({ | |
86 | name: fieldName, | |
87 | maxCount: 1 | |
88 | }) | |
89 | } | |
90 | ||
91 | return multer({ storage }).fields(fields) | |
92 | } | |
93 | ||
c729caf6 C |
94 | function createAnyReqFiles ( |
95 | mimeTypes: { [id: string]: string | string[] }, | |
c729caf6 C |
96 | fileFilter: (req: express.Request, file: Express.Multer.File, cb: (err: Error, result: boolean) => void) => void |
97 | ): RequestHandler { | |
98 | const storage = diskStorage({ | |
99 | destination: (req, file, cb) => { | |
d3d3deaa | 100 | cb(null, CONFIG.STORAGE.TMP_DIR) |
c729caf6 C |
101 | }, |
102 | ||
103 | filename: (req, file, cb) => { | |
104 | return generateReqFilename(file, mimeTypes, cb) | |
105 | } | |
106 | }) | |
107 | ||
108 | return multer({ storage, fileFilter }).any() | |
109 | } | |
110 | ||
687d638c | 111 | function isUserAbleToSearchRemoteURI (res: express.Response) { |
dae86118 | 112 | const user = res.locals.oauth ? res.locals.oauth.token.User : undefined |
687d638c C |
113 | |
114 | return CONFIG.SEARCH.REMOTE_URI.ANONYMOUS === true || | |
115 | (CONFIG.SEARCH.REMOTE_URI.USERS === true && user !== undefined) | |
116 | } | |
117 | ||
fe987656 C |
118 | function getCountVideos (req: express.Request) { |
119 | return req.query.skipCount !== true | |
120 | } | |
121 | ||
0626e7af C |
122 | // --------------------------------------------------------------------------- |
123 | ||
124 | export { | |
d525fc39 | 125 | buildNSFWFilter, |
0626e7af | 126 | getHostWithPort, |
c729caf6 | 127 | createAnyReqFiles, |
687d638c | 128 | isUserAbleToSearchRemoteURI, |
0626e7af | 129 | badRequest, |
06215f15 | 130 | createReqFiles, |
fe987656 | 131 | cleanUpReqFiles, |
e030bfb5 | 132 | getCountVideos |
0626e7af | 133 | } |
c729caf6 C |
134 | |
135 | // --------------------------------------------------------------------------- | |
136 | ||
137 | async function generateReqFilename ( | |
138 | file: Express.Multer.File, | |
139 | mimeTypes: { [id: string]: string | string[] }, | |
140 | cb: (err: Error, name: string) => void | |
141 | ) { | |
142 | let extension: string | |
143 | const fileExtension = getLowercaseExtension(file.originalname) | |
144 | const extensionFromMimetype = getExtFromMimetype(mimeTypes, file.mimetype) | |
145 | ||
146 | // Take the file extension if we don't understand the mime type | |
147 | if (!extensionFromMimetype) { | |
148 | extension = fileExtension | |
149 | } else { | |
150 | // Take the first available extension for this mimetype | |
151 | extension = extensionFromMimetype | |
152 | } | |
153 | ||
154 | let randomString = '' | |
155 | ||
156 | try { | |
157 | randomString = await generateRandomString(16) | |
158 | } catch (err) { | |
159 | logger.error('Cannot generate random string for file name.', { err }) | |
160 | randomString = 'fake-random-string' | |
161 | } | |
162 | ||
163 | cb(null, randomString + extension) | |
164 | } |