X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fhelpers%2Fexpress-utils.ts;h=82dd4c17807006dce47cdfa884974d62082a58f5;hb=cffef25313bdf7a6c435f56ac6715fdd91acf7b3;hp=780fd6345e850df4735f70d066c3f65616e8d926;hpb=c3edc5b074aa4bb1861ed0a94d3713808e87170f;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/helpers/express-utils.ts b/server/helpers/express-utils.ts index 780fd6345..82dd4c178 100644 --- a/server/helpers/express-utils.ts +++ b/server/helpers/express-utils.ts @@ -1,9 +1,9 @@ import express, { RequestHandler } from 'express' import multer, { diskStorage } from 'multer' +import { getLowercaseExtension } from '@shared/core-utils' import { HttpStatusCode } from '../../shared/models/http/http-error-codes' import { CONFIG } from '../initializers/config' import { REMOTE_SCHEME } from '../initializers/constants' -import { getLowercaseExtension } from '@shared/core-utils' import { isArray } from './custom-validators/misc' import { logger } from './logger' import { deleteFileAndCatch, generateRandomString } from './utils' @@ -68,36 +68,15 @@ function badRequest (_req: express.Request, res: express.Response) { function createReqFiles ( fieldNames: string[], mimeTypes: { [id: string]: string | string[] }, - destinations: { [fieldName: string]: string } + destination = CONFIG.STORAGE.TMP_DIR ): RequestHandler { const storage = diskStorage({ destination: (req, file, cb) => { - cb(null, destinations[file.fieldname]) + cb(null, destination) }, - filename: async (req, file, cb) => { - let extension: string - const fileExtension = getLowercaseExtension(file.originalname) - const extensionFromMimetype = getExtFromMimetype(mimeTypes, file.mimetype) - - // Take the file extension if we don't understand the mime type - if (!extensionFromMimetype) { - extension = fileExtension - } else { - // Take the first available extension for this mimetype - extension = extensionFromMimetype - } - - let randomString = '' - - try { - randomString = await generateRandomString(16) - } catch (err) { - logger.error('Cannot generate random string for file name.', { err }) - randomString = 'fake-random-string' - } - - cb(null, randomString + extension) + filename: (req, file, cb) => { + return generateReqFilename(file, mimeTypes, cb) } }) @@ -112,6 +91,23 @@ function createReqFiles ( return multer({ storage }).fields(fields) } +function createAnyReqFiles ( + mimeTypes: { [id: string]: string | string[] }, + fileFilter: (req: express.Request, file: Express.Multer.File, cb: (err: Error, result: boolean) => void) => void +): RequestHandler { + const storage = diskStorage({ + destination: (req, file, cb) => { + cb(null, CONFIG.STORAGE.TMP_DIR) + }, + + filename: (req, file, cb) => { + return generateReqFilename(file, mimeTypes, cb) + } + }) + + return multer({ storage, fileFilter }).any() +} + function isUserAbleToSearchRemoteURI (res: express.Response) { const user = res.locals.oauth ? res.locals.oauth.token.User : undefined @@ -128,9 +124,41 @@ function getCountVideos (req: express.Request) { export { buildNSFWFilter, getHostWithPort, + createAnyReqFiles, isUserAbleToSearchRemoteURI, badRequest, createReqFiles, cleanUpReqFiles, getCountVideos } + +// --------------------------------------------------------------------------- + +async function generateReqFilename ( + file: Express.Multer.File, + mimeTypes: { [id: string]: string | string[] }, + cb: (err: Error, name: string) => void +) { + let extension: string + const fileExtension = getLowercaseExtension(file.originalname) + const extensionFromMimetype = getExtFromMimetype(mimeTypes, file.mimetype) + + // Take the file extension if we don't understand the mime type + if (!extensionFromMimetype) { + extension = fileExtension + } else { + // Take the first available extension for this mimetype + extension = extensionFromMimetype + } + + let randomString = '' + + try { + randomString = await generateRandomString(16) + } catch (err) { + logger.error('Cannot generate random string for file name.', { err }) + randomString = 'fake-random-string' + } + + cb(null, randomString + extension) +}