More robust duration parsing

pull/5067/head
Chocobozzz 2022-06-14 08:47:26 +02:00
parent 65ba19ace9
commit c371016de2
No known key found for this signature in database
GPG Key ID: 583A612D890159BE
2 changed files with 68 additions and 34 deletions

View File

@ -56,6 +56,7 @@ const timeTable = {
export function parseDurationToMs (duration: number | string): number { export function parseDurationToMs (duration: number | string): number {
if (duration === null) return null if (duration === null) return null
if (typeof duration === 'number') return duration if (typeof duration === 'number') return duration
if (!isNaN(+duration)) return +duration
if (typeof duration === 'string') { if (typeof duration === 'string') {
const split = duration.match(/^([\d.,]+)\s?(\w+)$/) const split = duration.match(/^([\d.,]+)\s?(\w+)$/)
@ -76,6 +77,7 @@ export function parseDurationToMs (duration: number | string): number {
export function parseBytes (value: string | number): number { export function parseBytes (value: string | number): number {
if (typeof value === 'number') return value if (typeof value === 'number') return value
if (!isNaN(+value)) return +value
const tgm = /^(\d+)\s*TB\s*(\d+)\s*GB\s*(\d+)\s*MB$/ const tgm = /^(\d+)\s*TB\s*(\d+)\s*GB\s*(\d+)\s*MB$/
const tg = /^(\d+)\s*TB\s*(\d+)\s*GB$/ const tg = /^(\d+)\s*TB\s*(\d+)\s*GB$/
@ -85,40 +87,55 @@ export function parseBytes (value: string | number): number {
const g = /^(\d+)\s*GB$/ const g = /^(\d+)\s*GB$/
const m = /^(\d+)\s*MB$/ const m = /^(\d+)\s*MB$/
const b = /^(\d+)\s*B$/ const b = /^(\d+)\s*B$/
let match
let match: RegExpMatchArray
if (value.match(tgm)) { if (value.match(tgm)) {
match = value.match(tgm) match = value.match(tgm)
return parseInt(match[1], 10) * 1024 * 1024 * 1024 * 1024 + return parseInt(match[1], 10) * 1024 * 1024 * 1024 * 1024 +
parseInt(match[2], 10) * 1024 * 1024 * 1024 + parseInt(match[2], 10) * 1024 * 1024 * 1024 +
parseInt(match[3], 10) * 1024 * 1024 parseInt(match[3], 10) * 1024 * 1024
} else if (value.match(tg)) { }
if (value.match(tg)) {
match = value.match(tg) match = value.match(tg)
return parseInt(match[1], 10) * 1024 * 1024 * 1024 * 1024 + return parseInt(match[1], 10) * 1024 * 1024 * 1024 * 1024 +
parseInt(match[2], 10) * 1024 * 1024 * 1024 parseInt(match[2], 10) * 1024 * 1024 * 1024
} else if (value.match(tm)) { }
if (value.match(tm)) {
match = value.match(tm) match = value.match(tm)
return parseInt(match[1], 10) * 1024 * 1024 * 1024 * 1024 + return parseInt(match[1], 10) * 1024 * 1024 * 1024 * 1024 +
parseInt(match[2], 10) * 1024 * 1024 parseInt(match[2], 10) * 1024 * 1024
} else if (value.match(gm)) { }
if (value.match(gm)) {
match = value.match(gm) match = value.match(gm)
return parseInt(match[1], 10) * 1024 * 1024 * 1024 + return parseInt(match[1], 10) * 1024 * 1024 * 1024 +
parseInt(match[2], 10) * 1024 * 1024 parseInt(match[2], 10) * 1024 * 1024
} else if (value.match(t)) { }
if (value.match(t)) {
match = value.match(t) match = value.match(t)
return parseInt(match[1], 10) * 1024 * 1024 * 1024 * 1024 return parseInt(match[1], 10) * 1024 * 1024 * 1024 * 1024
} else if (value.match(g)) { }
if (value.match(g)) {
match = value.match(g) match = value.match(g)
return parseInt(match[1], 10) * 1024 * 1024 * 1024 return parseInt(match[1], 10) * 1024 * 1024 * 1024
} else if (value.match(m)) { }
if (value.match(m)) {
match = value.match(m) match = value.match(m)
return parseInt(match[1], 10) * 1024 * 1024 return parseInt(match[1], 10) * 1024 * 1024
} else if (value.match(b)) { }
if (value.match(b)) {
match = value.match(b) match = value.match(b)
return parseInt(match[1], 10) * 1024 return parseInt(match[1], 10) * 1024
} else {
return parseInt(value, 10)
} }
return parseInt(value, 10)
} }
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------

View File

@ -6,47 +6,64 @@ import { snakeCase } from 'lodash'
import validator from 'validator' import validator from 'validator'
import { getAverageBitrate, getMaxBitrate } from '@shared/core-utils' import { getAverageBitrate, getMaxBitrate } from '@shared/core-utils'
import { VideoResolution } from '@shared/models' import { VideoResolution } from '@shared/models'
import { objectConverter, parseBytes } from '../../helpers/core-utils' import { objectConverter, parseBytes, parseDurationToMs } from '../../helpers/core-utils'
const expect = chai.expect const expect = chai.expect
describe('Parse Bytes', function () { describe('Parse Bytes', function () {
it('Should pass when given valid value', async function () { it('Should pass on valid value', async function () {
// just return it // just return it
expect(parseBytes(1024)).to.be.eq(1024) expect(parseBytes(-1024)).to.equal(-1024)
expect(parseBytes(1048576)).to.be.eq(1048576) expect(parseBytes(1024)).to.equal(1024)
expect(parseBytes('1024')).to.be.eq(1024) expect(parseBytes(1048576)).to.equal(1048576)
expect(parseBytes('1048576')).to.be.eq(1048576) expect(parseBytes('1024')).to.equal(1024)
expect(parseBytes('1048576')).to.equal(1048576)
// sizes // sizes
expect(parseBytes('1B')).to.be.eq(1024) expect(parseBytes('1B')).to.equal(1024)
expect(parseBytes('1MB')).to.be.eq(1048576) expect(parseBytes('1MB')).to.equal(1048576)
expect(parseBytes('1GB')).to.be.eq(1073741824) expect(parseBytes('1GB')).to.equal(1073741824)
expect(parseBytes('1TB')).to.be.eq(1099511627776) expect(parseBytes('1TB')).to.equal(1099511627776)
expect(parseBytes('5GB')).to.be.eq(5368709120) expect(parseBytes('5GB')).to.equal(5368709120)
expect(parseBytes('5TB')).to.be.eq(5497558138880) expect(parseBytes('5TB')).to.equal(5497558138880)
expect(parseBytes('1024B')).to.be.eq(1048576) expect(parseBytes('1024B')).to.equal(1048576)
expect(parseBytes('1024MB')).to.be.eq(1073741824) expect(parseBytes('1024MB')).to.equal(1073741824)
expect(parseBytes('1024GB')).to.be.eq(1099511627776) expect(parseBytes('1024GB')).to.equal(1099511627776)
expect(parseBytes('1024TB')).to.be.eq(1125899906842624) expect(parseBytes('1024TB')).to.equal(1125899906842624)
// with whitespace // with whitespace
expect(parseBytes('1 GB')).to.be.eq(1073741824) expect(parseBytes('1 GB')).to.equal(1073741824)
expect(parseBytes('1\tGB')).to.be.eq(1073741824) expect(parseBytes('1\tGB')).to.equal(1073741824)
// sum value // sum value
expect(parseBytes('1TB 1024MB')).to.be.eq(1100585369600) expect(parseBytes('1TB 1024MB')).to.equal(1100585369600)
expect(parseBytes('4GB 1024MB')).to.be.eq(5368709120) expect(parseBytes('4GB 1024MB')).to.equal(5368709120)
expect(parseBytes('4TB 1024GB')).to.be.eq(5497558138880) expect(parseBytes('4TB 1024GB')).to.equal(5497558138880)
expect(parseBytes('4TB 1024GB 0MB')).to.be.eq(5497558138880) expect(parseBytes('4TB 1024GB 0MB')).to.equal(5497558138880)
expect(parseBytes('1024TB 1024GB 1024MB')).to.be.eq(1127000492212224) expect(parseBytes('1024TB 1024GB 1024MB')).to.equal(1127000492212224)
}) })
it('Should be invalid when given invalid value', async function () { it('Should be invalid when given invalid value', async function () {
expect(parseBytes('6GB 1GB')).to.be.eq(6) expect(parseBytes('6GB 1GB')).to.equal(6)
})
})
describe('Parse duration', function () {
it('Should pass when given valid value', async function () {
expect(parseDurationToMs(35)).to.equal(35)
expect(parseDurationToMs(-35)).to.equal(-35)
expect(parseDurationToMs('35 seconds')).to.equal(35 * 1000)
expect(parseDurationToMs('1 minute')).to.equal(60 * 1000)
expect(parseDurationToMs('1 hour')).to.equal(3600 * 1000)
expect(parseDurationToMs('35 hours')).to.equal(3600 * 35 * 1000)
})
it('Should be invalid when given invalid value', async function () {
expect(parseBytes('35m 5s')).to.equal(35)
}) })
}) })