forked from kenzuya/unshackle
fix(attachment): keep custom fonts after mux cleanup
Attachment.delete() now removes a file only when it is in the temp directory, so fonts attached from the custom fonts directory survive.
This commit is contained in:
@@ -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")
|
new = track_module.direct_session(rs, "http://dl-proxy:2")
|
||||||
assert new.cookies.get("via_set") == "1"
|
assert new.cookies.get("via_set") == "1"
|
||||||
assert new.cookies.get("via_item") == "3"
|
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()
|
||||||
|
|||||||
@@ -363,9 +363,7 @@ class dl:
|
|||||||
return fonts[0] if fonts else None
|
return fonts[0] if fonts else None
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def prepare_temp_font(
|
def prepare_temp_font(self, font_name: str, matched_font: Path, system_fonts: dict[str, Path]) -> Path:
|
||||||
self, font_name: str, matched_font: Path, system_fonts: dict[str, Path], temp_font_files: list[Path]
|
|
||||||
) -> Path:
|
|
||||||
"""
|
"""
|
||||||
Copy system font to temp and log if using fallback.
|
Copy system font to temp and log if using fallback.
|
||||||
|
|
||||||
@@ -373,7 +371,6 @@ class dl:
|
|||||||
font_name: Requested font name
|
font_name: Requested font name
|
||||||
matched_font: Path to matched system font
|
matched_font: Path to matched system font
|
||||||
system_fonts: Dictionary of available system fonts
|
system_fonts: Dictionary of available system fonts
|
||||||
temp_font_files: List to track temp files for cleanup
|
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
Path to temp font file
|
Path to temp font file
|
||||||
@@ -388,20 +385,16 @@ class dl:
|
|||||||
|
|
||||||
if not temp_path.exists():
|
if not temp_path.exists():
|
||||||
shutil.copy2(matched_font, temp_path)
|
shutil.copy2(matched_font, temp_path)
|
||||||
temp_font_files.append(temp_path)
|
|
||||||
|
|
||||||
return temp_path
|
return temp_path
|
||||||
|
|
||||||
def attach_subtitle_fonts(
|
def attach_subtitle_fonts(self, font_names: list[str], title: Title_T) -> tuple[int, list[str]]:
|
||||||
self, font_names: list[str], title: Title_T, temp_font_files: list[Path]
|
|
||||||
) -> tuple[int, list[str]]:
|
|
||||||
"""
|
"""
|
||||||
Attach fonts for subtitle rendering.
|
Attach fonts for subtitle rendering.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
font_names: List of font names requested by subtitles
|
font_names: List of font names requested by subtitles
|
||||||
title: Title object to attach fonts to
|
title: Title object to attach fonts to
|
||||||
temp_font_files: List to track temp files for cleanup
|
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
Tuple of (fonts_attached_count, missing_fonts_list)
|
Tuple of (fonts_attached_count, missing_fonts_list)
|
||||||
@@ -418,7 +411,7 @@ class dl:
|
|||||||
continue
|
continue
|
||||||
|
|
||||||
if system_font := find_font_with_fallbacks(font_name, system_fonts):
|
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})"))
|
title.tracks.add(Attachment(path=temp_path, name=f"{font_name} ({system_font.stem})"))
|
||||||
font_count += 1
|
font_count += 1
|
||||||
else:
|
else:
|
||||||
@@ -2006,7 +1999,6 @@ class dl:
|
|||||||
|
|
||||||
title_rule = f"Track {title.track:02}: {title.name}" if isinstance(title, Song) else str(title)
|
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)))
|
console.print(Padding(Rule(f"[rule.text]{title_rule}"), (1, 2)))
|
||||||
temp_font_files = []
|
|
||||||
|
|
||||||
if isinstance(title, Episode) and not self.tmdb_searched:
|
if isinstance(title, Episode) and not self.tmdb_searched:
|
||||||
kind = "tv"
|
kind = "tv"
|
||||||
@@ -3076,7 +3068,7 @@ class dl:
|
|||||||
if subtitle.codec in (Subtitle.Codec.SubStationAlpha, Subtitle.Codec.SubStationAlphav4):
|
if subtitle.codec in (Subtitle.Codec.SubStationAlpha, Subtitle.Codec.SubStationAlphav4):
|
||||||
font_names.extend(Subtitle.extract_fonts(subtitle.path.read_text("utf8")))
|
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:
|
if font_count:
|
||||||
self.log.info(f"Attached {font_count} fonts for the Subtitles")
|
self.log.info(f"Attached {font_count} fonts for the Subtitles")
|
||||||
@@ -3480,13 +3472,7 @@ class dl:
|
|||||||
track.delete()
|
track.delete()
|
||||||
|
|
||||||
for attachment in title.tracks.attachments:
|
for attachment in title.tracks.attachments:
|
||||||
if attachment.path and attachment.path in temp_font_files:
|
attachment.delete()
|
||||||
attachment.path = None
|
|
||||||
else:
|
|
||||||
attachment.delete()
|
|
||||||
|
|
||||||
for temp_path in temp_font_files:
|
|
||||||
temp_path.unlink(missing_ok=True)
|
|
||||||
for temp_path in sidecar_original_paths.values():
|
for temp_path in sidecar_original_paths.values():
|
||||||
temp_path.unlink(missing_ok=True)
|
temp_path.unlink(missing_ok=True)
|
||||||
finally:
|
finally:
|
||||||
|
|||||||
@@ -154,8 +154,9 @@ class Attachment:
|
|||||||
return hex(checksum)
|
return hex(checksum)
|
||||||
|
|
||||||
def delete(self) -> None:
|
def delete(self) -> None:
|
||||||
if self.path and self.path.exists():
|
"""Delete the file only if it is in the temp directory. Files in other locations belong to the user."""
|
||||||
self.path.unlink()
|
if self.path and self.path.is_relative_to(config.directories.temp):
|
||||||
|
self.path.unlink(missing_ok=True)
|
||||||
self.path = None
|
self.path = None
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
|
|||||||
@@ -1003,8 +1003,7 @@ class Tracks:
|
|||||||
for track in self:
|
for track in self:
|
||||||
track.delete()
|
track.delete()
|
||||||
for attachment in self.attachments:
|
for attachment in self.attachments:
|
||||||
if attachment.path and attachment.path.exists():
|
attachment.delete()
|
||||||
attachment.path.unlink()
|
|
||||||
|
|
||||||
|
|
||||||
__all__ = ("Tracks",)
|
__all__ = ("Tracks",)
|
||||||
|
|||||||
Reference in New Issue
Block a user