import * as express from 'express'
import { move } from 'fs-extra'
-import { extname } from 'path'
+import { getLowercaseExtension } from '@server/helpers/core-utils'
import { deleteResumableUploadMetaFile, getResumableUploadPath } from '@server/helpers/upload'
import { createTorrentAndSetInfoHash } from '@server/helpers/webtorrent'
import { getLocalVideoActivityPubUrl } from '@server/lib/activitypub/url'
async function buildNewFile (video: MVideo, videoPhysicalFile: express.VideoUploadFile) {
const videoFile = new VideoFileModel({
- extname: extname(videoPhysicalFile.filename),
+ extname: getLowercaseExtension(videoPhysicalFile.filename),
size: videoPhysicalFile.size,
videoStreamingPlaylistId: null,
metadata: await getMetadataFromFile(videoPhysicalFile.path)
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'
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,
}
}
+// ---------------------------------------------------------------------------
+
function sanitizeUrl (url: string) {
const urlObject = new URL(url)
return host.replace(new RegExp(`:${toRemove}$`), '')
}
+// ---------------------------------------------------------------------------
+
function isTestInstance () {
return process.env.NODE_ENV === 'test'
}
return process.env.NODE_APP_INSTANCE
}
+// ---------------------------------------------------------------------------
+
let rootPath: string
function root () {
return rootPath
}
-function pageToStartAndCount (page: number, itemsPerPage: number) {
- const start = (page - 1) * itemsPerPage
+function buildPath (path: string) {
+ if (isAbsolute(path)) return path
- return { start, count: itemsPerPage }
+ return join(root(), path)
}
-function mapToJSON (map: Map<any, any>) {
- const obj: any = {}
-
- for (const [ k, v ] of map) {
- obj[k] = v
- }
+function getLowercaseExtension (filename: string) {
+ const ext = extname(filename) || ''
- 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
function peertubeTruncate (str: string, options: { length: number, separator?: RegExp, omission?: string }) {
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)
}
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) => {
})
}
+// ---------------------------------------------------------------------------
+
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) => {
}
}
-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)
getAppNumber,
objectConverter,
+ mapToJSON,
+
root,
- pageToStartAndCount,
+ buildPath,
+ getLowercaseExtension,
sanitizeUrl,
sanitizeHost,
- buildPath,
+
execShell,
+
+ pageToStartAndCount,
peertubeTruncate,
sha256,
sha1,
- mapToJSON,
promisify0,
promisify1,
import * as express from 'express'
import * as multer from 'multer'
-import { extname } from 'path'
import { HttpStatusCode } from '../../shared/core-utils/miscs/http-error-codes'
import { CONFIG } from '../initializers/config'
import { REMOTE_SCHEME } from '../initializers/constants'
+import { getLowercaseExtension } from './core-utils'
import { isArray } from './custom-validators/misc'
import { logger } from './logger'
import { deleteFileAndCatch, generateRandomString } from './utils'
filename: async (req, file, cb) => {
let extension: string
- const fileExtension = extname(file.originalname)
+ const fileExtension = getLowercaseExtension(file.originalname)
const extensionFromMimetype = getExtFromMimetype(mimeTypes, file.mimetype)
// Take the file extension if we don't understand the mime type
import { copy, readFile, remove, rename } from 'fs-extra'
import * as Jimp from 'jimp'
-import { extname } from 'path'
import { v4 as uuidv4 } from 'uuid'
+import { getLowercaseExtension } from './core-utils'
import { convertWebPToJPG, processGIF } from './ffmpeg-utils'
import { logger } from './logger'
newSize: { width: number, height: number },
keepOriginal = false
) {
- const extension = extname(path)
+ const extension = getLowercaseExtension(path)
if (path === destination) {
throw new Error('Jimp/FFmpeg needs an input path different that the output path.')
await remove(destination)
// 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)
}
-import { extname } from 'path'
import { v4 as uuidv4 } from 'uuid'
+import { getLowercaseExtension } from '@server/helpers/core-utils'
import { isActivityPubUrlValid } from '@server/helpers/custom-validators/activitypub/misc'
import { MIMETYPES } from '@server/initializers/constants'
import { ActorModel } from '@server/models/actor/actor'
if (icon.mediaType) {
extension = mimetypes.MIMETYPE_EXT[icon.mediaType]
} else {
- const tmp = extname(icon.url)
+ const tmp = getLowercaseExtension(icon.url)
if (mimetypes.EXT_MIMETYPE[tmp] !== undefined) extension = tmp
}
import * as Bull from 'bull'
import { copy, stat } from 'fs-extra'
import { extname } from 'path'
+import { getLowercaseExtension } from '@server/helpers/core-utils'
import { createTorrentAndSetInfoHash } from '@server/helpers/webtorrent'
import { generateVideoFilename, getVideoFilePath } from '@server/lib/video-paths'
import { UserModel } from '@server/models/user/user'
const { size } = await stat(inputFilePath)
const fps = await getVideoFileFPS(inputFilePath)
- const fileExt = extname(inputFilePath)
+ const fileExt = getLowercaseExtension(inputFilePath)
const currentVideoFile = video.VideoFiles.find(videoFile => videoFile.resolution === videoFileResolution)
import * as Bull from 'bull'
import { move, remove, stat } from 'fs-extra'
import { extname } from 'path'
+import { getLowercaseExtension } from '@server/helpers/core-utils'
import { retryTransactionWrapper } from '@server/helpers/database-utils'
import { YoutubeDL } from '@server/helpers/youtube-dl'
import { isPostImportVideoAccepted } from '@server/lib/moderation'
const duration = await getDurationFromVideoFile(tempVideoPath)
// Prepare video file object for creation in database
- const fileExt = extname(tempVideoPath)
+ const fileExt = getLowercaseExtension(tempVideoPath)
const videoFileData = {
extname: fileExt,
resolution: videoFileResolution,
import 'multer'
import { queue } from 'async'
import * as LRUCache from 'lru-cache'
-import { extname, join } from 'path'
+import { join } from 'path'
import { v4 as uuidv4 } from 'uuid'
+import { getLowercaseExtension } from '@server/helpers/core-utils'
import { ActorModel } from '@server/models/actor/actor'
import { ActivityPubActorType, ActorImageType } from '@shared/models'
import { retryTransactionWrapper } from '../helpers/database-utils'
? ACTOR_IMAGES_SIZE.AVATARS
: ACTOR_IMAGES_SIZE.BANNERS
- const extension = extname(imagePhysicalFile.filename)
+ const extension = getLowercaseExtension(imagePhysicalFile.filename)
const imageName = uuidv4() + extension
const destination = join(CONFIG.STORAGE.ACTOR_IMAGES, imageName)
import { values } from 'lodash'
-import { extname } from 'path'
import { literal, Op, Transaction } from 'sequelize'
import {
AllowNull,
Table,
UpdatedAt
} from 'sequelize-typescript'
+import { getLowercaseExtension } from '@server/helpers/core-utils'
import { ModelCache } from '@server/models/model-cache'
import { AttributesOnly } from '@shared/core-utils'
import { ActivityIconObject, ActivityPubActorType } from '../../../shared/models/activitypub'
let image: ActivityIconObject
if (this.avatarId) {
- const extension = extname(this.Avatar.filename)
+ const extension = getLowercaseExtension(this.Avatar.filename)
icon = {
type: 'Image',
if (this.bannerId) {
const banner = (this as MActorAPChannel).Banner
- const extension = extname(banner.filename)
+ const extension = getLowercaseExtension(banner.filename)
image = {
type: 'Image',
import { createReadStream, pathExists, readdir, readFile, stat } from 'fs-extra'
import got, { Response as GotResponse } from 'got/dist/source'
import * as parseTorrent from 'parse-torrent'
-import { extname, join } from 'path'
+import { join } from 'path'
import * as request from 'supertest'
import { v4 as uuidv4 } from 'uuid'
import validator from 'validator'
+import { getLowercaseExtension } from '@server/helpers/core-utils'
import { HttpStatusCode } from '@shared/core-utils'
import { VideosCommonQuery } from '@shared/models'
import { loadLanguages, VIDEO_CATEGORIES, VIDEO_LANGUAGES, VIDEO_LICENCES, VIDEO_PRIVACIES } from '../../../server/initializers/constants'
const file = videoDetails.files.find(f => f.resolution.id === attributeFile.resolution)
expect(file).not.to.be.undefined
- let extension = extname(attributes.fixture)
+ let extension = getLowercaseExtension(attributes.fixture)
// Transcoding enabled: extension will always be .mp4
if (attributes.files.length > 1) extension = '.mp4'