1 import { Model } from 'sequelize-typescript'
2 import * as ipaddr from 'ipaddr.js'
3 import { ResultList } from '../../shared'
4 import { VideoResolution } from '../../shared/models/videos'
5 import { CONFIG } from '../initializers'
6 import { UserModel } from '../models/account/user'
7 import { ActorModel } from '../models/activitypub/actor'
8 import { ApplicationModel } from '../models/application/application'
9 import { pseudoRandomBytesPromise, sha256, unlinkPromise } from './core-utils'
10 import { logger } from './logger'
11 import { isArray } from './custom-validators/misc'
12 import * as crypto from "crypto"
13 import { join } from "path"
14 import { Instance as ParseTorrent } from 'parse-torrent'
16 const isCidr = require('is-cidr')
18 function cleanUpReqFiles (req: { files: { [ fieldname: string ]: Express.Multer.File[] } | Express.Multer.File[] }) {
19 const files = req.files
24 (files as Express.Multer.File[]).forEach(f => deleteFileAsync(f.path))
28 for (const key of Object.keys(files)) {
29 const file = files[key]
31 if (isArray(file)) file.forEach(f => deleteFileAsync(f.path))
32 else deleteFileAsync(file.path)
36 function deleteFileAsync (path: string) {
38 .catch(err => logger.error('Cannot delete the file %s asynchronously.', path, { err }))
41 async function generateRandomString (size: number) {
42 const raw = await pseudoRandomBytesPromise(size)
44 return raw.toString('hex')
47 interface FormattableToJSON {
48 toFormattedJSON (args?: any)
51 function getFormattedObjects<U, T extends FormattableToJSON> (objects: T[], objectsTotal: number, formattedArg?: any) {
52 const formattedObjects: U[] = []
54 objects.forEach(object => {
55 formattedObjects.push(object.toFormattedJSON(formattedArg))
60 data: formattedObjects
64 async function isSignupAllowed () {
65 if (CONFIG.SIGNUP.ENABLED === false) {
69 // No limit and signup is enabled
70 if (CONFIG.SIGNUP.LIMIT === -1) {
74 const totalUsers = await UserModel.countTotal()
76 return totalUsers < CONFIG.SIGNUP.LIMIT
79 function isSignupAllowedForCurrentIP (ip: string) {
80 const addr = ipaddr.parse(ip)
81 let excludeList = [ 'blacklist' ]
84 // if there is a valid, non-empty whitelist, we exclude all unknown adresses too
85 if (CONFIG.SIGNUP.FILTERS.CIDR.WHITELIST.filter(cidr => isCidr(cidr)).length > 0) {
86 excludeList.push('unknown')
89 if (addr.kind() === 'ipv4') {
90 const addrV4 = ipaddr.IPv4.parse(ip)
92 whitelist: CONFIG.SIGNUP.FILTERS.CIDR.WHITELIST.filter(cidr => isCidr.v4(cidr))
93 .map(cidr => ipaddr.IPv4.parseCIDR(cidr)),
94 blacklist: CONFIG.SIGNUP.FILTERS.CIDR.BLACKLIST.filter(cidr => isCidr.v4(cidr))
95 .map(cidr => ipaddr.IPv4.parseCIDR(cidr))
97 matched = ipaddr.subnetMatch(addrV4, rangeList, 'unknown')
98 } else if (addr.kind() === 'ipv6') {
99 const addrV6 = ipaddr.IPv6.parse(ip)
101 whitelist: CONFIG.SIGNUP.FILTERS.CIDR.WHITELIST.filter(cidr => isCidr.v6(cidr))
102 .map(cidr => ipaddr.IPv6.parseCIDR(cidr)),
103 blacklist: CONFIG.SIGNUP.FILTERS.CIDR.BLACKLIST.filter(cidr => isCidr.v6(cidr))
104 .map(cidr => ipaddr.IPv6.parseCIDR(cidr))
106 matched = ipaddr.subnetMatch(addrV6, rangeList, 'unknown')
109 return !excludeList.includes(matched)
112 function computeResolutionsToTranscode (videoFileHeight: number) {
113 const resolutionsEnabled: number[] = []
114 const configResolutions = CONFIG.TRANSCODING.RESOLUTIONS
116 // Put in the order we want to proceed jobs
117 const resolutions = [
118 VideoResolution.H_480P,
119 VideoResolution.H_360P,
120 VideoResolution.H_720P,
121 VideoResolution.H_240P,
122 VideoResolution.H_1080P
125 for (const resolution of resolutions) {
126 if (configResolutions[ resolution + 'p' ] === true && videoFileHeight > resolution) {
127 resolutionsEnabled.push(resolution)
131 return resolutionsEnabled
140 week: 3600000 * 24 * 7,
141 month: 3600000 * 24 * 30
143 export function parseDuration (duration: number | string): number {
144 if (typeof duration === 'number') return duration
146 if (typeof duration === 'string') {
147 const split = duration.match(/^([\d\.,]+)\s?(\w+)$/)
149 if (split.length === 3) {
150 const len = parseFloat(split[1])
151 let unit = split[2].replace(/s$/i,'').toLowerCase()
156 return (len || 1) * (timeTable[unit] || 0)
160 throw new Error('Duration could not be properly parsed')
163 function resetSequelizeInstance (instance: Model<any>, savedFields: object) {
164 Object.keys(savedFields).forEach(key => {
165 const value = savedFields[key]
166 instance.set(key, value)
170 let serverActor: ActorModel
171 async function getServerActor () {
172 if (serverActor === undefined) {
173 const application = await ApplicationModel.load()
174 if (!application) throw Error('Could not load Application from database.')
176 serverActor = application.Account.Actor
180 logger.error('Cannot load server actor.')
184 return Promise.resolve(serverActor)
187 function generateVideoTmpPath (target: string | ParseTorrent) {
188 const id = typeof target === 'string' ? target : target.infoHash
190 const hash = sha256(id)
191 return join(CONFIG.STORAGE.VIDEOS_DIR, hash + '-import.mp4')
194 function getSecureTorrentName (originalName: string) {
195 return sha256(originalName) + '.torrent'
198 type SortType = { sortModel: any, sortValue: string }
200 // ---------------------------------------------------------------------------
205 generateRandomString,
208 getSecureTorrentName,
209 isSignupAllowedForCurrentIP,
210 computeResolutionsToTranscode,
211 resetSequelizeInstance,