Add file downloading

This commit is contained in:
2022-01-09 20:55:24 +03:00
parent cefc84af81
commit bb300daedd
5 changed files with 67 additions and 2 deletions

View File

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

View File

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

View File

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