From 3ca35f194e38438c5ce026df069a2a663706e8f8 Mon Sep 17 00:00:00 2001 From: Kerry <54252779+xxnuo@users.noreply.github.com> Date: Sat, 21 Dec 2024 00:13:59 +0800 Subject: [PATCH] Use temporary files to avoid concurrent conflicts --- pdf2zh/high_level.py | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/pdf2zh/high_level.py b/pdf2zh/high_level.py index b96ac916..2b654583 100644 --- a/pdf2zh/high_level.py +++ b/pdf2zh/high_level.py @@ -331,13 +331,12 @@ def translate( try: r = requests.get(file, allow_redirects=True) if r.status_code == 200: - if not os.path.exists("./pdf2zh_files"): - print("Making a temporary dir for downloading PDF files...") - os.mkdir(os.path.dirname("./pdf2zh_files")) - with open("./pdf2zh_files/tmp_download.pdf", "wb") as f: + with tempfile.NamedTemporaryFile( + suffix=".pdf", delete=False + ) as tmp_file: print(f"Writing the file: {file}...") - f.write(r.content) - file = "./pdf2zh_files/tmp_download.pdf" + tmp_file.write(r.content) + file = tmp_file.name else: r.raise_for_status() except Exception as e: @@ -349,13 +348,21 @@ def translate( # If the commandline has specified converting to PDF/A format # --compatible / -cp if compatible: - file_pdfa = file.replace(".pdf", "-pdfa.pdf") - print(f"Converting {file} to PDF/A format...") - convert_to_pdfa(file, file_pdfa) - doc_raw = open(file_pdfa, "rb") + with tempfile.NamedTemporaryFile( + suffix="-pdfa.pdf", delete=False + ) as tmp_pdfa: + print(f"Converting {file} to PDF/A format...") + convert_to_pdfa(file, tmp_pdfa.name) + doc_raw = open(tmp_pdfa.name, "rb") + os.unlink(tmp_pdfa.name) else: doc_raw = open(file, "rb") s_raw = doc_raw.read() + doc_raw.close() + + if file.startswith(tempfile.gettempdir()): + os.unlink(file) + s_mono, s_dual = translate_stream( s_raw, envs=kwarg.get("envs", {}), @@ -368,7 +375,8 @@ def translate( doc_dual = open(file_dual, "wb") doc_mono.write(s_mono) doc_dual.write(s_dual) + doc_mono.close() + doc_dual.close() result_files.append((str(file_mono), str(file_dual))) return result_files - return result_files