From 30bc55c88b3b7416c2224925e88639694fd32746 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Mon, 10 Aug 2020 14:25:29 +0200 Subject: Refactor video extensions logic in server --- server/helpers/custom-validators/videos.ts | 6 +----- server/helpers/express-utils.ts | 9 +++++---- server/helpers/video.ts | 19 ++++++++++++++----- 3 files changed, 20 insertions(+), 14 deletions(-) (limited to 'server/helpers') diff --git a/server/helpers/custom-validators/videos.ts b/server/helpers/custom-validators/videos.ts index 60e8075f6..40fecc09b 100644 --- a/server/helpers/custom-validators/videos.ts +++ b/server/helpers/custom-validators/videos.ts @@ -81,11 +81,7 @@ function isVideoFileExtnameValid (value: string) { } function isVideoFile (files: { [ fieldname: string ]: Express.Multer.File[] } | Express.Multer.File[]) { - const videoFileTypesRegex = Object.keys(MIMETYPES.VIDEO.MIMETYPE_EXT) - .map(m => `(${m})`) - .join('|') - - return isFileValid(files, videoFileTypesRegex, 'videofile', null) + return isFileValid(files, MIMETYPES.VIDEO.MIMETYPES_REGEX, 'videofile', null) } const videoImageTypes = CONSTRAINTS_FIELDS.VIDEOS.IMAGE.EXTNAME diff --git a/server/helpers/express-utils.ts b/server/helpers/express-utils.ts index f46812977..ba23557ba 100644 --- a/server/helpers/express-utils.ts +++ b/server/helpers/express-utils.ts @@ -6,6 +6,7 @@ import { deleteFileAsync, generateRandomString } from './utils' import { extname } from 'path' import { isArray } from './custom-validators/misc' import { CONFIG } from '../initializers/config' +import { getExtFromMimetype } from './video' function buildNSFWFilter (res?: express.Response, paramNSFW?: string) { if (paramNSFW === 'true') return true @@ -65,7 +66,7 @@ function badRequest (req: express.Request, res: express.Response) { function createReqFiles ( fieldNames: string[], - mimeTypes: { [id: string]: string }, + mimeTypes: { [id: string]: string | string[] }, destinations: { [fieldName: string]: string } ) { const storage = multer.diskStorage({ @@ -76,13 +77,13 @@ function createReqFiles ( filename: async (req, file, cb) => { let extension: string const fileExtension = extname(file.originalname) - const extensionFromMimetype = mimeTypes[file.mimetype] + const extensionFromMimetype = getExtFromMimetype(mimeTypes, file.mimetype) // Take the file extension if we don't understand the mime type - // We have the OGG/OGV exception too because firefox sends a bad mime type when sending an OGG file - if (fileExtension === '.ogg' || fileExtension === '.ogv' || !extensionFromMimetype) { + if (!extensionFromMimetype) { extension = fileExtension } else { + // Take the first available extension for this mimetype extension = extensionFromMimetype } diff --git a/server/helpers/video.ts b/server/helpers/video.ts index 89c85accb..488b4da17 100644 --- a/server/helpers/video.ts +++ b/server/helpers/video.ts @@ -1,5 +1,8 @@ -import { VideoModel } from '../models/video/video' import * as Bluebird from 'bluebird' +import { Response } from 'express' +import { CONFIG } from '@server/initializers/config' +import { DEFAULT_AUDIO_RESOLUTION } from '@server/initializers/constants' +import { JobQueue } from '@server/lib/job-queue' import { isStreamingPlaylist, MStreamingPlaylistVideo, @@ -12,11 +15,8 @@ import { MVideoThumbnail, MVideoWithRights } from '@server/types/models' -import { Response } from 'express' -import { DEFAULT_AUDIO_RESOLUTION } from '@server/initializers/constants' -import { JobQueue } from '@server/lib/job-queue' import { VideoPrivacy, VideoTranscodingPayload } from '@shared/models' -import { CONFIG } from "@server/initializers/config" +import { VideoModel } from '../models/video/video' type VideoFetchType = 'all' | 'only-video' | 'only-video-with-rights' | 'id' | 'none' | 'only-immutable-attributes' @@ -110,6 +110,14 @@ function getPrivaciesForFederation () { : [ { privacy: VideoPrivacy.PUBLIC } ] } +function getExtFromMimetype (mimeTypes: { [id: string]: string | string[] }, mimeType: string) { + const value = mimeTypes[mimeType] + + if (Array.isArray(value)) return value[0] + + return value +} + export { VideoFetchType, VideoFetchByUrlType, @@ -118,6 +126,7 @@ export { fetchVideoByUrl, addOptimizeOrMergeAudioJob, extractVideo, + getExtFromMimetype, isPrivacyForFederation, getPrivaciesForFederation } -- cgit v1.2.3