diff --git a/tests/tracks/test_attachment_download.py b/tests/tracks/test_attachment_download.py index b1777315..2e9cd5fe 100644 --- a/tests/tracks/test_attachment_download.py +++ b/tests/tracks/test_attachment_download.py @@ -114,3 +114,21 @@ def test_direct_session_copies_rnet_cookies_set_by_name() -> None: new = track_module.direct_session(rs, "http://dl-proxy:2") assert new.cookies.get("via_set") == "1" assert new.cookies.get("via_item") == "3" + + +def test_delete_removes_only_files_in_the_temp_directory(tmp_path, monkeypatch) -> None: + temp = tmp_path / "temp" + temp.mkdir() + monkeypatch.setattr(config.directories, "temp", temp) + temp_font = temp / "font.ttf" + user_font = tmp_path / "font.ttf" + temp_font.write_bytes(b"font") + user_font.write_bytes(b"font") + + for path in (temp_font, user_font): + attachment = Attachment(path=path) + attachment.delete() + assert attachment.path is None + + assert not temp_font.exists() + assert user_font.exists() diff --git a/unshackle/commands/dl.py b/unshackle/commands/dl.py index 79bed827..e64c481d 100644 --- a/unshackle/commands/dl.py +++ b/unshackle/commands/dl.py @@ -363,9 +363,7 @@ class dl: return fonts[0] if fonts else None return None - def prepare_temp_font( - self, font_name: str, matched_font: Path, system_fonts: dict[str, Path], temp_font_files: list[Path] - ) -> Path: + def prepare_temp_font(self, font_name: str, matched_font: Path, system_fonts: dict[str, Path]) -> Path: """ Copy system font to temp and log if using fallback. @@ -373,7 +371,6 @@ class dl: font_name: Requested font name matched_font: Path to matched system font system_fonts: Dictionary of available system fonts - temp_font_files: List to track temp files for cleanup Returns: Path to temp font file @@ -388,20 +385,16 @@ class dl: if not temp_path.exists(): shutil.copy2(matched_font, temp_path) - temp_font_files.append(temp_path) return temp_path - def attach_subtitle_fonts( - self, font_names: list[str], title: Title_T, temp_font_files: list[Path] - ) -> tuple[int, list[str]]: + def attach_subtitle_fonts(self, font_names: list[str], title: Title_T) -> tuple[int, list[str]]: """ Attach fonts for subtitle rendering. Args: font_names: List of font names requested by subtitles title: Title object to attach fonts to - temp_font_files: List to track temp files for cleanup Returns: Tuple of (fonts_attached_count, missing_fonts_list) @@ -418,7 +411,7 @@ class dl: continue if system_font := find_font_with_fallbacks(font_name, system_fonts): - temp_path = self.prepare_temp_font(font_name, system_font, system_fonts, temp_font_files) + temp_path = self.prepare_temp_font(font_name, system_font, system_fonts) title.tracks.add(Attachment(path=temp_path, name=f"{font_name} ({system_font.stem})")) font_count += 1 else: @@ -2006,7 +1999,6 @@ class dl: title_rule = f"Track {title.track:02}: {title.name}" if isinstance(title, Song) else str(title) console.print(Padding(Rule(f"[rule.text]{title_rule}"), (1, 2))) - temp_font_files = [] if isinstance(title, Episode) and not self.tmdb_searched: kind = "tv" @@ -3076,7 +3068,7 @@ class dl: if subtitle.codec in (Subtitle.Codec.SubStationAlpha, Subtitle.Codec.SubStationAlphav4): font_names.extend(Subtitle.extract_fonts(subtitle.path.read_text("utf8"))) - font_count, missing_fonts = self.attach_subtitle_fonts(font_names, title, temp_font_files) + font_count, missing_fonts = self.attach_subtitle_fonts(font_names, title) if font_count: self.log.info(f"Attached {font_count} fonts for the Subtitles") @@ -3480,13 +3472,7 @@ class dl: track.delete() for attachment in title.tracks.attachments: - if attachment.path and attachment.path in temp_font_files: - attachment.path = None - else: - attachment.delete() - - for temp_path in temp_font_files: - temp_path.unlink(missing_ok=True) + attachment.delete() for temp_path in sidecar_original_paths.values(): temp_path.unlink(missing_ok=True) finally: diff --git a/unshackle/core/tracks/attachment.py b/unshackle/core/tracks/attachment.py index d73aedb0..c0fc82c3 100644 --- a/unshackle/core/tracks/attachment.py +++ b/unshackle/core/tracks/attachment.py @@ -154,8 +154,9 @@ class Attachment: return hex(checksum) def delete(self) -> None: - if self.path and self.path.exists(): - self.path.unlink() + """Delete the file only if it is in the temp directory. Files in other locations belong to the user.""" + if self.path and self.path.is_relative_to(config.directories.temp): + self.path.unlink(missing_ok=True) self.path = None @classmethod diff --git a/unshackle/core/tracks/tracks.py b/unshackle/core/tracks/tracks.py index 4e21550a..429c5fbf 100644 --- a/unshackle/core/tracks/tracks.py +++ b/unshackle/core/tracks/tracks.py @@ -1003,8 +1003,7 @@ class Tracks: for track in self: track.delete() for attachment in self.attachments: - if attachment.path and attachment.path.exists(): - attachment.path.unlink() + attachment.delete() __all__ = ("Tracks",)