feat(api): Invalid ID error message
This commit is contained in:
+36
-28
@@ -63,41 +63,49 @@ async def handle_download(
|
|||||||
else:
|
else:
|
||||||
cookies = None
|
cookies = None
|
||||||
|
|
||||||
match mode:
|
try:
|
||||||
case DownloadMode.story:
|
match mode:
|
||||||
story_id = download_id
|
case DownloadMode.story:
|
||||||
case DownloadMode.part:
|
story_id = download_id
|
||||||
story_id = await fetch_story_id(download_id, cookies)
|
case DownloadMode.part:
|
||||||
|
story_id = await fetch_story_id(download_id, cookies)
|
||||||
|
|
||||||
metadata = await retrieve_story(story_id, cookies)
|
metadata = await retrieve_story(story_id, cookies)
|
||||||
book = epub.EpubBook()
|
book = epub.EpubBook()
|
||||||
|
|
||||||
set_metadata(book, metadata)
|
set_metadata(book, metadata)
|
||||||
await set_cover(book, metadata, cookies=cookies)
|
await set_cover(book, metadata, cookies=cookies)
|
||||||
|
|
||||||
async for title in add_chapters(
|
async for title in add_chapters(
|
||||||
book, metadata, download_images=download_images, cookies=cookies
|
book, metadata, download_images=download_images, cookies=cookies
|
||||||
):
|
):
|
||||||
...
|
...
|
||||||
|
|
||||||
# Book is compiled
|
# Book is compiled
|
||||||
temp_file = tempfile.NamedTemporaryFile(
|
temp_file = tempfile.NamedTemporaryFile(
|
||||||
suffix=".epub", delete=True
|
suffix=".epub", delete=True
|
||||||
) # Thanks https://stackoverflow.com/a/75398222
|
) # Thanks https://stackoverflow.com/a/75398222
|
||||||
|
|
||||||
# create epub file
|
# create epub file
|
||||||
epub.write_epub(temp_file, book, {})
|
epub.write_epub(temp_file, book, {})
|
||||||
|
|
||||||
temp_file.file.seek(0)
|
temp_file.file.seek(0)
|
||||||
book_data = temp_file.file.read()
|
book_data = temp_file.file.read()
|
||||||
|
|
||||||
return StreamingResponse(
|
return StreamingResponse(
|
||||||
BytesIO(book_data),
|
BytesIO(book_data),
|
||||||
media_type="application/epub+zip",
|
media_type="application/epub+zip",
|
||||||
headers={
|
headers={
|
||||||
"Content-Disposition": f'attachment; filename="{slugify(metadata["title"])}_{story_id}_{"images" if download_images else ""}.epub"' # Thanks https://stackoverflow.com/a/72729058
|
"Content-Disposition": f'attachment; filename="{slugify(metadata["title"])}_{story_id}_{"images" if download_images else ""}.epub"' # Thanks https://stackoverflow.com/a/72729058
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
|
except KeyError:
|
||||||
|
# Invalid ID
|
||||||
|
return HTMLResponse(
|
||||||
|
status_code=404,
|
||||||
|
content='The story you tried to download does not exist or has been deleted. Support is available on the <a href="https://discord.gg/P9RHC4KCwd" target="_blank">Discord</a>',
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
app.mount("/", StaticFiles(directory=BUILD_PATH), "static")
|
app.mount("/", StaticFiles(directory=BUILD_PATH), "static")
|
||||||
|
|||||||
Reference in New Issue
Block a user