X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fhelpers%2Fcore-utils.ts;h=b1e9af0a17a61325b7d8241814ad294498692799;hb=b6a1dd4d1b3b0032f8b968e72cbd074f646e8827;hp=84e33c0e987a253dded722800e7d9b0f460c96ac;hpb=0e5ff97f6fdf9a4cebe5a15f5a390380465803ad;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/helpers/core-utils.ts b/server/helpers/core-utils.ts index 84e33c0e9..b1e9af0a1 100644 --- a/server/helpers/core-utils.ts +++ b/server/helpers/core-utils.ts @@ -12,6 +12,24 @@ import { URL } from 'url' import { truncate } from 'lodash' import { exec } from 'child_process' +const objectConverter = (oldObject: any, keyConverter: (e: string) => string, valueConverter: (e: any) => any) => { + if (!oldObject || typeof oldObject !== 'object') { + return valueConverter(oldObject) + } + + if (Array.isArray(oldObject)) { + return oldObject.map(e => objectConverter(e, keyConverter, valueConverter)) + } + + const newObject = {} + Object.keys(oldObject).forEach(oldKey => { + const newKey = keyConverter(oldKey) + newObject[ newKey ] = objectConverter(oldObject[ oldKey ], keyConverter, valueConverter) + }) + + return newObject +} + const timeTable = { ms: 1, second: 1000, @@ -22,7 +40,7 @@ const timeTable = { month: 3600000 * 24 * 30 } -export function parseDuration (duration: number | string): number { +export function parseDurationToMs (duration: number | string): number { if (typeof duration === 'number') return duration if (typeof duration === 'string') { @@ -39,7 +57,7 @@ export function parseDuration (duration: number | string): number { } } - throw new Error('Duration could not be properly parsed') + throw new Error(`Duration ${duration} could not be properly parsed`) } export function parseBytes (value: string | number): number { @@ -116,6 +134,10 @@ function isProdInstance () { return process.env.NODE_ENV === 'production' } +function getAppNumber () { + return process.env.NODE_APP_INSTANCE +} + function root () { // We are in /helpers/utils.js const paths = [ __dirname, '..', '..' ] @@ -174,10 +196,14 @@ function peertubeTruncate (str: string, maxLength: number) { return truncate(str, options) } -function sha256 (str: string, encoding: HexBase64Latin1Encoding = 'hex') { +function sha256 (str: string | Buffer, encoding: HexBase64Latin1Encoding = 'hex') { return createHash('sha256').update(str).digest(encoding) } +function sha1 (str: string | Buffer, encoding: HexBase64Latin1Encoding = 'hex') { + return createHash('sha1').update(str).digest(encoding) +} + function promisify0 (func: (cb: (err: any, result: A) => void) => void): () => Promise { return function promisified (): Promise { return new Promise((resolve: (arg: A) => void, reject: (err: any) => void) => { @@ -234,7 +260,9 @@ const execPromise = promisify1(exec) export { isTestInstance, isProdInstance, + getAppNumber, + objectConverter, root, escapeHTML, pageToStartAndCount, @@ -242,7 +270,9 @@ export { sanitizeHost, buildPath, peertubeTruncate, + sha256, + sha1, promisify0, promisify1,