Import torrents with webtorrent
[oweals/peertube.git] / server / helpers / utils.ts
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'
15
16 const isCidr = require('is-cidr')
17
18 function cleanUpReqFiles (req: { files: { [ fieldname: string ]: Express.Multer.File[] } | Express.Multer.File[] }) {
19   const files = req.files
20
21   if (!files) return
22
23   if (isArray(files)) {
24     (files as Express.Multer.File[]).forEach(f => deleteFileAsync(f.path))
25     return
26   }
27
28   for (const key of Object.keys(files)) {
29     const file = files[key]
30
31     if (isArray(file)) file.forEach(f => deleteFileAsync(f.path))
32     else deleteFileAsync(file.path)
33   }
34 }
35
36 function deleteFileAsync (path: string) {
37   unlinkPromise(path)
38     .catch(err => logger.error('Cannot delete the file %s asynchronously.', path, { err }))
39 }
40
41 async function generateRandomString (size: number) {
42   const raw = await pseudoRandomBytesPromise(size)
43
44   return raw.toString('hex')
45 }
46
47 interface FormattableToJSON {
48   toFormattedJSON (args?: any)
49 }
50
51 function getFormattedObjects<U, T extends FormattableToJSON> (objects: T[], objectsTotal: number, formattedArg?: any) {
52   const formattedObjects: U[] = []
53
54   objects.forEach(object => {
55     formattedObjects.push(object.toFormattedJSON(formattedArg))
56   })
57
58   return {
59     total: objectsTotal,
60     data: formattedObjects
61   } as ResultList<U>
62 }
63
64 async function isSignupAllowed () {
65   if (CONFIG.SIGNUP.ENABLED === false) {
66     return false
67   }
68
69   // No limit and signup is enabled
70   if (CONFIG.SIGNUP.LIMIT === -1) {
71     return true
72   }
73
74   const totalUsers = await UserModel.countTotal()
75
76   return totalUsers < CONFIG.SIGNUP.LIMIT
77 }
78
79 function isSignupAllowedForCurrentIP (ip: string) {
80   const addr = ipaddr.parse(ip)
81   let excludeList = [ 'blacklist' ]
82   let matched = ''
83
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')
87   }
88
89   if (addr.kind() === 'ipv4') {
90     const addrV4 = ipaddr.IPv4.parse(ip)
91     const rangeList = {
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))
96     }
97     matched = ipaddr.subnetMatch(addrV4, rangeList, 'unknown')
98   } else if (addr.kind() === 'ipv6') {
99     const addrV6 = ipaddr.IPv6.parse(ip)
100     const rangeList = {
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))
105     }
106     matched = ipaddr.subnetMatch(addrV6, rangeList, 'unknown')
107   }
108
109   return !excludeList.includes(matched)
110 }
111
112 function computeResolutionsToTranscode (videoFileHeight: number) {
113   const resolutionsEnabled: number[] = []
114   const configResolutions = CONFIG.TRANSCODING.RESOLUTIONS
115
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
123   ]
124
125   for (const resolution of resolutions) {
126     if (configResolutions[ resolution + 'p' ] === true && videoFileHeight > resolution) {
127       resolutionsEnabled.push(resolution)
128     }
129   }
130
131   return resolutionsEnabled
132 }
133
134 const timeTable = {
135   ms:           1,
136   second:       1000,
137   minute:       60000,
138   hour:         3600000,
139   day:          3600000 * 24,
140   week:         3600000 * 24 * 7,
141   month:        3600000 * 24 * 30
142 }
143 export function parseDuration (duration: number | string): number {
144   if (typeof duration === 'number') return duration
145
146   if (typeof duration === 'string') {
147     const split = duration.match(/^([\d\.,]+)\s?(\w+)$/)
148
149     if (split.length === 3) {
150       const len = parseFloat(split[1])
151       let unit = split[2].replace(/s$/i,'').toLowerCase()
152       if (unit === 'm') {
153         unit = 'ms'
154       }
155
156       return (len || 1) * (timeTable[unit] || 0)
157     }
158   }
159
160   throw new Error('Duration could not be properly parsed')
161 }
162
163 function resetSequelizeInstance (instance: Model<any>, savedFields: object) {
164   Object.keys(savedFields).forEach(key => {
165     const value = savedFields[key]
166     instance.set(key, value)
167   })
168 }
169
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.')
175
176     serverActor = application.Account.Actor
177   }
178
179   if (!serverActor) {
180     logger.error('Cannot load server actor.')
181     process.exit(0)
182   }
183
184   return Promise.resolve(serverActor)
185 }
186
187 function generateVideoTmpPath (target: string | ParseTorrent) {
188   const id = typeof target === 'string' ? target : target.infoHash
189
190   const hash = sha256(id)
191   return join(CONFIG.STORAGE.VIDEOS_DIR, hash + '-import.mp4')
192 }
193
194 function getSecureTorrentName (originalName: string) {
195   return sha256(originalName) + '.torrent'
196 }
197
198 type SortType = { sortModel: any, sortValue: string }
199
200 // ---------------------------------------------------------------------------
201
202 export {
203   cleanUpReqFiles,
204   deleteFileAsync,
205   generateRandomString,
206   getFormattedObjects,
207   isSignupAllowed,
208   getSecureTorrentName,
209   isSignupAllowedForCurrentIP,
210   computeResolutionsToTranscode,
211   resetSequelizeInstance,
212   getServerActor,
213   SortType,
214   generateVideoTmpPath
215 }