diff options
author | Chocobozzz <me@florianbigard.com> | 2022-02-11 10:51:33 +0100 |
---|---|---|
committer | Chocobozzz <chocobozzz@cpy.re> | 2022-02-28 10:42:19 +0100 |
commit | c729caf6cc34630877a0e5a1bda1719384cd0c8a (patch) | |
tree | 1d2e13722e518c73d2c9e6f0969615e29d51cf8c /server/helpers/express-utils.ts | |
parent | a24bf4dc659cebb65d887862bf21d7a35e9ec791 (diff) | |
download | PeerTube-c729caf6cc34630877a0e5a1bda1719384cd0c8a.tar.gz PeerTube-c729caf6cc34630877a0e5a1bda1719384cd0c8a.tar.zst PeerTube-c729caf6cc34630877a0e5a1bda1719384cd0c8a.zip |
Add basic video editor support
Diffstat (limited to 'server/helpers/express-utils.ts')
-rw-r--r-- | server/helpers/express-utils.ts | 77 |
1 files changed, 53 insertions, 24 deletions
diff --git a/server/helpers/express-utils.ts b/server/helpers/express-utils.ts index 780fd6345..08f77966f 100644 --- a/server/helpers/express-utils.ts +++ b/server/helpers/express-utils.ts | |||
@@ -1,9 +1,9 @@ | |||
1 | import express, { RequestHandler } from 'express' | 1 | import express, { RequestHandler } from 'express' |
2 | import multer, { diskStorage } from 'multer' | 2 | import multer, { diskStorage } from 'multer' |
3 | import { getLowercaseExtension } from '@shared/core-utils' | ||
3 | import { HttpStatusCode } from '../../shared/models/http/http-error-codes' | 4 | import { HttpStatusCode } from '../../shared/models/http/http-error-codes' |
4 | import { CONFIG } from '../initializers/config' | 5 | import { CONFIG } from '../initializers/config' |
5 | import { REMOTE_SCHEME } from '../initializers/constants' | 6 | import { REMOTE_SCHEME } from '../initializers/constants' |
6 | import { getLowercaseExtension } from '@shared/core-utils' | ||
7 | import { isArray } from './custom-validators/misc' | 7 | import { isArray } from './custom-validators/misc' |
8 | import { logger } from './logger' | 8 | import { logger } from './logger' |
9 | import { deleteFileAndCatch, generateRandomString } from './utils' | 9 | import { deleteFileAndCatch, generateRandomString } from './utils' |
@@ -75,29 +75,8 @@ function createReqFiles ( | |||
75 | cb(null, destinations[file.fieldname]) | 75 | cb(null, destinations[file.fieldname]) |
76 | }, | 76 | }, |
77 | 77 | ||
78 | filename: async (req, file, cb) => { | 78 | filename: (req, file, cb) => { |
79 | let extension: string | 79 | return generateReqFilename(file, mimeTypes, cb) |
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 | } | 80 | } |
102 | }) | 81 | }) |
103 | 82 | ||
@@ -112,6 +91,24 @@ function createReqFiles ( | |||
112 | return multer({ storage }).fields(fields) | 91 | return multer({ storage }).fields(fields) |
113 | } | 92 | } |
114 | 93 | ||
94 | function createAnyReqFiles ( | ||
95 | mimeTypes: { [id: string]: string | string[] }, | ||
96 | destinationDirectory: string, | ||
97 | fileFilter: (req: express.Request, file: Express.Multer.File, cb: (err: Error, result: boolean) => void) => void | ||
98 | ): RequestHandler { | ||
99 | const storage = diskStorage({ | ||
100 | destination: (req, file, cb) => { | ||
101 | cb(null, destinationDirectory) | ||
102 | }, | ||
103 | |||
104 | filename: (req, file, cb) => { | ||
105 | return generateReqFilename(file, mimeTypes, cb) | ||
106 | } | ||
107 | }) | ||
108 | |||
109 | return multer({ storage, fileFilter }).any() | ||
110 | } | ||
111 | |||
115 | function isUserAbleToSearchRemoteURI (res: express.Response) { | 112 | function isUserAbleToSearchRemoteURI (res: express.Response) { |
116 | const user = res.locals.oauth ? res.locals.oauth.token.User : undefined | 113 | const user = res.locals.oauth ? res.locals.oauth.token.User : undefined |
117 | 114 | ||
@@ -128,9 +125,41 @@ function getCountVideos (req: express.Request) { | |||
128 | export { | 125 | export { |
129 | buildNSFWFilter, | 126 | buildNSFWFilter, |
130 | getHostWithPort, | 127 | getHostWithPort, |
128 | createAnyReqFiles, | ||
131 | isUserAbleToSearchRemoteURI, | 129 | isUserAbleToSearchRemoteURI, |
132 | badRequest, | 130 | badRequest, |
133 | createReqFiles, | 131 | createReqFiles, |
134 | cleanUpReqFiles, | 132 | cleanUpReqFiles, |
135 | getCountVideos | 133 | getCountVideos |
136 | } | 134 | } |
135 | |||
136 | // --------------------------------------------------------------------------- | ||
137 | |||
138 | async function generateReqFilename ( | ||
139 | file: Express.Multer.File, | ||
140 | mimeTypes: { [id: string]: string | string[] }, | ||
141 | cb: (err: Error, name: string) => void | ||
142 | ) { | ||
143 | let extension: string | ||
144 | const fileExtension = getLowercaseExtension(file.originalname) | ||
145 | const extensionFromMimetype = getExtFromMimetype(mimeTypes, file.mimetype) | ||
146 | |||
147 | // Take the file extension if we don't understand the mime type | ||
148 | if (!extensionFromMimetype) { | ||
149 | extension = fileExtension | ||
150 | } else { | ||
151 | // Take the first available extension for this mimetype | ||
152 | extension = extensionFromMimetype | ||
153 | } | ||
154 | |||
155 | let randomString = '' | ||
156 | |||
157 | try { | ||
158 | randomString = await generateRandomString(16) | ||
159 | } catch (err) { | ||
160 | logger.error('Cannot generate random string for file name.', { err }) | ||
161 | randomString = 'fake-random-string' | ||
162 | } | ||
163 | |||
164 | cb(null, randomString + extension) | ||
165 | } | ||