mirror of
https://github.com/flibusta-apps/telegram_files_cache_server.git
synced 2025-12-08 09:30:40 +01:00
Add file downloading
This commit is contained in:
@@ -5,7 +5,7 @@ from typing import Optional
|
||||
from app.models import CachedFile
|
||||
from app.services.caption_getter import get_caption
|
||||
from app.services.downloader import download
|
||||
from app.services.files_uploader import upload_file
|
||||
from app.services.files_client import upload_file
|
||||
from app.services.library_client import get_books, get_book, Book
|
||||
|
||||
|
||||
|
||||
@@ -25,3 +25,19 @@ async def download(
|
||||
name = content_disposition.replace("attachment; filename=", "")
|
||||
|
||||
return response.content, name
|
||||
|
||||
|
||||
async def get_filename(book_id: int, file_type: str) -> Optional[str]:
|
||||
headers = {"Authorization": env_config.DOWNLOADER_API_KEY}
|
||||
|
||||
async with httpx.AsyncClient() as client:
|
||||
response = await client.get(
|
||||
f"{env_config.DOWNLOADER_URL}/filename/{book_id}/{file_type}",
|
||||
headers=headers,
|
||||
timeout=5 * 60,
|
||||
)
|
||||
|
||||
if response.status_code != 200:
|
||||
return None
|
||||
|
||||
return response.text
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
from datetime import datetime
|
||||
from typing import Optional
|
||||
|
||||
import httpx
|
||||
from pydantic import BaseModel
|
||||
@@ -29,3 +30,19 @@ async def upload_file(content: bytes, filename: str, caption: str) -> UploadedFi
|
||||
)
|
||||
|
||||
return UploadedFile.parse_obj(response.json())
|
||||
|
||||
|
||||
async def download_file(chat_id: int, message_id: int) -> Optional[bytes]:
|
||||
headers = {"Authorization": env_config.FILES_SERVER_API_KEY}
|
||||
|
||||
async with httpx.AsyncClient(timeout=60) as client:
|
||||
response = await client.get(
|
||||
f"{env_config.FILES_SERVER_URL}"
|
||||
f"/api/v1/files/download_by_message/{chat_id}/{message_id}",
|
||||
headers=headers,
|
||||
)
|
||||
|
||||
if response.status_code != 200:
|
||||
return None
|
||||
|
||||
return response.content
|
||||
Reference in New Issue
Block a user