Skip to content

Commit

Permalink
christen: prevent utf8 slash from turning into subfolders
Browse files Browse the repository at this point in the history
  • Loading branch information
chapmanjacobd committed Mar 4, 2025
1 parent 49ecda8 commit a781094
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 6 deletions.
3 changes: 1 addition & 2 deletions library/playback/torrents_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -549,13 +549,12 @@ def set_download_path(t):
qbt_client.torrents_recheck(torrent_hashes=torrent_hashes)

if args.export:
print("Exporting", len(torrents))
p = Path("exported_torrents")
p.mkdir(exist_ok=True)
for idx, t in enumerate(torrents):
printing.print_overwrite("Exporting", idx + 1, "of", len(torrents), "to", p)

file_name = f"{t.tracker_domain()}_{t.name}_{t.hash}.torrent".replace("/", "-").replace("\\", "-")
file_name = f"{t.tracker_domain()}_{t.name}_{t.hash}.torrent"
file_name = path_utils.clean_path(file_name.encode())
(p / file_name).write_bytes(qbt_client.torrents_export(torrent_hash=t.hash))

Expand Down
5 changes: 4 additions & 1 deletion library/utils/path_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ def clean_path(b, max_name_len=255, dot_space=False, case_insensitive=False, low
import ftfy

p = b.decode("utf-8", "backslashreplace")
p = ftfy.fix_text(p, explain=False)
path = Path(p)
ext = path.suffix

Expand All @@ -55,6 +54,10 @@ def clean_path(b, max_name_len=255, dot_space=False, case_insensitive=False, low
stem = strings.clean_string(path.stem)
# log.debug("cleaned %s %s", parent, stem)

parent = [ftfy.fix_text(s, explain=False) for s in parent]
stem = ftfy.fix_text(stem, explain=False)
# log.debug("ftfy %s %s", parent, stem)

parent = [strings.remove_prefixes(part, [" ", "-"]) for part in parent]
# log.debug("parent_prefixes %s %s", parent, stem)
parent = [strings.remove_suffixes(part, [" ", "-", "_", "."]) for part in parent]
Expand Down
11 changes: 9 additions & 2 deletions library/utils/strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,15 @@ def clean_string(p) -> str:
.replace(":", "")
.replace(">", "")
.replace("<", "")
.replace("\\", "")
.replace("/", "")
.replace("\\", " ")
.replace("/", " ")
.replace("/", " ")
.replace("∕", " ")
.replace("⁄", " ")
.replace("﹨", " ")
.replace("\", " ")
.replace("∖", " ")
.replace("﹨", " ")
)
p = remove_consecutives(p, chars=["."])
p = (
Expand Down
2 changes: 1 addition & 1 deletion tests/utils/test_path_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def test_clean_path():
if consts.IS_WINDOWS:
assert path_utils.clean_path(b"test/\xff\xfeH") == utils.p("test\\xff\\xfeH")
else:
assert path_utils.clean_path(b"test/\xff\xfeH") == utils.p("test/xffxfeH")
assert path_utils.clean_path(b"test/\xff\xfeH") == utils.p("test/xff xfeH")


@mock.patch("library.utils.consts.random_string", return_value="abcdef")
Expand Down

0 comments on commit a781094

Please sign in to comment.