From 8e1541ea56bbe4046767e59f42d7631091531ef1 Mon Sep 17 00:00:00 2001 From: karlmanait Date: Wed, 23 Nov 2022 12:36:49 +0800 Subject: [PATCH] feat: add configuration for default upload format (#232) * feat: add configuration for default upload format * fix: change default back to original --- .env.local.example | 3 ++- src/lib/config/Config.ts | 1 + src/lib/config/readConfig.ts | 1 + src/lib/config/validateConfig.ts | 2 ++ src/pages/api/upload.ts | 2 +- 5 files changed, 7 insertions(+), 2 deletions(-) diff --git a/.env.local.example b/.env.local.example index 5cce1c6..c12d8f9 100644 --- a/.env.local.example +++ b/.env.local.example @@ -34,6 +34,7 @@ DATASOURCE_SWIFT_PASSWORD=password DATASOURCE_SWIFT_PROJECT_ID=project_id DATASOURCE_SWIFT_DOMAIN_ID=domain_id +UPLOADER_DEFAULT_FORMAT=RANDOM UPLOADER_ROUTE=/u UPLOADER_LENGTH=6 UPLOADER_ADMIN_LIMIT=104900000 @@ -44,4 +45,4 @@ URLS_ROUTE=/go URLS_LENGTH=6 RATELIMIT_USER = 5 -RATELIMIT_ADMIN = 3 \ No newline at end of file +RATELIMIT_ADMIN = 3 diff --git a/src/lib/config/Config.ts b/src/lib/config/Config.ts index e0ee915..6d2fe69 100644 --- a/src/lib/config/Config.ts +++ b/src/lib/config/Config.ts @@ -43,6 +43,7 @@ export interface ConfigSwiftDatasource { } export interface ConfigUploader { + default_format: string; route: string; length: number; admin_limit: number; diff --git a/src/lib/config/readConfig.ts b/src/lib/config/readConfig.ts index a86a520..2bc5ee6 100644 --- a/src/lib/config/readConfig.ts +++ b/src/lib/config/readConfig.ts @@ -85,6 +85,7 @@ export default function readConfig() { map('DATASOURCE_SWIFT_DOMAIN_ID', 'string', 'datasource.swift.domain_id'), map('DATASOURCE_SWIFT_REGION_ID', 'string', 'datasource.swift.region_id'), + map('UPLOADER_DEFAULT_FORMAT', 'string', 'uploader.default_format'), map('UPLOADER_ROUTE', 'string', 'uploader.route'), map('UPLOADER_LENGTH', 'number', 'uploader.length'), map('UPLOADER_ADMIN_LIMIT', 'human-to-byte', 'uploader.admin_limit'), diff --git a/src/lib/config/validateConfig.ts b/src/lib/config/validateConfig.ts index 85ac1b8..2d6b6e5 100644 --- a/src/lib/config/validateConfig.ts +++ b/src/lib/config/validateConfig.ts @@ -77,6 +77,7 @@ const validator = s.object({ }), uploader: s .object({ + default_format: s.string.default('RANDOM'), route: s.string.default('/u'), embed_route: s.string.default('/a'), length: s.number.default(6), @@ -86,6 +87,7 @@ const validator = s.object({ format_date: s.string.default('YYYY-MM-DD_HH:mm:ss'), }) .default({ + default_format: 'RANDOM', route: '/u', embed_route: '/a', length: 6, diff --git a/src/pages/api/upload.ts b/src/pages/api/upload.ts index eeb2867..b57486b 100644 --- a/src/pages/api/upload.ts +++ b/src/pages/api/upload.ts @@ -49,7 +49,7 @@ async function handler(req: NextApiReq, res: NextApiRes) { } } - const rawFormat = ((req.headers.format || '') as string).toUpperCase() || 'RANDOM'; + const rawFormat = ((req.headers.format || '') as string).toUpperCase() || zconfig.uploader.default_format; const format: ImageFormat = Object.keys(ImageFormat).includes(rawFormat) && ImageFormat[rawFormat]; const imageCompressionPercent = req.headers['image-compression-percent']