mirror of
https://github.com/flibusta-apps/telegram_files_cache_server.git
synced 2025-12-06 06:35:38 +01:00
Fix caching
This commit is contained in:
@@ -1,8 +1,7 @@
|
|||||||
import collections
|
import collections
|
||||||
from datetime import date, timedelta
|
from datetime import date, timedelta
|
||||||
from io import BytesIO
|
from io import BytesIO
|
||||||
from tempfile import SpooledTemporaryFile
|
from typing import Optional
|
||||||
from typing import Optional, cast
|
|
||||||
|
|
||||||
from fastapi import UploadFile
|
from fastapi import UploadFile
|
||||||
|
|
||||||
@@ -81,6 +80,7 @@ async def cache_file(book: Book, file_type: str) -> Optional[CachedFile]:
|
|||||||
object_id=book.id, object_type=file_type
|
object_id=book.id, object_type=file_type
|
||||||
).exists():
|
).exists():
|
||||||
return
|
return
|
||||||
|
|
||||||
try:
|
try:
|
||||||
data = await download(book.source.id, book.remote_id, file_type)
|
data = await download(book.source.id, book.remote_id, file_type)
|
||||||
except httpx.HTTPError:
|
except httpx.HTTPError:
|
||||||
@@ -95,14 +95,14 @@ async def cache_file(book: Book, file_type: str) -> Optional[CachedFile]:
|
|||||||
temp_file = UploadFile(BytesIO(), filename=filename)
|
temp_file = UploadFile(BytesIO(), filename=filename)
|
||||||
async for chunk in response.aiter_bytes(2048):
|
async for chunk in response.aiter_bytes(2048):
|
||||||
await temp_file.write(chunk)
|
await temp_file.write(chunk)
|
||||||
|
|
||||||
|
file_size = temp_file.file.tell()
|
||||||
await temp_file.seek(0)
|
await temp_file.seek(0)
|
||||||
|
|
||||||
await response.aclose()
|
await response.aclose()
|
||||||
await client.aclose()
|
await client.aclose()
|
||||||
|
|
||||||
upload_data = await upload_file(
|
upload_data = await upload_file(temp_file.file, file_size, filename, caption)
|
||||||
cast(SpooledTemporaryFile, temp_file.file), filename, caption
|
|
||||||
)
|
|
||||||
|
|
||||||
if upload_data is None:
|
if upload_data is None:
|
||||||
return None
|
return None
|
||||||
|
|||||||
@@ -1,27 +1,29 @@
|
|||||||
from datetime import datetime
|
from typing import BinaryIO, Optional
|
||||||
from tempfile import SpooledTemporaryFile
|
|
||||||
from typing import Optional
|
|
||||||
|
|
||||||
import httpx
|
import httpx
|
||||||
from pydantic import BaseModel
|
from pydantic import BaseModel
|
||||||
|
from typing_extensions import TypedDict
|
||||||
|
|
||||||
from core.config import env_config
|
from core.config import env_config
|
||||||
|
|
||||||
|
|
||||||
|
class Data(TypedDict):
|
||||||
|
chat_id: str | int
|
||||||
|
message_id: int
|
||||||
|
|
||||||
|
|
||||||
class UploadedFile(BaseModel):
|
class UploadedFile(BaseModel):
|
||||||
id: int
|
|
||||||
backend: str
|
backend: str
|
||||||
data: dict
|
data: Data
|
||||||
upload_time: datetime
|
|
||||||
|
|
||||||
|
|
||||||
async def upload_file(
|
async def upload_file(
|
||||||
content: SpooledTemporaryFile, filename: str, caption: str
|
content: BinaryIO, content_size: int, filename: str, caption: str
|
||||||
) -> Optional[UploadedFile]:
|
) -> Optional[UploadedFile]:
|
||||||
headers = {"Authorization": env_config.FILES_SERVER_API_KEY}
|
headers = {"Authorization": env_config.FILES_SERVER_API_KEY}
|
||||||
|
|
||||||
async with httpx.AsyncClient() as client:
|
async with httpx.AsyncClient() as client:
|
||||||
form = {"caption": caption}
|
form = {"caption": caption, "file_size": content_size}
|
||||||
files = {"file": (filename, content)}
|
files = {"file": (filename, content)}
|
||||||
|
|
||||||
response = await client.post(
|
response = await client.post(
|
||||||
|
|||||||
Reference in New Issue
Block a user