fix: config byte parsing

resolves #42
This commit is contained in:
Sylver 2024-05-06 10:57:53 +08:00
parent e56d700d2e
commit db4728f1f7
2 changed files with 32 additions and 4 deletions

View File

@ -1,5 +1,4 @@
import { loadConfig } from '@ryanke/venera';
import bytes from 'bytes';
import c from 'chalk';
import { randomBytes } from 'crypto';
import dedent from 'dedent';
@ -9,6 +8,7 @@ import z, { any, array, boolean, number, record, strictObject, string, union } f
import { fromZodError } from 'zod-validation-error';
import { expandMime } from './helpers/expand-mime.js';
import { HostService } from './modules/host/host.service.js';
import { parseBytes } from './helpers/parse-bytes.js';
export type MicroHost = ReturnType<typeof enhanceHost>;
@ -16,7 +16,7 @@ const schema = strictObject({
databaseUrl: string().startsWith('postgresql://'),
secret: string().min(6),
inquiries: string().email(),
uploadLimit: string().transform(bytes.parse),
uploadLimit: string().transform(parseBytes),
maxPasteLength: number().default(500000),
allowTypes: z
.union([array(string()), string()])
@ -25,7 +25,7 @@ const schema = strictObject({
storagePath: string(),
restrictFilesToHost: boolean().default(true),
purge: strictObject({
overLimit: string().transform(bytes.parse),
overLimit: string().transform(parseBytes),
afterTime: string().transform(ms),
}).optional(),
email: strictObject({
@ -36,7 +36,7 @@ const schema = strictObject({
strictObject({
from: union([array(string()), string()]).transform((value) => new Set(expandMime(value))),
to: string(),
minSize: string().transform(bytes.parse).optional(),
minSize: string().transform(parseBytes).optional(),
}),
).optional(),
hosts: array(

View File

@ -0,0 +1,28 @@
const UNITS = new Map<string, number>([
['B', 1],
['KB', 1024],
['K', 1024],
['MB', 1024 * 1024],
['M', 1024 * 1024],
['GB', 1024 * 1024 * 1024],
['G', 1024 * 1024 * 1024],
]);
const PATTERN = new RegExp(`(?<value>[0-9]+(?:\\.[0-9]+)?)\\s*(?<unit>${[...UNITS.keys()].join('|')})`, 'i')
export const parseBytes = (input:string) => {
const match = input.match(PATTERN)
if (!match) {
throw new Error('Invalid byte format')
}
const value = +match.groups!.value
const unit = match.groups!.unit.toUpperCase()
const unitMultiplier = UNITS.get(unit)
if (!unitMultiplier) {
throw new Error('Invalid byte format')
}
return value * unitMultiplier
}