]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/helpers/express-utils.ts
Translated using Weblate (Arabic)
[github/Chocobozzz/PeerTube.git] / server / helpers / express-utils.ts
1 import * as express from 'express'
2 import * as multer from 'multer'
3 import { REMOTE_SCHEME } from '../initializers/constants'
4 import { logger } from './logger'
5 import { deleteFileAsync, generateRandomString } from './utils'
6 import { extname } from 'path'
7 import { isArray } from './custom-validators/misc'
8 import { CONFIG } from '../initializers/config'
9 import { getExtFromMimetype } from './video'
10
11 function buildNSFWFilter (res?: express.Response, paramNSFW?: string) {
12 if (paramNSFW === 'true') return true
13 if (paramNSFW === 'false') return false
14 if (paramNSFW === 'both') return undefined
15
16 if (res?.locals.oauth) {
17 const user = res.locals.oauth.token.User
18
19 // User does not want NSFW videos
20 if (user.nsfwPolicy === 'do_not_list') return false
21
22 // Both
23 return undefined
24 }
25
26 if (CONFIG.INSTANCE.DEFAULT_NSFW_POLICY === 'do_not_list') return false
27
28 // Display all
29 return null
30 }
31
32 function 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
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
63 function badRequest (req: express.Request, res: express.Response) {
64 return res.type('json').status(400).end()
65 }
66
67 function createReqFiles (
68 fieldNames: string[],
69 mimeTypes: { [id: string]: 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) => {
78 let extension: string
79 const fileExtension = extname(file.originalname)
80 const extensionFromMimetype = getExtFromMimetype(mimeTypes, file.mimetype)
81
82 // Take the file extension if we don't understand the mime type
83 if (!extensionFromMimetype) {
84 extension = fileExtension
85 } else {
86 // Take the first available extension for this mimetype
87 extension = extensionFromMimetype
88 }
89
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
103 const fields: { name: string, maxCount: number }[] = []
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
114 function isUserAbleToSearchRemoteURI (res: express.Response) {
115 const user = res.locals.oauth ? res.locals.oauth.token.User : undefined
116
117 return CONFIG.SEARCH.REMOTE_URI.ANONYMOUS === true ||
118 (CONFIG.SEARCH.REMOTE_URI.USERS === true && user !== undefined)
119 }
120
121 function getCountVideos (req: express.Request) {
122 return req.query.skipCount !== true
123 }
124
125 // ---------------------------------------------------------------------------
126
127 export {
128 buildNSFWFilter,
129 getHostWithPort,
130 isUserAbleToSearchRemoteURI,
131 badRequest,
132 createReqFiles,
133 cleanUpReqFiles,
134 getCountVideos
135 }