]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame_incremental - server/helpers/express-utils.ts
Translated using Weblate (Chinese (Simplified))
[github/Chocobozzz/PeerTube.git] / server / helpers / express-utils.ts
... / ...
CommitLineData
1import express, { RequestHandler } from 'express'
2import multer, { diskStorage } from 'multer'
3import { HttpStatusCode } from '../../shared/models/http/http-error-codes'
4import { CONFIG } from '../initializers/config'
5import { REMOTE_SCHEME } from '../initializers/constants'
6import { getLowercaseExtension } from '@shared/core-utils'
7import { isArray } from './custom-validators/misc'
8import { logger } from './logger'
9import { deleteFileAndCatch, generateRandomString } from './utils'
10import { getExtFromMimetype } from './video'
11
12function buildNSFWFilter (res?: express.Response, paramNSFW?: string) {
13 if (paramNSFW === 'true') return true
14 if (paramNSFW === 'false') return false
15 if (paramNSFW === 'both') return undefined
16
17 if (res?.locals.oauth) {
18 const user = res.locals.oauth.token.User
19
20 // User does not want NSFW videos
21 if (user.nsfwPolicy === 'do_not_list') return false
22
23 // Both
24 return undefined
25 }
26
27 if (CONFIG.INSTANCE.DEFAULT_NSFW_POLICY === 'do_not_list') return false
28
29 // Display all
30 return null
31}
32
33function cleanUpReqFiles (req: express.Request) {
34 const filesObject = req.files
35 if (!filesObject) return
36
37 if (isArray(filesObject)) {
38 filesObject.forEach(f => deleteFileAndCatch(f.path))
39 return
40 }
41
42 for (const key of Object.keys(filesObject)) {
43 const files = filesObject[key]
44
45 files.forEach(f => deleteFileAndCatch(f.path))
46 }
47}
48
49function 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
62function badRequest (_req: express.Request, res: express.Response) {
63 return res.type('json')
64 .status(HttpStatusCode.BAD_REQUEST_400)
65 .end()
66}
67
68function createReqFiles (
69 fieldNames: string[],
70 mimeTypes: { [id: string]: string | string[] },
71 destinations: { [fieldName: string]: string }
72): RequestHandler {
73 const storage = diskStorage({
74 destination: (req, file, cb) => {
75 cb(null, destinations[file.fieldname])
76 },
77
78 filename: async (req, file, cb) => {
79 let extension: string
80 const fileExtension = getLowercaseExtension(file.originalname)
81 const extensionFromMimetype = getExtFromMimetype(mimeTypes, file.mimetype)
82
83 // Take the file extension if we don't understand the mime type
84 if (!extensionFromMimetype) {
85 extension = fileExtension
86 } else {
87 // Take the first available extension for this mimetype
88 extension = extensionFromMimetype
89 }
90
91 let randomString = ''
92
93 try {
94 randomString = await generateRandomString(16)
95 } catch (err) {
96 logger.error('Cannot generate random string for file name.', { err })
97 randomString = 'fake-random-string'
98 }
99
100 cb(null, randomString + extension)
101 }
102 })
103
104 const fields: { name: string, maxCount: number }[] = []
105 for (const fieldName of fieldNames) {
106 fields.push({
107 name: fieldName,
108 maxCount: 1
109 })
110 }
111
112 return multer({ storage }).fields(fields)
113}
114
115function isUserAbleToSearchRemoteURI (res: express.Response) {
116 const user = res.locals.oauth ? res.locals.oauth.token.User : undefined
117
118 return CONFIG.SEARCH.REMOTE_URI.ANONYMOUS === true ||
119 (CONFIG.SEARCH.REMOTE_URI.USERS === true && user !== undefined)
120}
121
122function getCountVideos (req: express.Request) {
123 return req.query.skipCount !== true
124}
125
126// ---------------------------------------------------------------------------
127
128export {
129 buildNSFWFilter,
130 getHostWithPort,
131 isUserAbleToSearchRemoteURI,
132 badRequest,
133 createReqFiles,
134 cleanUpReqFiles,
135 getCountVideos
136}