]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/helpers/express-utils.ts
Resumable video uploads (#3933)
[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 { deleteFileAndCatch, 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 import { HttpStatusCode } from '../../shared/core-utils/miscs/http-error-codes'
11
12 function 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
33 function cleanUpReqFiles (req: { files: { [fieldname: string]: Express.Multer.File[] } | Express.Multer.File[] }) {
34 const files = req.files
35
36 if (!files) return
37
38 if (isArray(files)) {
39 (files as Express.Multer.File[]).forEach(f => deleteFileAndCatch(f.path))
40 return
41 }
42
43 for (const key of Object.keys(files)) {
44 const file = files[key]
45
46 if (isArray(file)) file.forEach(f => deleteFileAndCatch(f.path))
47 else deleteFileAndCatch(file.path)
48 }
49 }
50
51 function 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
64 function badRequest (req: express.Request, res: express.Response) {
65 return res.type('json')
66 .status(HttpStatusCode.BAD_REQUEST_400)
67 .end()
68 }
69
70 function createReqFiles (
71 fieldNames: string[],
72 mimeTypes: { [id: string]: string | string[] },
73 destinations: { [fieldName: string]: string }
74 ) {
75 const storage = multer.diskStorage({
76 destination: (req, file, cb) => {
77 cb(null, destinations[file.fieldname])
78 },
79
80 filename: async (req, file, cb) => {
81 let extension: string
82 const fileExtension = extname(file.originalname)
83 const extensionFromMimetype = getExtFromMimetype(mimeTypes, file.mimetype)
84
85 // Take the file extension if we don't understand the mime type
86 if (!extensionFromMimetype) {
87 extension = fileExtension
88 } else {
89 // Take the first available extension for this mimetype
90 extension = extensionFromMimetype
91 }
92
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
106 const fields: { name: string, maxCount: number }[] = []
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
117 function isUserAbleToSearchRemoteURI (res: express.Response) {
118 const user = res.locals.oauth ? res.locals.oauth.token.User : undefined
119
120 return CONFIG.SEARCH.REMOTE_URI.ANONYMOUS === true ||
121 (CONFIG.SEARCH.REMOTE_URI.USERS === true && user !== undefined)
122 }
123
124 function getCountVideos (req: express.Request) {
125 return req.query.skipCount !== true
126 }
127
128 // ---------------------------------------------------------------------------
129
130 export {
131 buildNSFWFilter,
132 getHostWithPort,
133 isUserAbleToSearchRemoteURI,
134 badRequest,
135 createReqFiles,
136 cleanUpReqFiles,
137 getCountVideos
138 }