1
0
Fork 0
mirror of https://github.com/Chocobozzz/PeerTube.git synced 2025-10-04 02:09:37 +02:00

Fix video upload with a capitalized ext

This commit is contained in:
Chocobozzz 2021-06-08 09:33:03 +02:00
parent 2951065162
commit ea54cd04c1
No known key found for this signature in database
GPG key ID: 583A612D890159BE
10 changed files with 82 additions and 49 deletions

View file

@ -8,7 +8,7 @@
import { exec, ExecOptions } from 'child_process'
import { BinaryToTextEncoding, createHash, randomBytes } from 'crypto'
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 { pipeline } from 'stream'
import { URL } from 'url'
@ -32,6 +32,18 @@ const objectConverter = (oldObject: any, keyConverter: (e: string) => string, va
return newObject
}
function mapToJSON (map: Map<any, any>) {
const obj: any = {}
for (const [ k, v ] of map) {
obj[k] = v
}
return obj
}
// ---------------------------------------------------------------------------
const timeTable = {
ms: 1,
second: 1000,
@ -110,6 +122,8 @@ export function parseBytes (value: string | number): number {
}
}
// ---------------------------------------------------------------------------
function sanitizeUrl (url: string) {
const urlObject = new URL(url)
@ -129,6 +143,8 @@ function sanitizeHost (host: string, remoteScheme: string) {
return host.replace(new RegExp(`:${toRemove}$`), '')
}
// ---------------------------------------------------------------------------
function isTestInstance () {
return process.env.NODE_ENV === 'test'
}
@ -141,6 +157,8 @@ function getAppNumber () {
return process.env.NODE_APP_INSTANCE
}
// ---------------------------------------------------------------------------
let rootPath: string
function root () {
@ -154,28 +172,20 @@ function root () {
return rootPath
}
function pageToStartAndCount (page: number, itemsPerPage: number) {
const start = (page - 1) * itemsPerPage
return { start, count: itemsPerPage }
}
function mapToJSON (map: Map<any, any>) {
const obj: any = {}
for (const [ k, v ] of map) {
obj[k] = v
}
return obj
}
function buildPath (path: string) {
if (isAbsolute(path)) return path
return join(root(), path)
}
function getLowercaseExtension (filename: string) {
const ext = extname(filename) || ''
return ext.toLowerCase()
}
// ---------------------------------------------------------------------------
// Consistent with .length, lodash truncate function is not
function peertubeTruncate (str: string, options: { length: number, separator?: RegExp, omission?: string }) {
const truncatedStr = truncate(str, options)
@ -189,6 +199,27 @@ function peertubeTruncate (str: string, options: { length: number, separator?: R
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') {
return createHash('sha256').update(str).digest(encoding)
}
@ -197,6 +228,8 @@ function sha1 (str: string | Buffer, encoding: BinaryToTextEncoding = 'hex') {
return createHash('sha1').update(str).digest(encoding)
}
// ---------------------------------------------------------------------------
function execShell (command: string, options?: ExecOptions) {
return new Promise<{ err?: Error, stdout: string, stderr: string }>((res, rej) => {
exec(command, options, (err, stdout, stderr) => {
@ -208,6 +241,8 @@ function execShell (command: string, options?: ExecOptions) {
})
}
// ---------------------------------------------------------------------------
function promisify0<A> (func: (cb: (err: any, result: A) => void) => void): () => Promise<A> {
return function promisified (): Promise<A> {
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)
}
}
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 createPrivateKey = promisify1<number, { key: string }>(pem.createPrivateKey)
const getPublicKey = promisify1<string, { publicKey: string }>(pem.getPublicKey)
@ -259,17 +283,21 @@ export {
getAppNumber,
objectConverter,
mapToJSON,
root,
pageToStartAndCount,
buildPath,
getLowercaseExtension,
sanitizeUrl,
sanitizeHost,
buildPath,
execShell,
pageToStartAndCount,
peertubeTruncate,
sha256,
sha1,
mapToJSON,
promisify0,
promisify1,