diff options
author | Chocobozzz <me@florianbigard.com> | 2018-08-14 15:28:30 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2018-08-14 15:28:30 +0200 |
commit | 06215f15e0a9fea2ef95b8b49cb2b5868fb64017 (patch) | |
tree | 6f7ff9f82ea851ea87fd3fc4b61843c7a38aec43 /server/helpers/core-utils.ts | |
parent | 59c76ffa8f503e962d517c78f033f1beccb1de1a (diff) | |
download | PeerTube-06215f15e0a9fea2ef95b8b49cb2b5868fb64017.tar.gz PeerTube-06215f15e0a9fea2ef95b8b49cb2b5868fb64017.tar.zst PeerTube-06215f15e0a9fea2ef95b8b49cb2b5868fb64017.zip |
Cleanup utils helper
Diffstat (limited to 'server/helpers/core-utils.ts')
-rw-r--r-- | server/helpers/core-utils.ts | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/server/helpers/core-utils.ts b/server/helpers/core-utils.ts index 3b38da66c..90d2cd9b3 100644 --- a/server/helpers/core-utils.ts +++ b/server/helpers/core-utils.ts | |||
@@ -14,6 +14,35 @@ import * as rimraf from 'rimraf' | |||
14 | import { URL } from 'url' | 14 | import { URL } from 'url' |
15 | import { truncate } from 'lodash' | 15 | import { truncate } from 'lodash' |
16 | 16 | ||
17 | const timeTable = { | ||
18 | ms: 1, | ||
19 | second: 1000, | ||
20 | minute: 60000, | ||
21 | hour: 3600000, | ||
22 | day: 3600000 * 24, | ||
23 | week: 3600000 * 24 * 7, | ||
24 | month: 3600000 * 24 * 30 | ||
25 | } | ||
26 | export function parseDuration (duration: number | string): number { | ||
27 | if (typeof duration === 'number') return duration | ||
28 | |||
29 | if (typeof duration === 'string') { | ||
30 | const split = duration.match(/^([\d\.,]+)\s?(\w+)$/) | ||
31 | |||
32 | if (split.length === 3) { | ||
33 | const len = parseFloat(split[1]) | ||
34 | let unit = split[2].replace(/s$/i,'').toLowerCase() | ||
35 | if (unit === 'm') { | ||
36 | unit = 'ms' | ||
37 | } | ||
38 | |||
39 | return (len || 1) * (timeTable[unit] || 0) | ||
40 | } | ||
41 | } | ||
42 | |||
43 | throw new Error('Duration could not be properly parsed') | ||
44 | } | ||
45 | |||
17 | function sanitizeUrl (url: string) { | 46 | function sanitizeUrl (url: string) { |
18 | const urlObject = new URL(url) | 47 | const urlObject = new URL(url) |
19 | 48 | ||