handle ?format= (very basic)
This commit is contained in:
parent
665d2a0ef0
commit
280223df75
1 changed files with 13 additions and 2 deletions
|
@ -3,6 +3,7 @@ from pathlib import Path
|
||||||
from sqlalchemy import create_engine
|
from sqlalchemy import create_engine
|
||||||
from sqlalchemy.orm import sessionmaker, joinedload
|
from sqlalchemy.orm import sessionmaker, joinedload
|
||||||
from fastapi import FastAPI, HTTPException
|
from fastapi import FastAPI, HTTPException
|
||||||
|
from fastapi.requests import Request
|
||||||
from fastapi.responses import RedirectResponse, FileResponse
|
from fastapi.responses import RedirectResponse, FileResponse
|
||||||
from fastapi.templating import Jinja2Templates
|
from fastapi.templating import Jinja2Templates
|
||||||
|
|
||||||
|
@ -23,9 +24,15 @@ session_pool = sessionmaker(bind=engine)
|
||||||
def home():
|
def home():
|
||||||
return "public archive"
|
return "public archive"
|
||||||
|
|
||||||
|
def get_format(path: str, request: Request) -> str:
|
||||||
|
# XXX: add some logic involving Accept, user-agent, etc.
|
||||||
|
return path.split('.')[-1]
|
||||||
|
|
||||||
|
|
||||||
@app.get("/dl/{path:path}")
|
@app.get("/dl/{path:path}")
|
||||||
async def get_file(path: str):
|
async def get_file(request: Request, path: str, fmt: str = 'auto'):
|
||||||
|
if fmt == 'auto':
|
||||||
|
fmt = get_format(path, request)
|
||||||
with session_pool() as conn:
|
with session_pool() as conn:
|
||||||
original = (
|
original = (
|
||||||
conn.query(database.Original)
|
conn.query(database.Original)
|
||||||
|
@ -48,5 +55,9 @@ async def get_file(path: str):
|
||||||
else:
|
else:
|
||||||
return 404
|
return 404
|
||||||
|
|
||||||
# XXX: handle ?accept=
|
# look for the "best" archived version
|
||||||
|
right_format = [arc for arc in original.archived
|
||||||
|
if arc.format == fmt]
|
||||||
|
if right_format:
|
||||||
|
return RedirectResponse(right_format[0].link)
|
||||||
return RedirectResponse(original.archived[0].link)
|
return RedirectResponse(original.archived[0].link)
|
||||||
|
|
Loading…
Reference in a new issue