diff options
Diffstat (limited to 'server/helpers/core-utils.ts')
-rw-r--r-- | server/helpers/core-utils.ts | 15 |
1 files changed, 10 insertions, 5 deletions
diff --git a/server/helpers/core-utils.ts b/server/helpers/core-utils.ts index 3fb824e36..305d3b71e 100644 --- a/server/helpers/core-utils.ts +++ b/server/helpers/core-utils.ts | |||
@@ -11,14 +11,13 @@ import * as pem from 'pem' | |||
11 | import { URL } from 'url' | 11 | import { URL } from 'url' |
12 | import { truncate } from 'lodash' | 12 | import { truncate } from 'lodash' |
13 | import { exec } from 'child_process' | 13 | import { exec } from 'child_process' |
14 | import { isArray } from './custom-validators/misc' | ||
15 | 14 | ||
16 | const objectConverter = (oldObject: any, keyConverter: (e: string) => string, valueConverter: (e: any) => any) => { | 15 | const objectConverter = (oldObject: any, keyConverter: (e: string) => string, valueConverter: (e: any) => any) => { |
17 | if (!oldObject || typeof oldObject !== 'object') { | 16 | if (!oldObject || typeof oldObject !== 'object') { |
18 | return valueConverter(oldObject) | 17 | return valueConverter(oldObject) |
19 | } | 18 | } |
20 | 19 | ||
21 | if (isArray(oldObject)) { | 20 | if (Array.isArray(oldObject)) { |
22 | return oldObject.map(e => objectConverter(e, keyConverter, valueConverter)) | 21 | return oldObject.map(e => objectConverter(e, keyConverter, valueConverter)) |
23 | } | 22 | } |
24 | 23 | ||
@@ -41,7 +40,7 @@ const timeTable = { | |||
41 | month: 3600000 * 24 * 30 | 40 | month: 3600000 * 24 * 30 |
42 | } | 41 | } |
43 | 42 | ||
44 | export function parseDuration (duration: number | string): number { | 43 | export function parseDurationToMs (duration: number | string): number { |
45 | if (typeof duration === 'number') return duration | 44 | if (typeof duration === 'number') return duration |
46 | 45 | ||
47 | if (typeof duration === 'string') { | 46 | if (typeof duration === 'string') { |
@@ -58,7 +57,7 @@ export function parseDuration (duration: number | string): number { | |||
58 | } | 57 | } |
59 | } | 58 | } |
60 | 59 | ||
61 | throw new Error('Duration could not be properly parsed') | 60 | throw new Error(`Duration ${duration} could not be properly parsed`) |
62 | } | 61 | } |
63 | 62 | ||
64 | export function parseBytes (value: string | number): number { | 63 | export function parseBytes (value: string | number): number { |
@@ -193,10 +192,14 @@ function peertubeTruncate (str: string, maxLength: number) { | |||
193 | return truncate(str, options) | 192 | return truncate(str, options) |
194 | } | 193 | } |
195 | 194 | ||
196 | function sha256 (str: string, encoding: HexBase64Latin1Encoding = 'hex') { | 195 | function sha256 (str: string | Buffer, encoding: HexBase64Latin1Encoding = 'hex') { |
197 | return createHash('sha256').update(str).digest(encoding) | 196 | return createHash('sha256').update(str).digest(encoding) |
198 | } | 197 | } |
199 | 198 | ||
199 | function sha1 (str: string | Buffer, encoding: HexBase64Latin1Encoding = 'hex') { | ||
200 | return createHash('sha1').update(str).digest(encoding) | ||
201 | } | ||
202 | |||
200 | function promisify0<A> (func: (cb: (err: any, result: A) => void) => void): () => Promise<A> { | 203 | function promisify0<A> (func: (cb: (err: any, result: A) => void) => void): () => Promise<A> { |
201 | return function promisified (): Promise<A> { | 204 | return function promisified (): Promise<A> { |
202 | return new Promise<A>((resolve: (arg: A) => void, reject: (err: any) => void) => { | 205 | return new Promise<A>((resolve: (arg: A) => void, reject: (err: any) => void) => { |
@@ -262,7 +265,9 @@ export { | |||
262 | sanitizeHost, | 265 | sanitizeHost, |
263 | buildPath, | 266 | buildPath, |
264 | peertubeTruncate, | 267 | peertubeTruncate, |
268 | |||
265 | sha256, | 269 | sha256, |
270 | sha1, | ||
266 | 271 | ||
267 | promisify0, | 272 | promisify0, |
268 | promisify1, | 273 | promisify1, |