Skip to content

Commit

Permalink
Merge pull request #213 from aplaice/paranoia
Browse files Browse the repository at this point in the history
Be paranoid about clearing media before copying when exporting
  • Loading branch information
aplaice authored Sep 15, 2024
2 parents a441f96 + 7d059e7 commit db2fdf2
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion crowd_anki/export/anki_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def _save_changes(self, deck, is_export_child=False):
def _copy_media(self, deck, deck_directory):
media_directory = deck_directory.joinpath(MEDIA_SUBDIRECTORY_NAME)

shutil.rmtree(str(media_directory.resolve()), ignore_errors=True)
self._clear_media(media_directory.resolve())
media_directory.mkdir(parents=True, exist_ok=True)

for file_src in deck.get_media_file_list():
Expand All @@ -93,3 +93,18 @@ def _copy_media(self, deck, deck_directory):
str(media_directory.resolve()))
except IOError as ioerror:
print("Failed to copy a file {}. Full error: {}".format(file_src, ioerror))

def _clear_media(self, media_directory: Path):
"""Clear existing media from export dir before copying.
Try to be minimally invasive to avoid catastrophic data
removal if the relevant variables become incorrectly set
elsewhere.
"""
unsafe_dirs = [Path.home(), Path("/")]
if not media_directory in unsafe_dirs:
if media_directory.is_dir():
for media_file in media_directory.iterdir():
if media_file.is_file():
media_file.unlink()

0 comments on commit db2fdf2

Please sign in to comment.