X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fhelpers%2Futils.ts;h=a1ed8e72df461650e7f697c7bebf8cc94b62a5a2;hb=41a676db3989fe3eca91301ac5f5aea30d98654a;hp=ef6a878cfebe1ee0abf4f0c4426f81673cf69b49;hpb=50d6de9c286abcb34ff4234d56d9cbb803db7665;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/helpers/utils.ts b/server/helpers/utils.ts index ef6a878cf..a1ed8e72d 100644 --- a/server/helpers/utils.ts +++ b/server/helpers/utils.ts @@ -1,16 +1,16 @@ -import * as express from 'express' -import { Model } from 'sequelize-typescript' import { ResultList } from '../../shared' -import { VideoResolution } from '../../shared/models/videos' import { CONFIG } from '../initializers' -import { UserModel } from '../models/account/user' import { ActorModel } from '../models/activitypub/actor' import { ApplicationModel } from '../models/application/application' -import { pseudoRandomBytesPromise } from './core-utils' +import { pseudoRandomBytesPromise, sha256 } from './core-utils' import { logger } from './logger' +import { join } from 'path' +import { Instance as ParseTorrent } from 'parse-torrent' +import { remove } from 'fs-extra' -function badRequest (req: express.Request, res: express.Response, next: express.NextFunction) { - return res.type('json').status(400).end() +function deleteFileAsync (path: string) { + remove(path) + .catch(err => logger.error('Cannot delete the file %s asynchronously.', path, { err })) } async function generateRandomString (size: number) { @@ -20,93 +20,59 @@ async function generateRandomString (size: number) { } interface FormattableToJSON { - toFormattedJSON () + toFormattedJSON (args?: any) } -function getFormattedObjects (objects: T[], objectsTotal: number) { +function getFormattedObjects (objects: T[], objectsTotal: number, formattedArg?: any) { const formattedObjects: U[] = [] objects.forEach(object => { - formattedObjects.push(object.toFormattedJSON()) + formattedObjects.push(object.toFormattedJSON(formattedArg)) }) - const res: ResultList = { + return { total: objectsTotal, data: formattedObjects - } - - return res + } as ResultList } -async function isSignupAllowed () { - if (CONFIG.SIGNUP.ENABLED === false) { - return false - } +async function getServerActor () { + if (getServerActor.serverActor === undefined) { + const application = await ApplicationModel.load() + if (!application) throw Error('Could not load Application from database.') - // No limit and signup is enabled - if (CONFIG.SIGNUP.LIMIT === -1) { - return true + getServerActor.serverActor = application.Account.Actor } - const totalUsers = await UserModel.countTotal() - - return totalUsers < CONFIG.SIGNUP.LIMIT -} - -function computeResolutionsToTranscode (videoFileHeight: number) { - const resolutionsEnabled: number[] = [] - const configResolutions = CONFIG.TRANSCODING.RESOLUTIONS - - const resolutions = [ - VideoResolution.H_240P, - VideoResolution.H_360P, - VideoResolution.H_480P, - VideoResolution.H_720P, - VideoResolution.H_1080P - ] - - for (const resolution of resolutions) { - if (configResolutions[resolution.toString()] === true && videoFileHeight > resolution) { - resolutionsEnabled.push(resolution) - } + if (!getServerActor.serverActor) { + logger.error('Cannot load server actor.') + process.exit(0) } - return resolutionsEnabled + return Promise.resolve(getServerActor.serverActor) } - -function resetSequelizeInstance (instance: Model, savedFields: object) { - Object.keys(savedFields).forEach(key => { - const value = savedFields[key] - instance.set(key, value) - }) +namespace getServerActor { + export let serverActor: ActorModel } -let serverActor: ActorModel -async function getServerActor () { - if (serverActor === undefined) { - const application = await ApplicationModel.load() - serverActor = application.Account.Actor - } - - if (!serverActor) { - logger.error('Cannot load server actor.') - process.exit(0) - } +function generateVideoTmpPath (target: string | ParseTorrent) { + const id = typeof target === 'string' ? target : target.infoHash - return Promise.resolve(serverActor) + const hash = sha256(id) + return join(CONFIG.STORAGE.VIDEOS_DIR, hash + '-import.mp4') } -type SortType = { sortModel: any, sortValue: string } +function getSecureTorrentName (originalName: string) { + return sha256(originalName) + '.torrent' +} // --------------------------------------------------------------------------- export { - badRequest, + deleteFileAsync, generateRandomString, getFormattedObjects, - isSignupAllowed, - computeResolutionsToTranscode, - resetSequelizeInstance, + getSecureTorrentName, getServerActor, - SortType + generateVideoTmpPath }