Skip to content

Commit

Permalink
Use temporary files to avoid concurrent conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
xxnuo committed Dec 20, 2024
1 parent ec57d04 commit 3ca35f1
Showing 1 changed file with 19 additions and 11 deletions.
30 changes: 19 additions & 11 deletions pdf2zh/high_level.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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", {}),
Expand All @@ -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

0 comments on commit 3ca35f1

Please sign in to comment.