feat: default expiration/ttl (#237)

This commit is contained in:
diced 2022-12-01 09:42:16 -08:00
parent cb123cb575
commit 78e884e97e
No known key found for this signature in database
GPG key ID: 370BD1BA142842D1
4 changed files with 10 additions and 0 deletions

View file

@ -46,6 +46,7 @@ export interface ConfigUploader {
user_limit: number; user_limit: number;
disabled_extensions: string[]; disabled_extensions: string[];
format_date: string; format_date: string;
default_expiration: string;
} }
export interface ConfigUrls { export interface ConfigUrls {

View file

@ -3,6 +3,7 @@ import { expand } from 'dotenv-expand';
import { existsSync, readFileSync } from 'fs'; import { existsSync, readFileSync } from 'fs';
import Logger from '../logger'; import Logger from '../logger';
import { humanToBytes } from '../utils/bytes'; import { humanToBytes } from '../utils/bytes';
import { parseExpiry } from '../utils/client';
export type ValueType = 'string' | 'number' | 'boolean' | 'array' | 'json-array' | 'human-to-byte'; 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_USER_LIMIT', 'human-to-byte', 'uploader.user_limit'),
map('UPLOADER_DISABLED_EXTENSIONS', 'array', 'uploader.disabled_extensions'), map('UPLOADER_DISABLED_EXTENSIONS', 'array', 'uploader.disabled_extensions'),
map('UPLOADER_FORMAT_DATE', 'string', 'uploader.format_date'), map('UPLOADER_FORMAT_DATE', 'string', 'uploader.format_date'),
map('UPLOADER_DEFAULT_EXPIRATION', 'string', 'uploader.default_expiration'),
map('URLS_ROUTE', 'string', 'urls.route'), map('URLS_ROUTE', 'string', 'urls.route'),
map('URLS_LENGTH', 'number', 'urls.length'), map('URLS_LENGTH', 'number', 'urls.length'),

View file

@ -78,6 +78,7 @@ const validator = s.object({
user_limit: s.number.default(humanToBytes('100MB')), user_limit: s.number.default(humanToBytes('100MB')),
disabled_extensions: s.string.array.default([]), disabled_extensions: s.string.array.default([]),
format_date: s.string.default('YYYY-MM-DD_HH:mm:ss'), format_date: s.string.default('YYYY-MM-DD_HH:mm:ss'),
default_expiration: s.string.optional.default(null),
}) })
.default({ .default({
default_format: 'RANDOM', default_format: 'RANDOM',
@ -88,6 +89,7 @@ const validator = s.object({
user_limit: humanToBytes('100MB'), user_limit: humanToBytes('100MB'),
disabled_extensions: [], disabled_extensions: [],
format_date: 'YYYY-MM-DD_HH:mm:ss', format_date: 'YYYY-MM-DD_HH:mm:ss',
default_expiration: null,
}), }),
urls: s urls: s
.object({ .object({

View file

@ -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 rawFormat = ((req.headers.format || '') as string).toUpperCase() || zconfig.uploader.default_format;
const format: ImageFormat = Object.keys(ImageFormat).includes(rawFormat) && ImageFormat[rawFormat]; const format: ImageFormat = Object.keys(ImageFormat).includes(rawFormat) && ImageFormat[rawFormat];