fix(downloads): shell-safe names, refresh display_name on re-download, GC orphan sweep
Follow-up to user prod testing of 0.51.0: - re-download now clears job.display_name so the card title + Content-Disposition filename are re-derived from the freshly-cleaned title (previously an auto-name from an older un-cleaned title — still carrying a "… views · … reactions |" prefix — survived the rebuild). - on-disk AND served names are now a strict shell-safe slug: only [A-Za-z0-9._-] survive; every other char (the "!" the user saw, plus ( ) [ ] & $ ' " ; | ? * …) collapses to "_". The id no longer uses [brackets] (glob metacharacters). Shared _slug helper for sanitize + display_filename. - GC gains a 5th pass: sweep on-disk files/dirs no ready asset owns (old accented channel dirs, a poster from a superseded naming scheme left after a re-download), skipping the dot system trees and anything newer than an hour so an in-flight download is never touched. - Tests: slug strips !/$/()[]; rel_path is bracket-free; orphan-sweep keeps owned/system/fresh files.
This commit is contained in:
@@ -18,12 +18,14 @@ from datetime import date
|
||||
from pathlib import Path
|
||||
from xml.sax.saxutils import escape
|
||||
|
||||
# Reserve room for the " [id].ext" suffix + parent dirs within the 255-byte component limit.
|
||||
# Reserve room for the "_id.ext" suffix + parent dirs within the 255-char component limit.
|
||||
_MAX_TITLE = 120
|
||||
# User-facing (Content-Disposition) names can be longer than an on-disk component.
|
||||
_MAX_DISPLAY = 150
|
||||
# Filesystem-illegal on Windows/Plex + control chars.
|
||||
_ILLEGAL = re.compile(r'[<>:"/\\|?*\x00-\x1f]')
|
||||
# The ONLY characters allowed in a generated name — letters, digits, and `._-`. Everything else
|
||||
# (spaces, and every shell/filesystem-unpleasant metacharacter: ! ? * & $ ( ) [ ] { } ' " ; | < >
|
||||
# etc.) collapses to a single underscore, so names are safe to handle in any shell or tool.
|
||||
_UNSAFE = re.compile(r"[^A-Za-z0-9._-]+")
|
||||
# Unicode categories we drop entirely: emoji/pictographs (So), modifier symbols/skin-tones
|
||||
# (Sk), and every control/format/private/surrogate/unassigned char (C*).
|
||||
_DROP_CATEGORIES = {"So", "Sk", "Cc", "Cf", "Cs", "Co", "Cn"}
|
||||
@@ -62,49 +64,33 @@ class MediaMeta:
|
||||
thumbnail_url: str | None = None
|
||||
|
||||
|
||||
def sanitize(name: str, limit: int = _MAX_TITLE) -> str:
|
||||
"""Turn an arbitrary (emoji-laden, clickbait) title into a clean, space-free ASCII path token.
|
||||
def _slug(name: str, limit: int, fallback: str) -> str:
|
||||
"""Reduce an arbitrary (emoji-laden, clickbait, accented) title to a portable, shell-safe token.
|
||||
|
||||
Steps: NFKC-normalize (fold fancy width/compat forms) → drop emoji/symbols/control chars →
|
||||
ASCII-fold accents (á→a, ő→o, ß→ss) → strip filesystem-illegal chars → collapse whitespace and
|
||||
runs of the same punctuation → join words with underscores → trim junk punctuation from the ends
|
||||
→ length-cap. The result is pure ASCII with no spaces or path separators (digits survive)."""
|
||||
text = unicodedata.normalize("NFKC", name or "")
|
||||
text = "".join(
|
||||
ch for ch in text if unicodedata.category(ch) not in _DROP_CATEGORIES
|
||||
)
|
||||
text = _ascii_fold(text)
|
||||
text = _ILLEGAL.sub("", text)
|
||||
# Collapse repeated punctuation ("!!!", "???", "-----") to a single mark.
|
||||
text = re.sub(r"([^\w\s])\1{1,}", r"\1", text)
|
||||
# Whitespace → single underscore (no spaces in generated names).
|
||||
text = re.sub(r"\s+", "_", text.strip())
|
||||
# Tidy separator pile-ups and trim junk from the ends.
|
||||
text = re.sub(r"_{2,}", "_", text).strip("_.-·—–|,;:!?ّ ")
|
||||
# Cap by BYTES, not characters: a path component's hard limit is 255 BYTES, and rel_path packs
|
||||
# the (byte-capped) channel + date + id + ext into that same component — so an accented/multi-byte
|
||||
# title capped only by char count could still overflow to ENAMETOOLONG. Truncate on a UTF-8
|
||||
# boundary (decode(..., "ignore") drops a trailing partial char) so we never split a character.
|
||||
if len(text.encode("utf-8")) > limit:
|
||||
text = text.encode("utf-8")[:limit].decode("utf-8", "ignore").rstrip("_.-")
|
||||
return text or "untitled"
|
||||
|
||||
|
||||
def display_filename(name: str) -> str:
|
||||
"""User-facing download filename (Content-Disposition) base: the same ASCII-slug policy as the
|
||||
on-disk name — emoji/symbols/control + illegal chars dropped, accents folded, whitespace joined
|
||||
with underscores — so a saved file is portable (no spaces, no accents). Only the length cap
|
||||
differs (a served name may be longer than an on-disk path component)."""
|
||||
NFKC-normalize → drop emoji/symbols/control chars → ASCII-fold accents (á→a, ő→o, ß→ss) →
|
||||
replace every run of unsafe characters (spaces + shell/filesystem metacharacters) with a single
|
||||
underscore → collapse separator pile-ups → trim junk from the ends → length-cap. The result is
|
||||
pure ASCII containing only `[A-Za-z0-9._-]` — no spaces, accents, or characters that need
|
||||
quoting in a shell."""
|
||||
text = unicodedata.normalize("NFKC", name or "")
|
||||
text = "".join(ch for ch in text if unicodedata.category(ch) not in _DROP_CATEGORIES)
|
||||
text = _ascii_fold(text)
|
||||
text = _ILLEGAL.sub("", text)
|
||||
text = re.sub(r"([^\w\s])\1{1,}", r"\1", text)
|
||||
text = re.sub(r"\s+", "_", text.strip())
|
||||
text = re.sub(r"_{2,}", "_", text).strip("_.-·—–|,;:!?ّ ")
|
||||
if len(text) > _MAX_DISPLAY:
|
||||
text = text[:_MAX_DISPLAY].rstrip("_.-")
|
||||
return text or "download"
|
||||
text = _UNSAFE.sub("_", text)
|
||||
text = re.sub(r"[-_]{2,}", "_", text).strip("_.-")
|
||||
if len(text) > limit: # pure ASCII → 1 char == 1 byte, so a char cap is also the byte cap
|
||||
text = text[:limit].rstrip("_.-")
|
||||
return text or fallback
|
||||
|
||||
|
||||
def sanitize(name: str, limit: int = _MAX_TITLE) -> str:
|
||||
"""On-disk path token: portable, shell-safe, pure ASCII (see `_slug`)."""
|
||||
return _slug(name, limit, "untitled")
|
||||
|
||||
|
||||
def display_filename(name: str) -> str:
|
||||
"""Content-Disposition filename base — the same shell-safe slug policy as the on-disk name, just
|
||||
with a larger length cap (a served name may be longer than a path component)."""
|
||||
return _slug(name, _MAX_DISPLAY, "download")
|
||||
|
||||
|
||||
def rel_path(meta: MediaMeta, ext: str, layout: str = "plex", asset_id: int | None = None) -> str:
|
||||
@@ -116,14 +102,16 @@ def rel_path(meta: MediaMeta, ext: str, layout: str = "plex", asset_id: int | No
|
||||
either removes the shared file. The `_a{id}` suffix (which Plex ignores — the .nfo sidecar carries
|
||||
the metadata) keeps them apart. Optional so callers/tests that don't need it keep the plain name."""
|
||||
title = sanitize(meta.title)
|
||||
vid = meta.video_id
|
||||
# The id goes into the name bare (no brackets — `[`/`]` are shell glob metacharacters); keep it
|
||||
# shell-safe and bounded in case a URL source has no clean extractor id.
|
||||
vid = _UNSAFE.sub("_", str(meta.video_id))[:40]
|
||||
disc = f"_a{asset_id}" if asset_id is not None else ""
|
||||
if layout == "flat":
|
||||
return f"{title}_[{vid}]{disc}.{ext}"
|
||||
return f"{title}_{vid}{disc}.{ext}"
|
||||
channel = sanitize(meta.uploader or "Unknown_Channel", 80)
|
||||
year = meta.upload_date.year if meta.upload_date else 0
|
||||
d = meta.upload_date.isoformat() if meta.upload_date else "0000-00-00"
|
||||
epname = f"{channel}_-_{d}_-_{title}_[{vid}]{disc}.{ext}"
|
||||
epname = f"{channel}_-_{d}_-_{title}_{vid}{disc}.{ext}"
|
||||
return f"{channel}/Season_{year}/{epname}"
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user