fix(clips): precompose accented titles so the file name survives a fetch()
"canción" has two Unicode spellings: a precomposed ó, or an o followed by a combining acute. yt-dlp hands over titles in either, and sanitize_filename let the decomposed form through into the clip file, the R2 key and the URL path. Measured against prod on 24-ago-2026, on a clip whose title carried the combining form: a <video> element loads it fine, and fetch() of the very same URL comes back 503, while curl gets 206 with the identical headers. So the download button, which fetches, was broken on effectively every Spanish title, and playback hid it. Normalising at sanitize_filename fixes the whole chain at its source: one name for both spellings, and a no-op for the ASCII names that already worked. Clips already archived keep their decomposed name and still play.
This commit is contained in:
parent
080dd5780b
commit
c6ddb40630
2 changed files with 24 additions and 0 deletions
9
main.py
9
main.py
|
|
@ -5,6 +5,7 @@ import argparse
|
||||||
import re
|
import re
|
||||||
import sys
|
import sys
|
||||||
import threading
|
import threading
|
||||||
|
import unicodedata
|
||||||
import uuid
|
import uuid
|
||||||
from concurrent.futures import ThreadPoolExecutor, as_completed
|
from concurrent.futures import ThreadPoolExecutor, as_completed
|
||||||
from scenedetect import open_video, SceneManager
|
from scenedetect import open_video, SceneManager
|
||||||
|
|
@ -599,6 +600,14 @@ def truncate_bytes(text, max_bytes):
|
||||||
|
|
||||||
def sanitize_filename(filename):
|
def sanitize_filename(filename):
|
||||||
"""Remove invalid characters from filename and bound it for the filesystem."""
|
"""Remove invalid characters from filename and bound it for the filesystem."""
|
||||||
|
# "canción" has two Unicode spellings: a precomposed ó (NFC) or an o plus a
|
||||||
|
# combining acute (NFD). yt-dlp hands over titles in either, and the name
|
||||||
|
# becomes the clip file, the R2 key and the URL path. Measured 24-ago-2026:
|
||||||
|
# a key carrying the combining form is fetchable by a <video> element but a
|
||||||
|
# fetch() of the same URL comes back 503, which broke the download button on
|
||||||
|
# every clip with a Spanish title. Normalising here fixes the whole chain at
|
||||||
|
# its source, and is a no-op for the ASCII names that already worked.
|
||||||
|
filename = unicodedata.normalize('NFC', filename)
|
||||||
filename = re.sub(r'[<>:"/\\|?*#]', '', filename)
|
filename = re.sub(r'[<>:"/\\|?*#]', '', filename)
|
||||||
filename = filename.replace(' ', '_')
|
filename = filename.replace(' ', '_')
|
||||||
return truncate_bytes(filename, MAX_TITLE_BYTES)
|
return truncate_bytes(filename, MAX_TITLE_BYTES)
|
||||||
|
|
|
||||||
|
|
@ -52,6 +52,21 @@ class TestSanitizeFilename:
|
||||||
assert main.sanitize_filename('a<b>c:d"e/f\\g|h?i*j#k') == "abcdefghijk"
|
assert main.sanitize_filename('a<b>c:d"e/f\\g|h?i*j#k') == "abcdefghijk"
|
||||||
assert main.sanitize_filename("with spaces") == "with_spaces"
|
assert main.sanitize_filename("with spaces") == "with_spaces"
|
||||||
|
|
||||||
|
def test_combining_accents_are_precomposed(self):
|
||||||
|
"""A Spanish title arrives from yt-dlp in either Unicode spelling.
|
||||||
|
|
||||||
|
The decomposed form (o + U+0301) survived into the R2 key and the URL,
|
||||||
|
where a fetch() of the object came back 503 while a <video> element
|
||||||
|
loaded it fine — the download button broke on every accented title
|
||||||
|
(24-ago-2026). Both spellings must produce the same, precomposed name.
|
||||||
|
"""
|
||||||
|
decomposed = "cancio\u0301n" # o + combining acute
|
||||||
|
precomposed = "canci\u00f3n" # single ó
|
||||||
|
assert decomposed != precomposed
|
||||||
|
assert main.sanitize_filename(decomposed) == precomposed
|
||||||
|
assert main.sanitize_filename(precomposed) == precomposed
|
||||||
|
assert "\u0301" not in main.sanitize_filename("Ha\u0301bitos_Nº1")
|
||||||
|
|
||||||
def test_the_old_character_cap_would_have_overflowed(self):
|
def test_the_old_character_cap_would_have_overflowed(self):
|
||||||
# Documents why the rule changed: 100 chars of Bengali is ~300 bytes.
|
# Documents why the rule changed: 100 chars of Bengali is ~300 bytes.
|
||||||
assert len((BENGALI * 4)[:100].encode("utf-8")) > FS_LIMIT
|
assert len((BENGALI * 4)[:100].encode("utf-8")) > FS_LIMIT
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue