]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/helpers/express-utils.ts
Fix run committed transation
[github/Chocobozzz/PeerTube.git] / server / helpers / express-utils.ts
CommitLineData
0626e7af
C
1import * as express from 'express'
2import * as multer from 'multer'
c158a5fa
C
3import { HttpStatusCode } from '../../shared/core-utils/miscs/http-error-codes'
4import { CONFIG } from '../initializers/config'
74dc3bca 5import { REMOTE_SCHEME } from '../initializers/constants'
ea54cd04 6import { getLowercaseExtension } from './core-utils'
c158a5fa 7import { isArray } from './custom-validators/misc'
0626e7af 8import { logger } from './logger'
f6d6e7f8 9import { deleteFileAndCatch, generateRandomString } from './utils'
30bc55c8 10import { getExtFromMimetype } from './video'
0626e7af 11
2feebf3e 12function 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
c158a5fa
C
33function cleanUpReqFiles (
34 req: { files: { [fieldname: string]: Express.Multer.File[] } | Express.Multer.File[] }
35) {
36 const filesObject = req.files
37 if (!filesObject) return
06215f15 38
c158a5fa
C
39 if (isArray(filesObject)) {
40 filesObject.forEach(f => deleteFileAndCatch(f.path))
06215f15
C
41 return
42 }
43
c158a5fa
C
44 for (const key of Object.keys(filesObject)) {
45 const files = filesObject[key]
06215f15 46
c158a5fa 47 files.forEach(f => deleteFileAndCatch(f.path))
06215f15
C
48 }
49}
50
0626e7af
C
51function getHostWithPort (host: string) {
52 const splitted = host.split(':')
53
54 // The port was not specified
55 if (splitted.length === 1) {
56 if (REMOTE_SCHEME.HTTP === 'https') return host + ':443'
57
58 return host + ':80'
59 }
60
61 return host
62}
63
6dd9de95 64function badRequest (req: express.Request, res: express.Response) {
2d53be02
RK
65 return res.type('json')
66 .status(HttpStatusCode.BAD_REQUEST_400)
67 .end()
0626e7af
C
68}
69
70function createReqFiles (
71 fieldNames: string[],
30bc55c8 72 mimeTypes: { [id: string]: string | string[] },
a1587156 73 destinations: { [fieldName: string]: string }
0626e7af
C
74) {
75 const storage = multer.diskStorage({
76 destination: (req, file, cb) => {
a1587156 77 cb(null, destinations[file.fieldname])
0626e7af
C
78 },
79
80 filename: async (req, file, cb) => {
820d79c8 81 let extension: string
ea54cd04 82 const fileExtension = getLowercaseExtension(file.originalname)
30bc55c8 83 const extensionFromMimetype = getExtFromMimetype(mimeTypes, file.mimetype)
820d79c8
C
84
85 // Take the file extension if we don't understand the mime type
30bc55c8 86 if (!extensionFromMimetype) {
820d79c8
C
87 extension = fileExtension
88 } else {
30bc55c8 89 // Take the first available extension for this mimetype
820d79c8
C
90 extension = extensionFromMimetype
91 }
92
0626e7af
C
93 let randomString = ''
94
95 try {
96 randomString = await generateRandomString(16)
97 } catch (err) {
98 logger.error('Cannot generate random string for file name.', { err })
99 randomString = 'fake-random-string'
100 }
101
102 cb(null, randomString + extension)
103 }
104 })
105
a1587156 106 const fields: { name: string, maxCount: number }[] = []
0626e7af
C
107 for (const fieldName of fieldNames) {
108 fields.push({
109 name: fieldName,
110 maxCount: 1
111 })
112 }
113
114 return multer({ storage }).fields(fields)
115}
116
687d638c 117function isUserAbleToSearchRemoteURI (res: express.Response) {
dae86118 118 const user = res.locals.oauth ? res.locals.oauth.token.User : undefined
687d638c
C
119
120 return CONFIG.SEARCH.REMOTE_URI.ANONYMOUS === true ||
121 (CONFIG.SEARCH.REMOTE_URI.USERS === true && user !== undefined)
122}
123
fe987656
C
124function getCountVideos (req: express.Request) {
125 return req.query.skipCount !== true
126}
127
0626e7af
C
128// ---------------------------------------------------------------------------
129
130export {
d525fc39 131 buildNSFWFilter,
0626e7af 132 getHostWithPort,
687d638c 133 isUserAbleToSearchRemoteURI,
0626e7af 134 badRequest,
06215f15 135 createReqFiles,
fe987656 136 cleanUpReqFiles,
e030bfb5 137 getCountVideos
0626e7af 138}