Skip to content

Commit

Permalink
fix: handle long file names
Browse files Browse the repository at this point in the history
  • Loading branch information
aalemayhu committed Oct 15, 2024
1 parent 9782978 commit ba04697
Showing 1 changed file with 19 additions and 8 deletions.
27 changes: 19 additions & 8 deletions helpers/write_apkg.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
"""
Write APKG file to disk
"""

import os
import sys
import tempfile

from genanki import Deck, Package


def _write_new_apkg(payload, media_files):
first_id = ""
decks = []
Expand All @@ -23,7 +19,22 @@ def _write_new_apkg(payload, media_files):

pkg = Package(decks)
pkg.media_files = media_files
fout_anki = f'{first_id}.apkg'

pkg.write_to_file(fout_anki)
sys.stdout.write(os.getcwd() + "/" + fout_anki)
# Use a temporary file to avoid file name too long errors
with tempfile.NamedTemporaryFile(delete=False) as tmp_file:
fout_anki = tmp_file.name
try:
pkg.write_to_file(fout_anki)
except OSError as e:
if e.errno == 36: # File name too long
# Handle the error, e.g., shorten the file name or move to a different directory
print("File name too long. Shortening file name.")
fout_anki = fout_anki[:255] + ".apkg"
pkg.write_to_file(fout_anki)
else:
raise e # Re-raise other exceptions

final_path = os.path.join(os.getcwd() , fout_anki)
os.rename(fout_anki, final_path)

sys.stdout.write(final_path)

0 comments on commit ba04697

Please sign in to comment.