mirror of
https://github.com/flibusta-apps/telegram_files_cache_server.git
synced 2025-12-06 14:45:36 +01:00
Add x-filename-b64 header
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
from base64 import b64decode
|
||||
from typing import Optional
|
||||
|
||||
import httpx
|
||||
@@ -28,8 +29,7 @@ async def download(
|
||||
await client.aclose()
|
||||
return None
|
||||
|
||||
content_disposition = response.headers["Content-Disposition"]
|
||||
name = content_disposition.replace("attachment; filename=", "")
|
||||
name = b64decode(response.headers["x-filename-b64"]).decode()
|
||||
|
||||
return response, client, name
|
||||
|
||||
@@ -47,4 +47,4 @@ async def get_filename(book_id: int, file_type: str) -> Optional[str]:
|
||||
if response.status_code != 200:
|
||||
return None
|
||||
|
||||
return response.text.encode("ascii", "ignore").decode("ascii")
|
||||
return response.text
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import asyncio
|
||||
import base64
|
||||
from base64 import b64encode
|
||||
|
||||
from arq.connections import ArqRedis
|
||||
from fastapi import APIRouter, Depends, HTTPException, Request, status
|
||||
@@ -51,7 +51,7 @@ async def download_cached_file(request: Request, object_id: int, object_type: st
|
||||
if not cached_file:
|
||||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND)
|
||||
|
||||
cache_data = cached_file.data
|
||||
cache_data: dict = cached_file.data # type: ignore
|
||||
|
||||
data, filename, book = await asyncio.gather(
|
||||
download_file_from_cache(cache_data["chat_id"], cache_data["message_id"]),
|
||||
@@ -76,14 +76,16 @@ async def download_cached_file(request: Request, object_id: int, object_type: st
|
||||
await client.aclose()
|
||||
|
||||
assert book
|
||||
assert filename
|
||||
|
||||
filename_ascii = filename.encode("ascii", "ignore").decode("ascii")
|
||||
|
||||
return StreamingResponse(
|
||||
response.aiter_bytes(),
|
||||
headers={
|
||||
"Content-Disposition": f"attachment; filename={filename}",
|
||||
"X-Caption-B64": base64.b64encode(get_caption(book).encode("utf-8")).decode(
|
||||
"latin-1"
|
||||
),
|
||||
"Content-Disposition": f"attachment; filename={filename_ascii}",
|
||||
"X-Caption-B64": b64encode(get_caption(book).encode("utf-8")).decode(),
|
||||
"X-Filename-B64": b64encode(filename.encode("utf-8")).decode(),
|
||||
},
|
||||
background=BackgroundTask(close),
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user