mirror of
https://github.com/flibusta-apps/telegram_files_server.git
synced 2025-12-06 04:25:38 +01:00
30 lines
961 B
Python
30 lines
961 B
Python
from typing import Optional
|
|
|
|
from fastapi import APIRouter, Depends, File, Form, HTTPException, UploadFile, status
|
|
from fastapi.responses import StreamingResponse
|
|
|
|
from app.depends import check_token
|
|
from app.serializers import UploadedFile
|
|
from app.services.file_downloader import FileDownloader
|
|
from app.services.file_uploader import FileUploader
|
|
|
|
|
|
router = APIRouter(
|
|
prefix="/api/v1/files", dependencies=[Depends(check_token)], tags=["files"]
|
|
)
|
|
|
|
|
|
@router.post("/upload/", response_model=UploadedFile)
|
|
async def upload_file(file: UploadFile = File({}), caption: Optional[str] = Form({})):
|
|
return await FileUploader.upload(file, caption=caption)
|
|
|
|
|
|
@router.get("/download_by_message/{chat_id}/{message_id}")
|
|
async def download_by_message(chat_id: str, message_id: int):
|
|
data = await FileDownloader.download_by_message_id(message_id)
|
|
|
|
if data is None:
|
|
raise HTTPException(status.HTTP_400_BAD_REQUEST)
|
|
|
|
return StreamingResponse(data)
|