Fix download for other files than epub (#174)

Fix #173
This commit is contained in:
CaliBrain
2025-05-06 08:44:43 -04:00
committed by GitHub
parent 3d2d285da4
commit 690c2b699c
2 changed files with 9 additions and 9 deletions
+7 -7
View File
@@ -239,19 +239,19 @@ def api_local_download() -> Union[Response, Tuple[Response, int]]:
return jsonify({"error": "No book ID provided"}), 400
try:
file_data, file_name = backend.get_book_data(book_id)
file_data, book_info = backend.get_book_data(book_id)
if file_data is None:
# Book data not found or not available
return jsonify({"error": "File not found"}), 404
# Santize the file name
file_name = re.sub(r'[\\/:*?"<>|]', '_', file_name.strip())[:255]
file_name = book_info.title
file_name = re.sub(r'[\\/:*?"<>|]', '_', file_name.strip())[:245]
file_extension = book_info.format
# Prepare the file for sending to the client
epub_file = io.BytesIO(file_data)
# Typically EPUB mime-type: 'application/epub+zip'
data = io.BytesIO(file_data)
return send_file(
epub_file,
mimetype='application/epub+zip',
download_name=f"{file_name}.epub",
data,
download_name=f"{file_name}.{file_extension}",
as_attachment=True
)
+2 -2
View File
@@ -84,7 +84,7 @@ def queue_status() -> Dict[str, Dict[str, Any]]:
for status_type, books in status.items()
}
def get_book_data(book_id: str) -> Tuple[Optional[bytes], str] :
def get_book_data(book_id: str) -> Tuple[Optional[bytes], BookInfo]:
"""Get book data for a specific book, including its title.
Args:
@@ -97,7 +97,7 @@ def get_book_data(book_id: str) -> Tuple[Optional[bytes], str] :
book_info = book_queue._book_data[book_id]
path = book_info.download_path
with open(path, "rb") as f:
return f.read(), book_info.title
return f.read(), book_info
except Exception as e:
logger.error_trace(f"Error getting book data: {e}")
book_info.download_path = None