From 78e884e97ef054b130482dd44334fe4e63519dff Mon Sep 17 00:00:00 2001 From: diced Date: Thu, 1 Dec 2022 09:42:16 -0800 Subject: [PATCH] feat: default expiration/ttl (#237) --- src/lib/config/Config.ts | 1 + src/lib/config/readConfig.ts | 2 ++ src/lib/config/validateConfig.ts | 2 ++ src/pages/api/upload.ts | 5 +++++ 4 files changed, 10 insertions(+) diff --git a/src/lib/config/Config.ts b/src/lib/config/Config.ts index 22cce66..2f92be8 100644 --- a/src/lib/config/Config.ts +++ b/src/lib/config/Config.ts @@ -46,6 +46,7 @@ export interface ConfigUploader { user_limit: number; disabled_extensions: string[]; format_date: string; + default_expiration: string; } export interface ConfigUrls { diff --git a/src/lib/config/readConfig.ts b/src/lib/config/readConfig.ts index c6b01dc..18f7d04 100644 --- a/src/lib/config/readConfig.ts +++ b/src/lib/config/readConfig.ts @@ -3,6 +3,7 @@ import { expand } from 'dotenv-expand'; import { existsSync, readFileSync } from 'fs'; import Logger from '../logger'; import { humanToBytes } from '../utils/bytes'; +import { parseExpiry } from '../utils/client'; export type ValueType = 'string' | 'number' | 'boolean' | 'array' | 'json-array' | 'human-to-byte'; @@ -88,6 +89,7 @@ export default function readConfig() { map('UPLOADER_USER_LIMIT', 'human-to-byte', 'uploader.user_limit'), map('UPLOADER_DISABLED_EXTENSIONS', 'array', 'uploader.disabled_extensions'), map('UPLOADER_FORMAT_DATE', 'string', 'uploader.format_date'), + map('UPLOADER_DEFAULT_EXPIRATION', 'string', 'uploader.default_expiration'), map('URLS_ROUTE', 'string', 'urls.route'), map('URLS_LENGTH', 'number', 'urls.length'), diff --git a/src/lib/config/validateConfig.ts b/src/lib/config/validateConfig.ts index 061c5c4..b7f319d 100644 --- a/src/lib/config/validateConfig.ts +++ b/src/lib/config/validateConfig.ts @@ -78,6 +78,7 @@ const validator = s.object({ user_limit: s.number.default(humanToBytes('100MB')), disabled_extensions: s.string.array.default([]), format_date: s.string.default('YYYY-MM-DD_HH:mm:ss'), + default_expiration: s.string.optional.default(null), }) .default({ default_format: 'RANDOM', @@ -88,6 +89,7 @@ const validator = s.object({ user_limit: humanToBytes('100MB'), disabled_extensions: [], format_date: 'YYYY-MM-DD_HH:mm:ss', + default_expiration: null, }), urls: s .object({ diff --git a/src/pages/api/upload.ts b/src/pages/api/upload.ts index 3b6a6fe..97166e1 100644 --- a/src/pages/api/upload.ts +++ b/src/pages/api/upload.ts @@ -49,6 +49,11 @@ async function handler(req: NextApiReq, res: NextApiRes) { } } + if (zconfig.uploader.default_expiration) { + expiry = parseExpiry(zconfig.uploader.default_expiration); + if (!expiry) return res.badRequest('invalid date (UPLOADER_DEFAULT_EXPIRATION)'); + } + const rawFormat = ((req.headers.format || '') as string).toUpperCase() || zconfig.uploader.default_format; const format: ImageFormat = Object.keys(ImageFormat).includes(rawFormat) && ImageFormat[rawFormat];