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