aboutsummaryrefslogtreecommitdiffhomepage
path: root/server
diff options
context:
space:
mode:
Diffstat (limited to 'server')
-rw-r--r--server/helpers/core-utils.ts48
-rw-r--r--server/initializers/constants.ts6
-rw-r--r--server/tests/helpers/core-utils.ts48
-rw-r--r--server/tests/helpers/index.ts1
4 files changed, 100 insertions, 3 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
24export function parseDuration (duration: number | string): number { 25export 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
45export 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
44function sanitizeUrl (url: string) { 92function sanitizeUrl (url: string) {
45 const urlObject = new URL(url) 93 const urlObject = new URL(url)
46 94
diff --git a/server/initializers/constants.ts b/server/initializers/constants.ts
index a3e5f5dd2..e08fd75cd 100644
--- a/server/initializers/constants.ts
+++ b/server/initializers/constants.ts
@@ -5,7 +5,7 @@ import { ActivityPubActorType } from '../../shared/models/activitypub'
5import { FollowState } from '../../shared/models/actors' 5import { FollowState } from '../../shared/models/actors'
6import { VideoAbuseState, VideoImportState, VideoPrivacy, VideoTranscodingFPS } from '../../shared/models/videos' 6import { VideoAbuseState, VideoImportState, VideoPrivacy, VideoTranscodingFPS } from '../../shared/models/videos'
7// Do not use barrels, remain constants as independent as possible 7// Do not use barrels, remain constants as independent as possible
8import { buildPath, isTestInstance, parseDuration, root, sanitizeHost, sanitizeUrl } from '../helpers/core-utils' 8import { buildPath, isTestInstance, parseDuration, parseBytes, root, sanitizeHost, sanitizeUrl } from '../helpers/core-utils'
9import { NSFWPolicyType } from '../../shared/models/videos/nsfw-policy.type' 9import { NSFWPolicyType } from '../../shared/models/videos/nsfw-policy.type'
10import { invert } from 'lodash' 10import { invert } from 'lodash'
11import { CronRepeatOptions, EveryRepeatOptions } from 'bull' 11import { CronRepeatOptions, EveryRepeatOptions } from 'bull'
@@ -232,8 +232,8 @@ const CONFIG = {
232 } 232 }
233 }, 233 },
234 USER: { 234 USER: {
235 get VIDEO_QUOTA () { return config.get<number>('user.video_quota') }, 235 get VIDEO_QUOTA () { return parseBytes(config.get<number>('user.video_quota')) },
236 get VIDEO_QUOTA_DAILY () { return config.get<number>('user.video_quota_daily') } 236 get VIDEO_QUOTA_DAILY () { return parseBytes(config.get<number>('user.video_quota_daily')) }
237 }, 237 },
238 TRANSCODING: { 238 TRANSCODING: {
239 get ENABLED () { return config.get<boolean>('transcoding.enabled') }, 239 get ENABLED () { return config.get<boolean>('transcoding.enabled') },
diff --git a/server/tests/helpers/core-utils.ts b/server/tests/helpers/core-utils.ts
new file mode 100644
index 000000000..a6d829a9f
--- /dev/null
+++ b/server/tests/helpers/core-utils.ts
@@ -0,0 +1,48 @@
1/* tslint:disable:no-unused-expression */
2
3import * as chai from 'chai'
4import 'mocha'
5import {
6 parseBytes
7} from '../../helpers/core-utils'
8
9const expect = chai.expect
10
11describe('Parse Bytes', function () {
12 it('Should pass when given valid value', async function () {
13 // just return it
14 expect(parseBytes(1024)).to.be.eq(1024)
15 expect(parseBytes(1048576)).to.be.eq(1048576)
16 expect(parseBytes('1024')).to.be.eq(1024)
17 expect(parseBytes('1048576')).to.be.eq(1048576)
18
19 // sizes
20 expect(parseBytes('1B')).to.be.eq(1024)
21 expect(parseBytes('1MB')).to.be.eq(1048576)
22 expect(parseBytes('1GB')).to.be.eq(1073741824)
23 expect(parseBytes('1TB')).to.be.eq(1099511627776)
24
25 expect(parseBytes('5GB')).to.be.eq(5368709120)
26 expect(parseBytes('5TB')).to.be.eq(5497558138880)
27
28 expect(parseBytes('1024B')).to.be.eq(1048576)
29 expect(parseBytes('1024MB')).to.be.eq(1073741824)
30 expect(parseBytes('1024GB')).to.be.eq(1099511627776)
31 expect(parseBytes('1024TB')).to.be.eq(1125899906842624)
32
33 // with whitespace
34 expect(parseBytes('1 GB')).to.be.eq(1073741824)
35 expect(parseBytes('1\tGB')).to.be.eq(1073741824)
36
37 // sum value
38 expect(parseBytes('1TB 1024MB')).to.be.eq(1100585369600)
39 expect(parseBytes('4GB 1024MB')).to.be.eq(5368709120)
40 expect(parseBytes('4TB 1024GB')).to.be.eq(5497558138880)
41 expect(parseBytes('4TB 1024GB 0MB')).to.be.eq(5497558138880)
42 expect(parseBytes('1024TB 1024GB 1024MB')).to.be.eq(1127000492212224)
43 })
44
45 it('Should be invalid when given invalid value', async function () {
46 expect(parseBytes('6GB 1GB')).to.be.eq(6)
47 })
48})
diff --git a/server/tests/helpers/index.ts b/server/tests/helpers/index.ts
new file mode 100644
index 000000000..40c7dc70e
--- /dev/null
+++ b/server/tests/helpers/index.ts
@@ -0,0 +1 @@
import './core-utils'