|
13 | 13 | from tidalapi.media import AudioExtensions
|
14 | 14 |
|
15 | 15 | 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 | +) |
17 | 23 | from tidal_dl_ng.helper.tidal import name_builder_album_artist, name_builder_artist, name_builder_title
|
18 | 24 |
|
19 | 25 |
|
@@ -68,7 +74,11 @@ def format_path_media(
|
68 | 74 | result_fmt = format_str_media(match.group(1), media, album_track_num_pad_min, list_pos, list_total)
|
69 | 75 |
|
70 | 76 | 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 | + ) |
72 | 82 | result = result.replace(template_str, value)
|
73 | 83 |
|
74 | 84 | return result
|
@@ -164,10 +174,10 @@ def format_str_media(
|
164 | 174 | result = ", ".join(tag for tag in media.media_metadata_tags)
|
165 | 175 | case "track_explicit":
|
166 | 176 | if isinstance(media, Track | Video):
|
167 |
| - result = " (Explicit)" if media.explicit else "" |
| 177 | + result = FORMAT_TEMPLATE_EXPLICIT if media.explicit else "" |
168 | 178 | case "album_explicit":
|
169 | 179 | if isinstance(media, Album):
|
170 |
| - result = " (Explicit)" if media.explicit else "" |
| 180 | + result = FORMAT_TEMPLATE_EXPLICIT if media.explicit else "" |
171 | 181 | case "album_num_volumes":
|
172 | 182 | if isinstance(media, Album):
|
173 | 183 | result = str(media.num_volumes)
|
@@ -264,6 +274,19 @@ def path_file_sanitize(path_file: pathlib.Path, adapt: bool = False, uniquify: b
|
264 | 274 | raise
|
265 | 275 |
|
266 | 276 | # 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'. |
267 | 290 | try:
|
268 | 291 | sanitized_path: pathlib.Path = sanitize_filepath(
|
269 | 292 | sanitized_path, replacement_text="_", validate_after_sanitize=True, platform="auto"
|
|
0 commit comments