fix(api): Close and delete temp files

This commit is contained in:
TheOnlyWayUp
2024-12-07 09:32:37 +00:00
parent 96d367da27
commit 5cbef75d19
2 changed files with 8 additions and 2 deletions
+4 -1
View File
@@ -482,7 +482,7 @@ class PDFGenerator:
# self.canvas = Canvas(self.file) # self.canvas = Canvas(self.file)
async def add_chapters(self, contents: List[str], download_images: bool = False): async def add_chapters(self, contents: List[str], download_images: bool = False):
chapters = [] chapters: List[tempfile._TemporaryFileWrapper] = []
for part, content in zip(self.data["parts"], contents): for part, content in zip(self.data["parts"], contents):
html = BeautifulSoup(content, features="lxml") html = BeautifulSoup(content, features="lxml")
@@ -561,6 +561,9 @@ class PDFGenerator:
) )
) )
for chapter in chapters:
chapter.file.close()
def dump(self) -> PDFGenerator: def dump(self) -> PDFGenerator:
self.file.seek(0) self.file.seek(0)
+4 -1
View File
@@ -182,7 +182,10 @@ async def handle_download(
): ):
print(title) print(title)
book_bytes = book.dump().file.read() book_file = book.dump().file
book_bytes = book_file.read()
book_file.close()
match format: match format:
case DownloadFormat.epub: case DownloadFormat.epub:
media_type = "application/epub+zip" media_type = "application/epub+zip"