diff options
author | BRAINS YUM <43896676+McFlat@users.noreply.github.com> | 2018-10-13 01:43:55 -0500 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2018-10-13 08:43:55 +0200 |
commit | 0e5ff97f6fdf9a4cebe5a15f5a390380465803ad (patch) | |
tree | e3f69adb2b8a910ef750b44972ea59944aa9ca7c /server/helpers/core-utils.ts | |
parent | 6e5a785b20230ff5457632361a893d814b53c95e (diff) | |
download | PeerTube-0e5ff97f6fdf9a4cebe5a15f5a390380465803ad.tar.gz PeerTube-0e5ff97f6fdf9a4cebe5a15f5a390380465803ad.tar.zst PeerTube-0e5ff97f6fdf9a4cebe5a15f5a390380465803ad.zip |
add parseBytes utility function and tests (#1239)
* add parseBytes utility function and tests
make it parse TB MB
fix parseBytes; * 1024
test bytes too, and make parseByte to parse quotas
add test in travis.sh in misc
* fix parseBytes and test to pass linting
Diffstat (limited to 'server/helpers/core-utils.ts')
-rw-r--r-- | server/helpers/core-utils.ts | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/server/helpers/core-utils.ts b/server/helpers/core-utils.ts index 224e4fe92..84e33c0e9 100644 --- a/server/helpers/core-utils.ts +++ b/server/helpers/core-utils.ts | |||
@@ -21,6 +21,7 @@ const timeTable = { | |||
21 | week: 3600000 * 24 * 7, | 21 | week: 3600000 * 24 * 7, |
22 | month: 3600000 * 24 * 30 | 22 | month: 3600000 * 24 * 30 |
23 | } | 23 | } |
24 | |||
24 | export function parseDuration (duration: number | string): number { | 25 | export function parseDuration (duration: number | string): number { |
25 | if (typeof duration === 'number') return duration | 26 | if (typeof duration === 'number') return duration |
26 | 27 | ||
@@ -41,6 +42,53 @@ export function parseDuration (duration: number | string): number { | |||
41 | throw new Error('Duration could not be properly parsed') | 42 | throw new Error('Duration could not be properly parsed') |
42 | } | 43 | } |
43 | 44 | ||
45 | export function parseBytes (value: string | number): number { | ||
46 | if (typeof value === 'number') return value | ||
47 | |||
48 | const tgm = /^(\d+)\s*TB\s*(\d+)\s*GB\s*(\d+)\s*MB$/ | ||
49 | const tg = /^(\d+)\s*TB\s*(\d+)\s*GB$/ | ||
50 | const tm = /^(\d+)\s*TB\s*(\d+)\s*MB$/ | ||
51 | const gm = /^(\d+)\s*GB\s*(\d+)\s*MB$/ | ||
52 | const t = /^(\d+)\s*TB$/ | ||
53 | const g = /^(\d+)\s*GB$/ | ||
54 | const m = /^(\d+)\s*MB$/ | ||
55 | const b = /^(\d+)\s*B$/ | ||
56 | let match | ||
57 | |||
58 | if (value.match(tgm)) { | ||
59 | match = value.match(tgm) | ||
60 | return parseInt(match[1], 10) * 1024 * 1024 * 1024 * 1024 | ||
61 | + parseInt(match[2], 10) * 1024 * 1024 * 1024 | ||
62 | + parseInt(match[3], 10) * 1024 * 1024 | ||
63 | } else if (value.match(tg)) { | ||
64 | match = value.match(tg) | ||
65 | return parseInt(match[1], 10) * 1024 * 1024 * 1024 * 1024 | ||
66 | + parseInt(match[2], 10) * 1024 * 1024 * 1024 | ||
67 | } else if (value.match(tm)) { | ||
68 | match = value.match(tm) | ||
69 | return parseInt(match[1], 10) * 1024 * 1024 * 1024 * 1024 | ||
70 | + parseInt(match[2], 10) * 1024 * 1024 | ||
71 | } else if (value.match(gm)) { | ||
72 | match = value.match(gm) | ||
73 | return parseInt(match[1], 10) * 1024 * 1024 * 1024 | ||
74 | + parseInt(match[2], 10) * 1024 * 1024 | ||
75 | } else if (value.match(t)) { | ||
76 | match = value.match(t) | ||
77 | return parseInt(match[1], 10) * 1024 * 1024 * 1024 * 1024 | ||
78 | } else if (value.match(g)) { | ||
79 | match = value.match(g) | ||
80 | return parseInt(match[1], 10) * 1024 * 1024 * 1024 | ||
81 | } else if (value.match(m)) { | ||
82 | match = value.match(m) | ||
83 | return parseInt(match[1], 10) * 1024 * 1024 | ||
84 | } else if (value.match(b)) { | ||
85 | match = value.match(b) | ||
86 | return parseInt(match[1], 10) * 1024 | ||
87 | } else { | ||
88 | return parseInt(value, 10) | ||
89 | } | ||
90 | } | ||
91 | |||
44 | function sanitizeUrl (url: string) { | 92 | function sanitizeUrl (url: string) { |
45 | const urlObject = new URL(url) | 93 | const urlObject = new URL(url) |
46 | 94 | ||