diff options
author | Chocobozzz <me@florianbigard.com> | 2022-06-14 08:47:26 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2022-06-14 08:47:26 +0200 |
commit | c371016de26b3a08860ba638c7b0494978b6cde7 (patch) | |
tree | 8d3b6333a1c31ae28c837ba7f4b335a08ca83d75 | |
parent | 65ba19ace9ee95ac29394daf7326fffe33ae906d (diff) | |
download | PeerTube-c371016de26b3a08860ba638c7b0494978b6cde7.tar.gz PeerTube-c371016de26b3a08860ba638c7b0494978b6cde7.tar.zst PeerTube-c371016de26b3a08860ba638c7b0494978b6cde7.zip |
More robust duration parsing
-rw-r--r-- | server/helpers/core-utils.ts | 37 | ||||
-rw-r--r-- | server/tests/helpers/core-utils.ts | 65 |
2 files changed, 68 insertions, 34 deletions
diff --git a/server/helpers/core-utils.ts b/server/helpers/core-utils.ts index 0ec45eb2e..6ebe8e2ac 100644 --- a/server/helpers/core-utils.ts +++ b/server/helpers/core-utils.ts | |||
@@ -56,6 +56,7 @@ const timeTable = { | |||
56 | export function parseDurationToMs (duration: number | string): number { | 56 | export function parseDurationToMs (duration: number | string): number { |
57 | if (duration === null) return null | 57 | if (duration === null) return null |
58 | if (typeof duration === 'number') return duration | 58 | if (typeof duration === 'number') return duration |
59 | if (!isNaN(+duration)) return +duration | ||
59 | 60 | ||
60 | if (typeof duration === 'string') { | 61 | if (typeof duration === 'string') { |
61 | const split = duration.match(/^([\d.,]+)\s?(\w+)$/) | 62 | const split = duration.match(/^([\d.,]+)\s?(\w+)$/) |
@@ -76,6 +77,7 @@ export function parseDurationToMs (duration: number | string): number { | |||
76 | 77 | ||
77 | export function parseBytes (value: string | number): number { | 78 | export function parseBytes (value: string | number): number { |
78 | if (typeof value === 'number') return value | 79 | if (typeof value === 'number') return value |
80 | if (!isNaN(+value)) return +value | ||
79 | 81 | ||
80 | const tgm = /^(\d+)\s*TB\s*(\d+)\s*GB\s*(\d+)\s*MB$/ | 82 | const tgm = /^(\d+)\s*TB\s*(\d+)\s*GB\s*(\d+)\s*MB$/ |
81 | const tg = /^(\d+)\s*TB\s*(\d+)\s*GB$/ | 83 | const tg = /^(\d+)\s*TB\s*(\d+)\s*GB$/ |
@@ -85,40 +87,55 @@ export function parseBytes (value: string | number): number { | |||
85 | const g = /^(\d+)\s*GB$/ | 87 | const g = /^(\d+)\s*GB$/ |
86 | const m = /^(\d+)\s*MB$/ | 88 | const m = /^(\d+)\s*MB$/ |
87 | const b = /^(\d+)\s*B$/ | 89 | const b = /^(\d+)\s*B$/ |
88 | let match | 90 | |
91 | let match: RegExpMatchArray | ||
89 | 92 | ||
90 | if (value.match(tgm)) { | 93 | if (value.match(tgm)) { |
91 | match = value.match(tgm) | 94 | match = value.match(tgm) |
92 | return parseInt(match[1], 10) * 1024 * 1024 * 1024 * 1024 + | 95 | return parseInt(match[1], 10) * 1024 * 1024 * 1024 * 1024 + |
93 | parseInt(match[2], 10) * 1024 * 1024 * 1024 + | 96 | parseInt(match[2], 10) * 1024 * 1024 * 1024 + |
94 | parseInt(match[3], 10) * 1024 * 1024 | 97 | parseInt(match[3], 10) * 1024 * 1024 |
95 | } else if (value.match(tg)) { | 98 | } |
99 | |||
100 | if (value.match(tg)) { | ||
96 | match = value.match(tg) | 101 | match = value.match(tg) |
97 | return parseInt(match[1], 10) * 1024 * 1024 * 1024 * 1024 + | 102 | return parseInt(match[1], 10) * 1024 * 1024 * 1024 * 1024 + |
98 | parseInt(match[2], 10) * 1024 * 1024 * 1024 | 103 | parseInt(match[2], 10) * 1024 * 1024 * 1024 |
99 | } else if (value.match(tm)) { | 104 | } |
105 | |||
106 | if (value.match(tm)) { | ||
100 | match = value.match(tm) | 107 | match = value.match(tm) |
101 | return parseInt(match[1], 10) * 1024 * 1024 * 1024 * 1024 + | 108 | return parseInt(match[1], 10) * 1024 * 1024 * 1024 * 1024 + |
102 | parseInt(match[2], 10) * 1024 * 1024 | 109 | parseInt(match[2], 10) * 1024 * 1024 |
103 | } else if (value.match(gm)) { | 110 | } |
111 | |||
112 | if (value.match(gm)) { | ||
104 | match = value.match(gm) | 113 | match = value.match(gm) |
105 | return parseInt(match[1], 10) * 1024 * 1024 * 1024 + | 114 | return parseInt(match[1], 10) * 1024 * 1024 * 1024 + |
106 | parseInt(match[2], 10) * 1024 * 1024 | 115 | parseInt(match[2], 10) * 1024 * 1024 |
107 | } else if (value.match(t)) { | 116 | } |
117 | |||
118 | if (value.match(t)) { | ||
108 | match = value.match(t) | 119 | match = value.match(t) |
109 | return parseInt(match[1], 10) * 1024 * 1024 * 1024 * 1024 | 120 | return parseInt(match[1], 10) * 1024 * 1024 * 1024 * 1024 |
110 | } else if (value.match(g)) { | 121 | } |
122 | |||
123 | if (value.match(g)) { | ||
111 | match = value.match(g) | 124 | match = value.match(g) |
112 | return parseInt(match[1], 10) * 1024 * 1024 * 1024 | 125 | return parseInt(match[1], 10) * 1024 * 1024 * 1024 |
113 | } else if (value.match(m)) { | 126 | } |
127 | |||
128 | if (value.match(m)) { | ||
114 | match = value.match(m) | 129 | match = value.match(m) |
115 | return parseInt(match[1], 10) * 1024 * 1024 | 130 | return parseInt(match[1], 10) * 1024 * 1024 |
116 | } else if (value.match(b)) { | 131 | } |
132 | |||
133 | if (value.match(b)) { | ||
117 | match = value.match(b) | 134 | match = value.match(b) |
118 | return parseInt(match[1], 10) * 1024 | 135 | return parseInt(match[1], 10) * 1024 |
119 | } else { | ||
120 | return parseInt(value, 10) | ||
121 | } | 136 | } |
137 | |||
138 | return parseInt(value, 10) | ||
122 | } | 139 | } |
123 | 140 | ||
124 | // --------------------------------------------------------------------------- | 141 | // --------------------------------------------------------------------------- |
diff --git a/server/tests/helpers/core-utils.ts b/server/tests/helpers/core-utils.ts index fa0a71341..e3e5eb45c 100644 --- a/server/tests/helpers/core-utils.ts +++ b/server/tests/helpers/core-utils.ts | |||
@@ -6,47 +6,64 @@ import { snakeCase } from 'lodash' | |||
6 | import validator from 'validator' | 6 | import validator from 'validator' |
7 | import { getAverageBitrate, getMaxBitrate } from '@shared/core-utils' | 7 | import { getAverageBitrate, getMaxBitrate } from '@shared/core-utils' |
8 | import { VideoResolution } from '@shared/models' | 8 | import { VideoResolution } from '@shared/models' |
9 | import { objectConverter, parseBytes } from '../../helpers/core-utils' | 9 | import { objectConverter, parseBytes, parseDurationToMs } from '../../helpers/core-utils' |
10 | 10 | ||
11 | const expect = chai.expect | 11 | const expect = chai.expect |
12 | 12 | ||
13 | describe('Parse Bytes', function () { | 13 | describe('Parse Bytes', function () { |
14 | 14 | ||
15 | it('Should pass when given valid value', async function () { | 15 | it('Should pass on valid value', async function () { |
16 | // just return it | 16 | // just return it |
17 | expect(parseBytes(1024)).to.be.eq(1024) | 17 | expect(parseBytes(-1024)).to.equal(-1024) |
18 | expect(parseBytes(1048576)).to.be.eq(1048576) | 18 | expect(parseBytes(1024)).to.equal(1024) |
19 | expect(parseBytes('1024')).to.be.eq(1024) | 19 | expect(parseBytes(1048576)).to.equal(1048576) |
20 | expect(parseBytes('1048576')).to.be.eq(1048576) | 20 | expect(parseBytes('1024')).to.equal(1024) |
21 | expect(parseBytes('1048576')).to.equal(1048576) | ||
21 | 22 | ||
22 | // sizes | 23 | // sizes |
23 | expect(parseBytes('1B')).to.be.eq(1024) | 24 | expect(parseBytes('1B')).to.equal(1024) |
24 | expect(parseBytes('1MB')).to.be.eq(1048576) | 25 | expect(parseBytes('1MB')).to.equal(1048576) |
25 | expect(parseBytes('1GB')).to.be.eq(1073741824) | 26 | expect(parseBytes('1GB')).to.equal(1073741824) |
26 | expect(parseBytes('1TB')).to.be.eq(1099511627776) | 27 | expect(parseBytes('1TB')).to.equal(1099511627776) |
27 | 28 | ||
28 | expect(parseBytes('5GB')).to.be.eq(5368709120) | 29 | expect(parseBytes('5GB')).to.equal(5368709120) |
29 | expect(parseBytes('5TB')).to.be.eq(5497558138880) | 30 | expect(parseBytes('5TB')).to.equal(5497558138880) |
30 | 31 | ||
31 | expect(parseBytes('1024B')).to.be.eq(1048576) | 32 | expect(parseBytes('1024B')).to.equal(1048576) |
32 | expect(parseBytes('1024MB')).to.be.eq(1073741824) | 33 | expect(parseBytes('1024MB')).to.equal(1073741824) |
33 | expect(parseBytes('1024GB')).to.be.eq(1099511627776) | 34 | expect(parseBytes('1024GB')).to.equal(1099511627776) |
34 | expect(parseBytes('1024TB')).to.be.eq(1125899906842624) | 35 | expect(parseBytes('1024TB')).to.equal(1125899906842624) |
35 | 36 | ||
36 | // with whitespace | 37 | // with whitespace |
37 | expect(parseBytes('1 GB')).to.be.eq(1073741824) | 38 | expect(parseBytes('1 GB')).to.equal(1073741824) |
38 | expect(parseBytes('1\tGB')).to.be.eq(1073741824) | 39 | expect(parseBytes('1\tGB')).to.equal(1073741824) |
39 | 40 | ||
40 | // sum value | 41 | // sum value |
41 | expect(parseBytes('1TB 1024MB')).to.be.eq(1100585369600) | 42 | expect(parseBytes('1TB 1024MB')).to.equal(1100585369600) |
42 | expect(parseBytes('4GB 1024MB')).to.be.eq(5368709120) | 43 | expect(parseBytes('4GB 1024MB')).to.equal(5368709120) |
43 | expect(parseBytes('4TB 1024GB')).to.be.eq(5497558138880) | 44 | expect(parseBytes('4TB 1024GB')).to.equal(5497558138880) |
44 | expect(parseBytes('4TB 1024GB 0MB')).to.be.eq(5497558138880) | 45 | expect(parseBytes('4TB 1024GB 0MB')).to.equal(5497558138880) |
45 | expect(parseBytes('1024TB 1024GB 1024MB')).to.be.eq(1127000492212224) | 46 | expect(parseBytes('1024TB 1024GB 1024MB')).to.equal(1127000492212224) |
47 | }) | ||
48 | |||
49 | it('Should be invalid when given invalid value', async function () { | ||
50 | expect(parseBytes('6GB 1GB')).to.equal(6) | ||
51 | }) | ||
52 | }) | ||
53 | |||
54 | describe('Parse duration', function () { | ||
55 | |||
56 | it('Should pass when given valid value', async function () { | ||
57 | expect(parseDurationToMs(35)).to.equal(35) | ||
58 | expect(parseDurationToMs(-35)).to.equal(-35) | ||
59 | expect(parseDurationToMs('35 seconds')).to.equal(35 * 1000) | ||
60 | expect(parseDurationToMs('1 minute')).to.equal(60 * 1000) | ||
61 | expect(parseDurationToMs('1 hour')).to.equal(3600 * 1000) | ||
62 | expect(parseDurationToMs('35 hours')).to.equal(3600 * 35 * 1000) | ||
46 | }) | 63 | }) |
47 | 64 | ||
48 | it('Should be invalid when given invalid value', async function () { | 65 | it('Should be invalid when given invalid value', async function () { |
49 | expect(parseBytes('6GB 1GB')).to.be.eq(6) | 66 | expect(parseBytes('35m 5s')).to.equal(35) |
50 | }) | 67 | }) |
51 | }) | 68 | }) |
52 | 69 | ||