Fix download fail

This commit is contained in:
2022-02-09 14:36:06 +03:00
parent 6894379c19
commit 697f5ec0e9
3 changed files with 13 additions and 8 deletions

View File

@@ -1,9 +1,9 @@
from typing import Protocol from typing import Protocol, Optional
class BaseDownloader(Protocol): class BaseDownloader(Protocol):
@classmethod @classmethod
async def download( async def download(
cls, remote_id: int, file_type: str, source_id: int cls, remote_id: int, file_type: str, source_id: int
) -> tuple[bytes, str]: ) -> Optional[tuple[bytes, str]]:
... ...

View File

@@ -146,7 +146,7 @@ class FLDownloader(BaseDownloader):
self.source_id, self.book_id 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() tasks = set()
if self.file_type in ["epub", "mobi"]: if self.file_type in ["epub", "mobi"]:
@@ -158,12 +158,12 @@ class FLDownloader(BaseDownloader):
data = await self._wait_until_some_done(tasks) data = await self._wait_until_some_done(tasks)
if data is None: if data is None:
raise ValueError return None
content, is_zip = data content, is_zip = data
if content is None or is_zip is None: if content is None or is_zip is None:
raise ValueError return None
if is_zip: if is_zip:
content = await asyncio.get_event_loop().run_in_executor( content = await asyncio.get_event_loop().run_in_executor(
@@ -192,6 +192,6 @@ class FLDownloader(BaseDownloader):
@classmethod @classmethod
async def download( async def download(
cls, remote_id: int, file_type: str, source_id: int 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) downloader = cls(remote_id, file_type, source_id)
return await downloader._download() return await downloader._download()

View File

@@ -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.depends import check_token
from app.services.book_library import BookLibraryClient 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): async def download(source_id: int, remote_id: int, file_type: str):
downloader = await DownloadersManager.get_downloader(source_id) 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( return Response(
content, headers={"Content-Disposition": f"attachment; filename={filename}"} content, headers={"Content-Disposition": f"attachment; filename={filename}"}