]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/helpers/express-utils.ts
Check the comment is defined when validating body
[github/Chocobozzz/PeerTube.git] / server / helpers / express-utils.ts
CommitLineData
0626e7af
C
1import * as express from 'express'
2import * as multer from 'multer'
3import { CONFIG, REMOTE_SCHEME } from '../initializers'
4import { logger } from './logger'
5import { User } from '../../shared/models/users'
06215f15 6import { deleteFileAsync, generateRandomString } from './utils'
2769e297 7import { extname } from 'path'
06215f15 8import { isArray } from './custom-validators/misc'
d1a63fc7 9import { UserModel } from '../models/account/user'
0626e7af 10
0b18f4aa
C
11function buildNSFWFilter (res: express.Response, paramNSFW?: string) {
12 if (paramNSFW === 'true') return true
13 if (paramNSFW === 'false') return false
14 if (paramNSFW === 'both') return undefined
d525fc39 15
0626e7af 16 if (res.locals.oauth) {
d1a63fc7 17 const user: UserModel = 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
06215f15
C
32function cleanUpReqFiles (req: { files: { [ fieldname: string ]: Express.Multer.File[] } | Express.Multer.File[] }) {
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)) {
43 const file = files[ key ]
44
45 if (isArray(file)) file.forEach(f => deleteFileAsync(f.path))
46 else deleteFileAsync(file.path)
47 }
48}
49
0626e7af
C
50function 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
63function badRequest (req: express.Request, res: express.Response, next: express.NextFunction) {
64 return res.type('json').status(400).end()
65}
66
67function createReqFiles (
68 fieldNames: string[],
69 mimeTypes: { [ id: string ]: string },
70 destinations: { [ fieldName: string ]: string }
71) {
72 const storage = multer.diskStorage({
73 destination: (req, file, cb) => {
74 cb(null, destinations[ file.fieldname ])
75 },
76
77 filename: async (req, file, cb) => {
2769e297 78 const extension = mimeTypes[ file.mimetype ] || extname(file.originalname)
0626e7af
C
79 let randomString = ''
80
81 try {
82 randomString = await generateRandomString(16)
83 } catch (err) {
84 logger.error('Cannot generate random string for file name.', { err })
85 randomString = 'fake-random-string'
86 }
87
88 cb(null, randomString + extension)
89 }
90 })
91
c1e791ba 92 let fields: { name: string, maxCount: number }[] = []
0626e7af
C
93 for (const fieldName of fieldNames) {
94 fields.push({
95 name: fieldName,
96 maxCount: 1
97 })
98 }
99
100 return multer({ storage }).fields(fields)
101}
102
687d638c
C
103function isUserAbleToSearchRemoteURI (res: express.Response) {
104 const user: User = res.locals.oauth ? res.locals.oauth.token.User : undefined
105
106 return CONFIG.SEARCH.REMOTE_URI.ANONYMOUS === true ||
107 (CONFIG.SEARCH.REMOTE_URI.USERS === true && user !== undefined)
108}
109
0626e7af
C
110// ---------------------------------------------------------------------------
111
112export {
d525fc39 113 buildNSFWFilter,
0626e7af 114 getHostWithPort,
687d638c 115 isUserAbleToSearchRemoteURI,
0626e7af 116 badRequest,
06215f15
C
117 createReqFiles,
118 cleanUpReqFiles
0626e7af 119}