Skip to content

Commit 28f15e0

Browse files
authored
Merge pull request #471 from exislow/447-character-limits-on-album-name
🛠️ Directory length limit is checked and adapted correctly now. Fixes #447 & #436
2 parents 0eaeed5 + 70e2977 commit 28f15e0

File tree

3 files changed

+35
-8
lines changed

3 files changed

+35
-8
lines changed

tidal_dl_ng/constants.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
PLAYLIST_EXTENSION: str = ".m3u"
1313
PLAYLIST_PREFIX: str = "_"
1414
FILENAME_LENGTH_MAX: int = 255
15+
FORMAT_TEMPLATE_EXPLICIT: str = " (Explicit)"
1516

1617

1718
class QualityVideo(StrEnum):

tidal_dl_ng/download.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import m3u8
1313
import requests
1414
from ffmpeg import FFmpeg
15+
from pathvalidate import sanitize_filename
1516
from requests.adapters import HTTPAdapter, Retry
1617
from requests.exceptions import HTTPError
1718
from rich.progress import Progress, TaskID
@@ -36,7 +37,6 @@
3637
check_file_exists,
3738
format_path_media,
3839
path_file_sanitize,
39-
sanitize_filename,
4040
url_to_filename,
4141
)
4242
from tidal_dl_ng.helper.tidal import (
@@ -437,9 +437,12 @@ def item(
437437
elif isinstance(media, Video):
438438
file_extension = AudioExtensions.MP4 if self.settings.data.video_convert_mp4 else VideoExtensions.TS
439439

440-
# Compute file name, sanitize once again and create destination directory
441-
path_media_dst = path_media_dst.with_suffix(file_extension)
442-
path_media_dst = pathlib.Path(path_file_sanitize(path_media_dst, adapt=True))
440+
# If file extension was guessed wrong in the beginning
441+
if path_media_dst.suffix != file_extension:
442+
# Compute file name, sanitize once again, because file extension could have been replaced after guessing it first and create destination directory
443+
path_media_dst = path_media_dst.with_suffix(file_extension)
444+
path_media_dst = pathlib.Path(path_file_sanitize(path_media_dst, adapt=True))
445+
443446
os.makedirs(path_media_dst.parent, exist_ok=True)
444447

445448
if not skip_download:

tidal_dl_ng/helper/path.py

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,13 @@
1313
from tidalapi.media import AudioExtensions
1414

1515
from tidal_dl_ng import __name_display__
16-
from tidal_dl_ng.constants import FILENAME_LENGTH_MAX, FILENAME_SANITIZE_PLACEHOLDER, UNIQUIFY_THRESHOLD, MediaType
16+
from tidal_dl_ng.constants import (
17+
FILENAME_LENGTH_MAX,
18+
FILENAME_SANITIZE_PLACEHOLDER,
19+
FORMAT_TEMPLATE_EXPLICIT,
20+
UNIQUIFY_THRESHOLD,
21+
MediaType,
22+
)
1723
from tidal_dl_ng.helper.tidal import name_builder_album_artist, name_builder_artist, name_builder_title
1824

1925

@@ -68,7 +74,11 @@ def format_path_media(
6874
result_fmt = format_str_media(match.group(1), media, album_track_num_pad_min, list_pos, list_total)
6975

7076
if result_fmt != match.group(1):
71-
value = sanitize_filename(result_fmt)
77+
# Sanitize here, in case of the filename has slashes or something, which will be recognized later as a directory separator.
78+
# Do not sanitize if value is the FORMAT_TEMPLATE_EXPLICIT placeholder, since it has a leading whitespace which otherwise gets removed.
79+
value = (
80+
sanitize_filename(result_fmt) if result_fmt != FORMAT_TEMPLATE_EXPLICIT else FORMAT_TEMPLATE_EXPLICIT
81+
)
7282
result = result.replace(template_str, value)
7383

7484
return result
@@ -164,10 +174,10 @@ def format_str_media(
164174
result = ", ".join(tag for tag in media.media_metadata_tags)
165175
case "track_explicit":
166176
if isinstance(media, Track | Video):
167-
result = " (Explicit)" if media.explicit else ""
177+
result = FORMAT_TEMPLATE_EXPLICIT if media.explicit else ""
168178
case "album_explicit":
169179
if isinstance(media, Album):
170-
result = " (Explicit)" if media.explicit else ""
180+
result = FORMAT_TEMPLATE_EXPLICIT if media.explicit else ""
171181
case "album_num_volumes":
172182
if isinstance(media, Album):
173183
result = str(media.num_volumes)
@@ -264,6 +274,19 @@ def path_file_sanitize(path_file: pathlib.Path, adapt: bool = False, uniquify: b
264274
raise
265275

266276
# Sanitize the path.
277+
# First sanitize sanitize each part of the path. Each part of the path is not allowed to be longer then 'PC_NAME_MAX'.
278+
sanitized_parts = []
279+
280+
for part in sanitized_path.parts:
281+
if part in sanitized_path.root:
282+
sanitized_parts.append(part)
283+
else:
284+
sanitized_parts.append(
285+
sanitize_filename(part, replacement_text="_", validate_after_sanitize=True, platform="auto")
286+
)
287+
sanitized_path = pathlib.Path(*sanitized_parts)
288+
289+
# Then sanitize the whole path itself. The whole path is not allowed to be longer than 'PC_NAME_MAX'.
267290
try:
268291
sanitized_path: pathlib.Path = sanitize_filepath(
269292
sanitized_path, replacement_text="_", validate_after_sanitize=True, platform="auto"

0 commit comments

Comments
 (0)