From 697f5ec0e90111b1534f3449ce9d29d1e7e0a905 Mon Sep 17 00:00:00 2001 From: Kurbanov Bulat Date: Wed, 9 Feb 2022 14:36:06 +0300 Subject: [PATCH] Fix download fail --- src/app/services/base.py | 4 ++-- src/app/services/fl_downloader.py | 8 ++++---- src/app/views.py | 9 +++++++-- 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/src/app/services/base.py b/src/app/services/base.py index 20fd9de..a500092 100644 --- a/src/app/services/base.py +++ b/src/app/services/base.py @@ -1,9 +1,9 @@ -from typing import Protocol +from typing import Protocol, Optional class BaseDownloader(Protocol): @classmethod async def download( cls, remote_id: int, file_type: str, source_id: int - ) -> tuple[bytes, str]: + ) -> Optional[tuple[bytes, str]]: ... diff --git a/src/app/services/fl_downloader.py b/src/app/services/fl_downloader.py index 45a7553..aa0ff49 100644 --- a/src/app/services/fl_downloader.py +++ b/src/app/services/fl_downloader.py @@ -146,7 +146,7 @@ class FLDownloader(BaseDownloader): self.source_id, self.book_id ) - async def _get_content(self) -> tuple[bytes, str]: + async def _get_content(self) -> Optional[tuple[bytes, str]]: tasks = set() if self.file_type in ["epub", "mobi"]: @@ -158,12 +158,12 @@ class FLDownloader(BaseDownloader): data = await self._wait_until_some_done(tasks) if data is None: - raise ValueError + return None content, is_zip = data if content is None or is_zip is None: - raise ValueError + return None if is_zip: content = await asyncio.get_event_loop().run_in_executor( @@ -192,6 +192,6 @@ class FLDownloader(BaseDownloader): @classmethod async def download( cls, remote_id: int, file_type: str, source_id: int - ) -> tuple[bytes, str]: + ) -> Optional[tuple[bytes, str]]: downloader = cls(remote_id, file_type, source_id) return await downloader._download() diff --git a/src/app/views.py b/src/app/views.py index 5c7b0e9..661431f 100644 --- a/src/app/views.py +++ b/src/app/views.py @@ -1,4 +1,4 @@ -from fastapi import APIRouter, Depends, Response +from fastapi import APIRouter, Depends, Response, status from app.depends import check_token from app.services.book_library import BookLibraryClient @@ -16,7 +16,12 @@ router = APIRouter( async def download(source_id: int, remote_id: int, file_type: str): downloader = await DownloadersManager.get_downloader(source_id) - content, filename = await downloader.download(remote_id, file_type, source_id) + result = await downloader.download(remote_id, file_type, source_id) + + if result is None: + return Response(status_code=status.HTTP_204_NO_CONTENT) + + content, filename = result return Response( content, headers={"Content-Disposition": f"attachment; filename={filename}"}