Skip to content

Commit 6ac116b

Browse files
committed
🛠️ Fixed and simplified path_file_sanitize method.
1 parent f4b40b1 commit 6ac116b

File tree

2 files changed

+38
-55
lines changed

2 files changed

+38
-55
lines changed

tidal_dl_ng/download.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -334,9 +334,7 @@ def item(
334334
if media_id and media_type:
335335
# If no media instance is provided, we need to create the media instance.
336336
media = instantiate_media(self.session, media_type, media_id)
337-
elif isinstance(media, Track) or isinstance(
338-
media, Video
339-
): # Check if media is available not deactivated / removed from TIDAL.
337+
elif isinstance(media, Track | Video): # Check if media is available not deactivated / removed from TIDAL.
340338
if not media.available:
341339
self.fn_logger.info(
342340
f"This item is not available for listening anymore on TIDAL. Skipping: {name_builder_item(media)}"

tidal_dl_ng/helper/path.py

Lines changed: 37 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -205,61 +205,60 @@ def get_format_template(
205205

206206

207207
def path_file_sanitize(path_file: pathlib.Path, adapt: bool = False, uniquify: bool = False) -> pathlib.Path:
208-
# Get each directory name separately (first value in tuple; second value is for the file suffix).
209-
to_sanitize: [[str, str]] = path_split_parts_suffix(path_file)
210-
sanitized_path_file: pathlib.Path = pathlib.Path(to_sanitize.pop(0)[0])
211-
212-
for name, suffix in to_sanitize:
213-
# Sanitize names: We need first top make sure that none file / directory name has bad chars or is longer than 255 chars.
214-
try:
215-
# sanitize_filename can shorten the file name actually
216-
filename_sanitized: str = sanitize_filename(
217-
name + suffix, replacement_text=" ", validate_after_sanitize=True, platform="auto"
218-
)
219-
220-
# Check if the file extension was removed by shortening the filename length
221-
if not filename_sanitized.endswith(suffix):
222-
# Add the original file extension
223-
file_suffix: str = FILENAME_SANITIZE_PLACEHOLDER + path_file.suffix
224-
filename_sanitized = filename_sanitized[: -len(file_suffix)] + file_suffix
225-
except ValidationError as e:
226-
if adapt:
227-
# TODO: Implement proper exception handling and logging.
228-
# Hacky stuff, since the sanitizing function does not shorten the filename (filename too long)
229-
if str(e).startswith("[PV1101]"):
230-
byte_ct: int = len(name.encode(sys.getfilesystemencoding())) - FILENAME_LENGTH_MAX
231-
filename_sanitized = (
232-
name[: -byte_ct - len(FILENAME_SANITIZE_PLACEHOLDER) - len(suffix)]
233-
+ FILENAME_SANITIZE_PLACEHOLDER
234-
+ suffix
235-
)
236-
else:
237-
raise
208+
sanitized_filename: str = path_file.name
209+
sanitized_path: pathlib.Path = path_file.parent
210+
result: pathlib.Path
211+
212+
# Sanitize filename and make sure it does not exceed FILENAME_LENGTH_MAX
213+
try:
214+
# sanitize_filename can shorten the file name actually
215+
sanitized_filename = sanitize_filename(
216+
sanitized_filename, replacement_text="_", validate_after_sanitize=True, platform="auto"
217+
)
218+
219+
# Check if the file extension was removed by shortening the filename length
220+
if not sanitized_filename.endswith(path_file.suffix):
221+
# Add the original file extension
222+
file_suffix: str = FILENAME_SANITIZE_PLACEHOLDER + path_file.suffix
223+
sanitized_filename = sanitized_filename[: -len(file_suffix)] + file_suffix
224+
except ValidationError as e:
225+
if adapt:
226+
# TODO: Implement proper exception handling and logging.
227+
# Hacky stuff, since the sanitizing function does not shorten the filename (filename too long)
228+
if str(e).startswith("[PV1101]"):
229+
byte_ct: int = len(sanitized_filename.encode(sys.getfilesystemencoding())) - FILENAME_LENGTH_MAX
230+
sanitized_filename = (
231+
sanitized_filename[: -byte_ct - len(FILENAME_SANITIZE_PLACEHOLDER) - len(path_file.suffix)]
232+
+ FILENAME_SANITIZE_PLACEHOLDER
233+
+ path_file.suffix
234+
)
238235
else:
239236
raise
240-
finally:
241-
sanitized_path_file = sanitized_path_file / filename_sanitized
237+
else:
238+
raise
242239

243-
# Sanitize the whole path. The whole path with filename is not allowed to be longer then the max path length depending on the OS.
240+
# Sanitize the path.
244241
try:
245-
sanitized_path_file: pathlib.Path = sanitize_filepath(
246-
sanitized_path_file, replacement_text=" ", validate_after_sanitize=True, platform="auto"
242+
sanitized_path: pathlib.Path = sanitize_filepath(
243+
sanitized_path, replacement_text="_", validate_after_sanitize=True, platform="auto"
247244
)
248245
except ValidationError as e:
249246
# If adaption of path is allowed in case of an error set path to HOME.
250247
if adapt:
251248
if str(e).startswith("[PV1101]"):
252-
sanitized_path_file = pathlib.Path.home() / sanitized_path_file.name
249+
sanitized_path = pathlib.Path.home()
253250
else:
254251
raise
255252
else:
256253
raise
257254

255+
result = sanitized_path / sanitized_filename
256+
258257
# Uniquify
259258
if uniquify:
260-
sanitized_path_file = path_file_uniquify(sanitized_path_file)
259+
result = path_file_uniquify(result)
261260

262-
return sanitized_path_file
261+
return result
263262

264263

265264
def path_file_uniquify(path_file: pathlib.Path) -> pathlib.Path:
@@ -340,17 +339,3 @@ def url_to_filename(url: str) -> str:
340339
raise ValueError # reject '%2f' or 'dir%5Cbasename.ext' on Windows
341340

342341
return basename
343-
344-
345-
def path_split_parts_suffix(p: pathlib.Path) -> [[str, str]]:
346-
"""Splits the path to file in parts and also splits the suffix from the file stem.
347-
348-
:param p: Path to file which should be split in parts.
349-
:type p: pathlib.Path
350-
:return: List of tuples (Stem, Suffix) of each path part.
351-
"""
352-
result: [[str, str]] = [[part, ""] for part in p.parts]
353-
result[-1][0] = p.stem
354-
result[-1][-1] = p.suffix
355-
356-
return result

0 commit comments

Comments
 (0)