1 import * as express from 'express'
2 import * as multer from 'multer'
3 import { Model } from 'sequelize-typescript'
4 import { ResultList } from '../../shared'
5 import { VideoResolution } from '../../shared/models/videos'
6 import { CONFIG, REMOTE_SCHEME } from '../initializers'
7 import { UserModel } from '../models/account/user'
8 import { ActorModel } from '../models/activitypub/actor'
9 import { ApplicationModel } from '../models/application/application'
10 import { pseudoRandomBytesPromise } from './core-utils'
11 import { logger } from './logger'
13 function getHostWithPort (host: string) {
14 const splitted = host.split(':')
16 // The port was not specified
17 if (splitted.length === 1) {
18 if (REMOTE_SCHEME.HTTP === 'https') return host + ':443'
26 function badRequest (req: express.Request, res: express.Response, next: express.NextFunction) {
27 return res.type('json').status(400).end()
30 function createReqFiles (fieldName: string, storageDir: string, mimeTypes: { [ id: string ]: string }) {
31 const storage = multer.diskStorage({
32 destination: (req, file, cb) => {
36 filename: async (req, file, cb) => {
37 const extension = mimeTypes[file.mimetype]
41 randomString = await generateRandomString(16)
43 logger.error('Cannot generate random string for file name.', err)
44 randomString = 'fake-random-string'
47 cb(null, randomString + extension)
51 return multer({ storage }).fields([{ name: fieldName, maxCount: 1 }])
54 async function generateRandomString (size: number) {
55 const raw = await pseudoRandomBytesPromise(size)
57 return raw.toString('hex')
60 interface FormattableToJSON {
64 function getFormattedObjects<U, T extends FormattableToJSON> (objects: T[], objectsTotal: number) {
65 const formattedObjects: U[] = []
67 objects.forEach(object => {
68 formattedObjects.push(object.toFormattedJSON())
71 const res: ResultList<U> = {
73 data: formattedObjects
79 async function isSignupAllowed () {
80 if (CONFIG.SIGNUP.ENABLED === false) {
84 // No limit and signup is enabled
85 if (CONFIG.SIGNUP.LIMIT === -1) {
89 const totalUsers = await UserModel.countTotal()
91 return totalUsers < CONFIG.SIGNUP.LIMIT
94 function computeResolutionsToTranscode (videoFileHeight: number) {
95 const resolutionsEnabled: number[] = []
96 const configResolutions = CONFIG.TRANSCODING.RESOLUTIONS
99 VideoResolution.H_240P,
100 VideoResolution.H_360P,
101 VideoResolution.H_480P,
102 VideoResolution.H_720P,
103 VideoResolution.H_1080P
106 for (const resolution of resolutions) {
107 if (configResolutions[resolution.toString()] === true && videoFileHeight > resolution) {
108 resolutionsEnabled.push(resolution)
112 return resolutionsEnabled
115 function resetSequelizeInstance (instance: Model<any>, savedFields: object) {
116 Object.keys(savedFields).forEach(key => {
117 const value = savedFields[key]
118 instance.set(key, value)
122 let serverActor: ActorModel
123 async function getServerActor () {
124 if (serverActor === undefined) {
125 const application = await ApplicationModel.load()
126 serverActor = application.Account.Actor
130 logger.error('Cannot load server actor.')
134 return Promise.resolve(serverActor)
137 type SortType = { sortModel: any, sortValue: string }
139 // ---------------------------------------------------------------------------
143 generateRandomString,
146 computeResolutionsToTranscode,
147 resetSequelizeInstance,