mirror of
https://github.com/flibusta-apps/telegram_files_server.git
synced 2025-12-06 12:35:39 +01:00
Add downloading
This commit is contained in:
@@ -1,12 +1,20 @@
|
||||
from typing import Optional
|
||||
|
||||
from fastapi import File, UploadFile, Depends, Form, APIRouter, HTTPException
|
||||
|
||||
from starlette import status
|
||||
from fastapi import (
|
||||
File,
|
||||
UploadFile,
|
||||
Depends,
|
||||
Form,
|
||||
APIRouter,
|
||||
HTTPException,
|
||||
Response,
|
||||
status,
|
||||
)
|
||||
|
||||
from app.depends import check_token
|
||||
from app.models import UploadedFile as UploadedFileDB
|
||||
from app.serializers import UploadedFile, CreateUploadedFile
|
||||
from app.services.file_downloader import FileDownloader
|
||||
from app.services.file_uploader import FileUploader
|
||||
|
||||
|
||||
@@ -46,6 +54,26 @@ async def upload_file(file: UploadFile = File({}), caption: Optional[str] = Form
|
||||
return await FileUploader.upload(file, caption=caption)
|
||||
|
||||
|
||||
@router.get("/download_by_file_id/{file_id}")
|
||||
async def download_by_file_id(file_id: str):
|
||||
data = await FileDownloader.download_by_file_id(file_id)
|
||||
|
||||
if data is None:
|
||||
raise HTTPException(status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
return Response(data.read())
|
||||
|
||||
|
||||
@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 Response(data.read())
|
||||
|
||||
|
||||
@router.delete("/{file_id}", response_model=UploadedFile, responses={400: {}})
|
||||
async def delete_file(file_id: int):
|
||||
uploaded_file = await UploadedFileDB.objects.get_or_none(id=file_id)
|
||||
|
||||
Reference in New Issue
Block a user