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
|
from typing import Optional
|
||||||
|
|
||||||
import httpx
|
import httpx
|
||||||
@@ -28,8 +29,7 @@ async def download(
|
|||||||
await client.aclose()
|
await client.aclose()
|
||||||
return None
|
return None
|
||||||
|
|
||||||
content_disposition = response.headers["Content-Disposition"]
|
name = b64decode(response.headers["x-filename-b64"]).decode()
|
||||||
name = content_disposition.replace("attachment; filename=", "")
|
|
||||||
|
|
||||||
return response, client, name
|
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:
|
if response.status_code != 200:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
return response.text.encode("ascii", "ignore").decode("ascii")
|
return response.text
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import asyncio
|
import asyncio
|
||||||
import base64
|
from base64 import b64encode
|
||||||
|
|
||||||
from arq.connections import ArqRedis
|
from arq.connections import ArqRedis
|
||||||
from fastapi import APIRouter, Depends, HTTPException, Request, status
|
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:
|
if not cached_file:
|
||||||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND)
|
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(
|
data, filename, book = await asyncio.gather(
|
||||||
download_file_from_cache(cache_data["chat_id"], cache_data["message_id"]),
|
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()
|
await client.aclose()
|
||||||
|
|
||||||
assert book
|
assert book
|
||||||
|
assert filename
|
||||||
|
|
||||||
|
filename_ascii = filename.encode("ascii", "ignore").decode("ascii")
|
||||||
|
|
||||||
return StreamingResponse(
|
return StreamingResponse(
|
||||||
response.aiter_bytes(),
|
response.aiter_bytes(),
|
||||||
headers={
|
headers={
|
||||||
"Content-Disposition": f"attachment; filename={filename}",
|
"Content-Disposition": f"attachment; filename={filename_ascii}",
|
||||||
"X-Caption-B64": base64.b64encode(get_caption(book).encode("utf-8")).decode(
|
"X-Caption-B64": b64encode(get_caption(book).encode("utf-8")).decode(),
|
||||||
"latin-1"
|
"X-Filename-B64": b64encode(filename.encode("utf-8")).decode(),
|
||||||
),
|
|
||||||
},
|
},
|
||||||
background=BackgroundTask(close),
|
background=BackgroundTask(close),
|
||||||
)
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user