Fix last commit
[oweals/peertube.git] / server / helpers / utils.ts
index e4556fa12d4a875b4665170785e3b940403d9dc1..cfb4275703280bd87e8a968b19b7f49a4ca46b46 100644 (file)
@@ -1,6 +1,5 @@
 import { Model } from 'sequelize-typescript'
 import * as ipaddr from 'ipaddr.js'
-const isCidr = require('is-cidr')
 import { ResultList } from '../../shared'
 import { VideoResolution } from '../../shared/models/videos'
 import { CONFIG } from '../initializers'
@@ -10,6 +9,8 @@ import { ApplicationModel } from '../models/application/application'
 import { pseudoRandomBytesPromise } from './core-utils'
 import { logger } from './logger'
 
+const isCidr = require('is-cidr')
+
 async function generateRandomString (size: number) {
   const raw = await pseudoRandomBytesPromise(size)
 
@@ -17,22 +18,20 @@ async function generateRandomString (size: number) {
 }
 
 interface FormattableToJSON {
-  toFormattedJSON ()
+  toFormattedJSON (args?: any)
 }
 
-function getFormattedObjects<U, T extends FormattableToJSON> (objects: T[], objectsTotal: number) {
+function getFormattedObjects<U, T extends FormattableToJSON> (objects: T[], objectsTotal: number, formattedArg?: any) {
   const formattedObjects: U[] = []
 
   objects.forEach(object => {
-    formattedObjects.push(object.toFormattedJSON())
+    formattedObjects.push(object.toFormattedJSON(formattedArg))
   })
 
-  const res: ResultList<U> = {
+  return {
     total: objectsTotal,
     data: formattedObjects
-  }
-
-  return res
+  } as ResultList<U>
 }
 
 async function isSignupAllowed () {
@@ -53,7 +52,7 @@ async function isSignupAllowed () {
 function isSignupAllowedForCurrentIP (ip: string) {
   const addr = ipaddr.parse(ip)
   let excludeList = [ 'blacklist' ]
-  let matched: string
+  let matched = ''
 
   // if there is a valid, non-empty whitelist, we exclude all unknown adresses too
   if (CONFIG.SIGNUP.FILTERS.CIDR.WHITELIST.filter(cidr => isCidr(cidr)).length > 0) {
@@ -87,16 +86,17 @@ function computeResolutionsToTranscode (videoFileHeight: number) {
   const resolutionsEnabled: number[] = []
   const configResolutions = CONFIG.TRANSCODING.RESOLUTIONS
 
+  // Put in the order we want to proceed jobs
   const resolutions = [
-    VideoResolution.H_240P,
-    VideoResolution.H_360P,
     VideoResolution.H_480P,
+    VideoResolution.H_360P,
     VideoResolution.H_720P,
+    VideoResolution.H_240P,
     VideoResolution.H_1080P
   ]
 
   for (const resolution of resolutions) {
-    if (configResolutions[resolution + 'p'] === true && videoFileHeight > resolution) {
+    if (configResolutions[ resolution + 'p' ] === true && videoFileHeight > resolution) {
       resolutionsEnabled.push(resolution)
     }
   }
@@ -104,6 +104,35 @@ function computeResolutionsToTranscode (videoFileHeight: number) {
   return resolutionsEnabled
 }
 
+const timeTable = {
+  ms:           1,
+  second:       1000,
+  minute:       60000,
+  hour:         3600000,
+  day:          3600000 * 24,
+  week:         3600000 * 24 * 7,
+  month:        3600000 * 24 * 30
+}
+export function parseDuration (duration: number | string): number {
+  if (typeof duration === 'number') return duration
+
+  if (typeof duration === 'string') {
+    const split = duration.match(/^([\d\.,]+)\s?(\w+)$/)
+
+    if (split.length === 3) {
+      const len = parseFloat(split[1])
+      let unit = split[2].replace(/s$/i,'').toLowerCase()
+      if (unit === 'm') {
+        unit = 'ms'
+      }
+
+      return (len || 1) * (timeTable[unit] || 0)
+    }
+  }
+
+  throw new Error('Duration could not be properly parsed')
+}
+
 function resetSequelizeInstance (instance: Model<any>, savedFields: object) {
   Object.keys(savedFields).forEach(key => {
     const value = savedFields[key]
@@ -115,6 +144,8 @@ let serverActor: ActorModel
 async function getServerActor () {
   if (serverActor === undefined) {
     const application = await ApplicationModel.load()
+    if (!application) throw Error('Could not load Application from database.')
+
     serverActor = application.Account.Actor
   }