From e2443d15130dd5a8c5af95740ced47fca9125d3c Mon Sep 17 00:00:00 2001 From: boyska Date: Sat, 7 Dec 2024 20:18:07 +0100 Subject: [PATCH] decouple format from mimetype --- caricari/public.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/caricari/public.py b/caricari/public.py index db115e4..022b1a6 100644 --- a/caricari/public.py +++ b/caricari/public.py @@ -29,11 +29,17 @@ def get_format(path: str, request: Request) -> str: # XXX: add some logic involving Accept, user-agent, etc. return path.split(".")[-1] +def get_mimes_from_format(fmt: str) -> list[str]: + if fmt == 'mp3': + return ['audio/mpeg'] + return [] + @app.get("/dl/{path:path}") async def get_file(request: Request, path: str, fmt: str = "auto"): if fmt == "auto": fmt = get_format(path, request) + mimes = get_mimes_from_format(fmt) with session_pool() as conn: original = ( conn.query(database.Original) @@ -57,7 +63,8 @@ async def get_file(request: Request, path: str, fmt: str = "auto"): return 404 # look for the "best" archived version - right_format = [arc for arc in original.archived if arc.format == fmt] + right_format = [arc for arc in original.archived if arc.format in mimes] if right_format: return RedirectResponse(right_format[0].link) + # XXX: if we have at least one upload from archive, let's assume there is an mp3 available return RedirectResponse(original.archived[0].link)