decouple format from mimetype

This commit is contained in:
boyska 2024-12-07 20:18:07 +01:00
parent abe521927e
commit e2443d1513

View file

@ -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)