Verified Commit ea54cd04 authored by Chocobozzz's avatar Chocobozzz
Browse files

Fix video upload with a capitalized ext

parent 29510651
import * as express from 'express' import * as express from 'express'
import { move } from 'fs-extra' import { move } from 'fs-extra'
import { extname } from 'path' import { getLowercaseExtension } from '@server/helpers/core-utils'
import { deleteResumableUploadMetaFile, getResumableUploadPath } from '@server/helpers/upload' import { deleteResumableUploadMetaFile, getResumableUploadPath } from '@server/helpers/upload'
import { createTorrentAndSetInfoHash } from '@server/helpers/webtorrent' import { createTorrentAndSetInfoHash } from '@server/helpers/webtorrent'
import { getLocalVideoActivityPubUrl } from '@server/lib/activitypub/url' import { getLocalVideoActivityPubUrl } from '@server/lib/activitypub/url'
...@@ -225,7 +225,7 @@ async function addVideo (options: { ...@@ -225,7 +225,7 @@ async function addVideo (options: {
async function buildNewFile (video: MVideo, videoPhysicalFile: express.VideoUploadFile) { async function buildNewFile (video: MVideo, videoPhysicalFile: express.VideoUploadFile) {
const videoFile = new VideoFileModel({ const videoFile = new VideoFileModel({
extname: extname(videoPhysicalFile.filename), extname: getLowercaseExtension(videoPhysicalFile.filename),
size: videoPhysicalFile.size, size: videoPhysicalFile.size,
videoStreamingPlaylistId: null, videoStreamingPlaylistId: null,
metadata: await getMetadataFromFile(videoPhysicalFile.path) metadata: await getMetadataFromFile(videoPhysicalFile.path)
......
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
import { exec, ExecOptions } from 'child_process' import { exec, ExecOptions } from 'child_process'
import { BinaryToTextEncoding, createHash, randomBytes } from 'crypto' import { BinaryToTextEncoding, createHash, randomBytes } from 'crypto'
import { truncate } from 'lodash' import { truncate } from 'lodash'
import { basename, isAbsolute, join, resolve } from 'path' import { basename, extname, isAbsolute, join, resolve } from 'path'
import * as pem from 'pem' import * as pem from 'pem'
import { pipeline } from 'stream' import { pipeline } from 'stream'
import { URL } from 'url' import { URL } from 'url'
...@@ -32,6 +32,18 @@ const objectConverter = (oldObject: any, keyConverter: (e: string) => string, va ...@@ -32,6 +32,18 @@ const objectConverter = (oldObject: any, keyConverter: (e: string) => string, va
return newObject return newObject
} }
function mapToJSON (map: Map<any, any>) {
const obj: any = {}
for (const [ k, v ] of map) {
obj[k] = v
}
return obj
}
// ---------------------------------------------------------------------------
const timeTable = { const timeTable = {
ms: 1, ms: 1,
second: 1000, second: 1000,
...@@ -110,6 +122,8 @@ export function parseBytes (value: string | number): number { ...@@ -110,6 +122,8 @@ export function parseBytes (value: string | number): number {
} }
} }
// ---------------------------------------------------------------------------
function sanitizeUrl (url: string) { function sanitizeUrl (url: string) {
const urlObject = new URL(url) const urlObject = new URL(url)
...@@ -129,6 +143,8 @@ function sanitizeHost (host: string, remoteScheme: string) { ...@@ -129,6 +143,8 @@ function sanitizeHost (host: string, remoteScheme: string) {
return host.replace(new RegExp(`:${toRemove}$`), '') return host.replace(new RegExp(`:${toRemove}$`), '')
} }
// ---------------------------------------------------------------------------
function isTestInstance () { function isTestInstance () {
return process.env.NODE_ENV === 'test' return process.env.NODE_ENV === 'test'
} }
...@@ -141,6 +157,8 @@ function getAppNumber () { ...@@ -141,6 +157,8 @@ function getAppNumber () {
return process.env.NODE_APP_INSTANCE return process.env.NODE_APP_INSTANCE
} }
// ---------------------------------------------------------------------------
let rootPath: string let rootPath: string
function root () { function root () {
...@@ -154,27 +172,19 @@ function root () { ...@@ -154,27 +172,19 @@ function root () {
return rootPath return rootPath
} }
function pageToStartAndCount (page: number, itemsPerPage: number) { function buildPath (path: string) {
const start = (page - 1) * itemsPerPage if (isAbsolute(path)) return path
return { start, count: itemsPerPage } return join(root(), path)
} }
function mapToJSON (map: Map<any, any>) { function getLowercaseExtension (filename: string) {
const obj: any = {} const ext = extname(filename) || ''
for (const [ k, v ] of map) {
obj[k] = v
}
return obj return ext.toLowerCase()
} }
function buildPath (path: string) { // ---------------------------------------------------------------------------
if (isAbsolute(path)) return path
return join(root(), path)
}
// Consistent with .length, lodash truncate function is not // Consistent with .length, lodash truncate function is not
function peertubeTruncate (str: string, options: { length: number, separator?: RegExp, omission?: string }) { function peertubeTruncate (str: string, options: { length: number, separator?: RegExp, omission?: string }) {
...@@ -189,6 +199,27 @@ function peertubeTruncate (str: string, options: { length: number, separator?: R ...@@ -189,6 +199,27 @@ function peertubeTruncate (str: string, options: { length: number, separator?: R
return truncate(str, options) return truncate(str, options)
} }
function pageToStartAndCount (page: number, itemsPerPage: number) {
const start = (page - 1) * itemsPerPage
return { start, count: itemsPerPage }
}
// ---------------------------------------------------------------------------
type SemVersion = { major: number, minor: number, patch: number }
function parseSemVersion (s: string) {
const parsed = s.match(/^v?(\d+)\.(\d+)\.(\d+)$/i)
return {
major: parseInt(parsed[1]),
minor: parseInt(parsed[2]),
patch: parseInt(parsed[3])
} as SemVersion
}
// ---------------------------------------------------------------------------
function sha256 (str: string | Buffer, encoding: BinaryToTextEncoding = 'hex') { function sha256 (str: string | Buffer, encoding: BinaryToTextEncoding = 'hex') {
return createHash('sha256').update(str).digest(encoding) return createHash('sha256').update(str).digest(encoding)
} }
...@@ -197,6 +228,8 @@ function sha1 (str: string | Buffer, encoding: BinaryToTextEncoding = 'hex') { ...@@ -197,6 +228,8 @@ function sha1 (str: string | Buffer, encoding: BinaryToTextEncoding = 'hex') {
return createHash('sha1').update(str).digest(encoding) return createHash('sha1').update(str).digest(encoding)
} }
// ---------------------------------------------------------------------------
function execShell (command: string, options?: ExecOptions) { function execShell (command: string, options?: ExecOptions) {
return new Promise<{ err?: Error, stdout: string, stderr: string }>((res, rej) => { return new Promise<{ err?: Error, stdout: string, stderr: string }>((res, rej) => {
exec(command, options, (err, stdout, stderr) => { exec(command, options, (err, stdout, stderr) => {
...@@ -208,6 +241,8 @@ function execShell (command: string, options?: ExecOptions) { ...@@ -208,6 +241,8 @@ function execShell (command: string, options?: ExecOptions) {
}) })
} }
// ---------------------------------------------------------------------------
function promisify0<A> (func: (cb: (err: any, result: A) => void) => void): () => Promise<A> { function promisify0<A> (func: (cb: (err: any, result: A) => void) => void): () => Promise<A> {
return function promisified (): Promise<A> { return function promisified (): Promise<A> {
return new Promise<A>((resolve: (arg: A) => void, reject: (err: any) => void) => { return new Promise<A>((resolve: (arg: A) => void, reject: (err: any) => void) => {
...@@ -233,17 +268,6 @@ function promisify2<T, U, A> (func: (arg1: T, arg2: U, cb: (err: any, result: A) ...@@ -233,17 +268,6 @@ function promisify2<T, U, A> (func: (arg1: T, arg2: U, cb: (err: any, result: A)
} }
} }
type SemVersion = { major: number, minor: number, patch: number }
function parseSemVersion (s: string) {
const parsed = s.match(/^v?(\d+)\.(\d+)\.(\d+)$/i)
return {
major: parseInt(parsed[1]),
minor: parseInt(parsed[2]),
patch: parseInt(parsed[3])
} as SemVersion
}
const randomBytesPromise = promisify1<number, Buffer>(randomBytes) const randomBytesPromise = promisify1<number, Buffer>(randomBytes)
const createPrivateKey = promisify1<number, { key: string }>(pem.createPrivateKey) const createPrivateKey = promisify1<number, { key: string }>(pem.createPrivateKey)
const getPublicKey = promisify1<string, { publicKey: string }>(pem.getPublicKey) const getPublicKey = promisify1<string, { publicKey: string }>(pem.getPublicKey)
...@@ -259,17 +283,21 @@ export { ...@@ -259,17 +283,21 @@ export {
getAppNumber, getAppNumber,
objectConverter, objectConverter,
mapToJSON,
root, root,
pageToStartAndCount, buildPath,
getLowercaseExtension,
sanitizeUrl, sanitizeUrl,
sanitizeHost, sanitizeHost,
buildPath,
execShell, execShell,
pageToStartAndCount,
peertubeTruncate, peertubeTruncate,
sha256, sha256,
sha1, sha1,
mapToJSON,
promisify0, promisify0,
promisify1, promisify1,
......
import * as express from 'express' import * as express from 'express'
import * as multer from 'multer' import * as multer from 'multer'
import { extname } from 'path'
import { HttpStatusCode } from '../../shared/core-utils/miscs/http-error-codes' import { HttpStatusCode } from '../../shared/core-utils/miscs/http-error-codes'
import { CONFIG } from '../initializers/config' import { CONFIG } from '../initializers/config'
import { REMOTE_SCHEME } from '../initializers/constants' import { REMOTE_SCHEME } from '../initializers/constants'
import { getLowercaseExtension } from './core-utils'
import { isArray } from './custom-validators/misc' import { isArray } from './custom-validators/misc'
import { logger } from './logger' import { logger } from './logger'
import { deleteFileAndCatch, generateRandomString } from './utils' import { deleteFileAndCatch, generateRandomString } from './utils'
...@@ -79,7 +79,7 @@ function createReqFiles ( ...@@ -79,7 +79,7 @@ function createReqFiles (
filename: async (req, file, cb) => { filename: async (req, file, cb) => {
let extension: string let extension: string
const fileExtension = extname(file.originalname) const fileExtension = getLowercaseExtension(file.originalname)
const extensionFromMimetype = getExtFromMimetype(mimeTypes, file.mimetype) const extensionFromMimetype = getExtFromMimetype(mimeTypes, file.mimetype)
// Take the file extension if we don't understand the mime type // Take the file extension if we don't understand the mime type
......
import { copy, readFile, remove, rename } from 'fs-extra' import { copy, readFile, remove, rename } from 'fs-extra'
import * as Jimp from 'jimp' import * as Jimp from 'jimp'
import { extname } from 'path'
import { v4 as uuidv4 } from 'uuid' import { v4 as uuidv4 } from 'uuid'
import { getLowercaseExtension } from './core-utils'
import { convertWebPToJPG, processGIF } from './ffmpeg-utils' import { convertWebPToJPG, processGIF } from './ffmpeg-utils'
import { logger } from './logger' import { logger } from './logger'
...@@ -15,7 +15,7 @@ async function processImage ( ...@@ -15,7 +15,7 @@ async function processImage (
newSize: { width: number, height: number }, newSize: { width: number, height: number },
keepOriginal = false keepOriginal = false
) { ) {
const extension = extname(path) const extension = getLowercaseExtension(path)
if (path === destination) { if (path === destination) {
throw new Error('Jimp/FFmpeg needs an input path different that the output path.') throw new Error('Jimp/FFmpeg needs an input path different that the output path.')
...@@ -61,7 +61,8 @@ async function jimpProcessor (path: string, destination: string, newSize: { widt ...@@ -61,7 +61,8 @@ async function jimpProcessor (path: string, destination: string, newSize: { widt
await remove(destination) await remove(destination)
// Optimization if the source file has the appropriate size // Optimization if the source file has the appropriate size
if (await skipProcessing({ jimpInstance, newSize, imageBytes: inputBuffer.byteLength, inputExt, outputExt: extname(destination) })) { const outputExt = getLowercaseExtension(destination)
if (skipProcessing({ jimpInstance, newSize, imageBytes: inputBuffer.byteLength, inputExt, outputExt })) {
return copy(path, destination) return copy(path, destination)
} }
......
import { extname } from 'path'
import { v4 as uuidv4 } from 'uuid' import { v4 as uuidv4 } from 'uuid'
import { getLowercaseExtension } from '@server/helpers/core-utils'
import { isActivityPubUrlValid } from '@server/helpers/custom-validators/activitypub/misc' import { isActivityPubUrlValid } from '@server/helpers/custom-validators/activitypub/misc'
import { MIMETYPES } from '@server/initializers/constants' import { MIMETYPES } from '@server/initializers/constants'
import { ActorModel } from '@server/models/actor/actor' import { ActorModel } from '@server/models/actor/actor'
...@@ -43,7 +43,7 @@ function getImageInfoFromObject (actorObject: ActivityPubActor, type: ActorImage ...@@ -43,7 +43,7 @@ function getImageInfoFromObject (actorObject: ActivityPubActor, type: ActorImage
if (icon.mediaType) { if (icon.mediaType) {
extension = mimetypes.MIMETYPE_EXT[icon.mediaType] extension = mimetypes.MIMETYPE_EXT[icon.mediaType]
} else { } else {
const tmp = extname(icon.url) const tmp = getLowercaseExtension(icon.url)
if (mimetypes.EXT_MIMETYPE[tmp] !== undefined) extension = tmp if (mimetypes.EXT_MIMETYPE[tmp] !== undefined) extension = tmp
} }
......
import * as Bull from 'bull' import * as Bull from 'bull'
import { copy, stat } from 'fs-extra' import { copy, stat } from 'fs-extra'
import { extname } from 'path' import { extname } from 'path'
import { getLowercaseExtension } from '@server/helpers/core-utils'
import { createTorrentAndSetInfoHash } from '@server/helpers/webtorrent' import { createTorrentAndSetInfoHash } from '@server/helpers/webtorrent'
import { generateVideoFilename, getVideoFilePath } from '@server/lib/video-paths' import { generateVideoFilename, getVideoFilePath } from '@server/lib/video-paths'
import { UserModel } from '@server/models/user/user' import { UserModel } from '@server/models/user/user'
...@@ -55,7 +56,7 @@ async function updateVideoFile (video: MVideoFullLight, inputFilePath: string) { ...@@ -55,7 +56,7 @@ async function updateVideoFile (video: MVideoFullLight, inputFilePath: string) {
const { size } = await stat(inputFilePath) const { size } = await stat(inputFilePath)
const fps = await getVideoFileFPS(inputFilePath) const fps = await getVideoFileFPS(inputFilePath)
const fileExt = extname(inputFilePath) const fileExt = getLowercaseExtension(inputFilePath)
const currentVideoFile = video.VideoFiles.find(videoFile => videoFile.resolution === videoFileResolution) const currentVideoFile = video.VideoFiles.find(videoFile => videoFile.resolution === videoFileResolution)
......
import * as Bull from 'bull' import * as Bull from 'bull'
import { move, remove, stat } from 'fs-extra' import { move, remove, stat } from 'fs-extra'
import { extname } from 'path' import { extname } from 'path'
import { getLowercaseExtension } from '@server/helpers/core-utils'
import { retryTransactionWrapper } from '@server/helpers/database-utils' import { retryTransactionWrapper } from '@server/helpers/database-utils'
import { YoutubeDL } from '@server/helpers/youtube-dl' import { YoutubeDL } from '@server/helpers/youtube-dl'
import { isPostImportVideoAccepted } from '@server/lib/moderation' import { isPostImportVideoAccepted } from '@server/lib/moderation'
...@@ -119,7 +120,7 @@ async function processFile (downloader: () => Promise<string>, videoImport: MVid ...@@ -119,7 +120,7 @@ async function processFile (downloader: () => Promise<string>, videoImport: MVid
const duration = await getDurationFromVideoFile(tempVideoPath) const duration = await getDurationFromVideoFile(tempVideoPath)
// Prepare video file object for creation in database // Prepare video file object for creation in database
const fileExt = extname(tempVideoPath) const fileExt = getLowercaseExtension(tempVideoPath)
const videoFileData = { const videoFileData = {
extname: fileExt, extname: fileExt,
resolution: videoFileResolution, resolution: videoFileResolution,
......
import 'multer' import 'multer'
import { queue } from 'async' import { queue } from 'async'
import * as LRUCache from 'lru-cache' import * as LRUCache from 'lru-cache'
import { extname, join } from 'path' import { join } from 'path'
import { v4 as uuidv4 } from 'uuid' import { v4 as uuidv4 } from 'uuid'
import { getLowercaseExtension } from '@server/helpers/core-utils'
import { ActorModel } from '@server/models/actor/actor' import { ActorModel } from '@server/models/actor/actor'
import { ActivityPubActorType, ActorImageType } from '@shared/models' import { ActivityPubActorType, ActorImageType } from '@shared/models'
import { retryTransactionWrapper } from '../helpers/database-utils' import { retryTransactionWrapper } from '../helpers/database-utils'
...@@ -41,7 +42,7 @@ async function updateLocalActorImageFile ( ...@@ -41,7 +42,7 @@ async function updateLocalActorImageFile (
? ACTOR_IMAGES_SIZE.AVATARS ? ACTOR_IMAGES_SIZE.AVATARS
: ACTOR_IMAGES_SIZE.BANNERS : ACTOR_IMAGES_SIZE.BANNERS
const extension = extname(imagePhysicalFile.filename) const extension = getLowercaseExtension(imagePhysicalFile.filename)
const imageName = uuidv4() + extension const imageName = uuidv4() + extension
const destination = join(CONFIG.STORAGE.ACTOR_IMAGES, imageName) const destination = join(CONFIG.STORAGE.ACTOR_IMAGES, imageName)
......
import { values } from 'lodash' import { values } from 'lodash'
import { extname } from 'path'
import { literal, Op, Transaction } from 'sequelize' import { literal, Op, Transaction } from 'sequelize'
import { import {
AllowNull, AllowNull,
...@@ -17,6 +16,7 @@ import { ...@@ -17,6 +16,7 @@ import {
Table, Table,
UpdatedAt UpdatedAt
} from 'sequelize-typescript' } from 'sequelize-typescript'
import { getLowercaseExtension } from '@server/helpers/core-utils'
import { ModelCache } from '@server/models/model-cache' import { ModelCache } from '@server/models/model-cache'
import { AttributesOnly } from '@shared/core-utils' import { AttributesOnly } from '@shared/core-utils'
import { ActivityIconObject, ActivityPubActorType } from '../../../shared/models/activitypub' import { ActivityIconObject, ActivityPubActorType } from '../../../shared/models/activitypub'
...@@ -567,7 +567,7 @@ export class ActorModel extends Model<Partial<AttributesOnly<ActorModel>>> { ...@@ -567,7 +567,7 @@ export class ActorModel extends Model<Partial<AttributesOnly<ActorModel>>> {
let image: ActivityIconObject let image: ActivityIconObject
if (this.avatarId) { if (this.avatarId) {
const extension = extname(this.Avatar.filename) const extension = getLowercaseExtension(this.Avatar.filename)
icon = { icon = {
type: 'Image', type: 'Image',
...@@ -580,7 +580,7 @@ export class ActorModel extends Model<Partial<AttributesOnly<ActorModel>>> { ...@@ -580,7 +580,7 @@ export class ActorModel extends Model<Partial<AttributesOnly<ActorModel>>> {
if (this.bannerId) { if (this.bannerId) {
const banner = (this as MActorAPChannel).Banner const banner = (this as MActorAPChannel).Banner
const extension = extname(banner.filename) const extension = getLowercaseExtension(banner.filename)
image = { image = {
type: 'Image', type: 'Image',
......
...@@ -4,10 +4,11 @@ import { expect } from 'chai' ...@@ -4,10 +4,11 @@ import { expect } from 'chai'
import { createReadStream, pathExists, readdir, readFile, stat } from 'fs-extra' import { createReadStream, pathExists, readdir, readFile, stat } from 'fs-extra'
import got, { Response as GotResponse } from 'got/dist/source' import got, { Response as GotResponse } from 'got/dist/source'
import * as parseTorrent from 'parse-torrent' import * as parseTorrent from 'parse-torrent'
import { extname, join } from 'path' import { join } from 'path'
import * as request from 'supertest' import * as request from 'supertest'
import { v4 as uuidv4 } from 'uuid' import { v4 as uuidv4 } from 'uuid'
import validator from 'validator' import validator from 'validator'
import { getLowercaseExtension } from '@server/helpers/core-utils'
import { HttpStatusCode } from '@shared/core-utils' import { HttpStatusCode } from '@shared/core-utils'
import { VideosCommonQuery } from '@shared/models' import { VideosCommonQuery } from '@shared/models'
import { loadLanguages, VIDEO_CATEGORIES, VIDEO_LANGUAGES, VIDEO_LICENCES, VIDEO_PRIVACIES } from '../../../server/initializers/constants' import { loadLanguages, VIDEO_CATEGORIES, VIDEO_LANGUAGES, VIDEO_LICENCES, VIDEO_PRIVACIES } from '../../../server/initializers/constants'
...@@ -738,7 +739,7 @@ async function completeVideoCheck ( ...@@ -738,7 +739,7 @@ async function completeVideoCheck (
const file = videoDetails.files.find(f => f.resolution.id === attributeFile.resolution) const file = videoDetails.files.find(f => f.resolution.id === attributeFile.resolution)
expect(file).not.to.be.undefined expect(file).not.to.be.undefined
let extension = extname(attributes.fixture) let extension = getLowercaseExtension(attributes.fixture)
// Transcoding enabled: extension will always be .mp4 // Transcoding enabled: extension will always be .mp4
if (attributes.files.length > 1) extension = '.mp4' if (attributes.files.length > 1) extension = '.mp4'
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment