🛠️ Fixed and simplified path_file_sanitize method.
This commit is contained in:
@@ -334,9 +334,7 @@ class Download:
|
|||||||
if media_id and media_type:
|
if media_id and media_type:
|
||||||
# If no media instance is provided, we need to create the media instance.
|
# If no media instance is provided, we need to create the media instance.
|
||||||
media = instantiate_media(self.session, media_type, media_id)
|
media = instantiate_media(self.session, media_type, media_id)
|
||||||
elif isinstance(media, Track) or isinstance(
|
elif isinstance(media, Track | Video): # Check if media is available not deactivated / removed from TIDAL.
|
||||||
media, Video
|
|
||||||
): # Check if media is available not deactivated / removed from TIDAL.
|
|
||||||
if not media.available:
|
if not media.available:
|
||||||
self.fn_logger.info(
|
self.fn_logger.info(
|
||||||
f"This item is not available for listening anymore on TIDAL. Skipping: {name_builder_item(media)}"
|
f"This item is not available for listening anymore on TIDAL. Skipping: {name_builder_item(media)}"
|
||||||
|
|||||||
+20
-35
@@ -205,61 +205,60 @@ def get_format_template(
|
|||||||
|
|
||||||
|
|
||||||
def path_file_sanitize(path_file: pathlib.Path, adapt: bool = False, uniquify: bool = False) -> pathlib.Path:
|
def path_file_sanitize(path_file: pathlib.Path, adapt: bool = False, uniquify: bool = False) -> pathlib.Path:
|
||||||
# Get each directory name separately (first value in tuple; second value is for the file suffix).
|
sanitized_filename: str = path_file.name
|
||||||
to_sanitize: [[str, str]] = path_split_parts_suffix(path_file)
|
sanitized_path: pathlib.Path = path_file.parent
|
||||||
sanitized_path_file: pathlib.Path = pathlib.Path(to_sanitize.pop(0)[0])
|
result: pathlib.Path
|
||||||
|
|
||||||
for name, suffix in to_sanitize:
|
# Sanitize filename and make sure it does not exceed FILENAME_LENGTH_MAX
|
||||||
# Sanitize names: We need first top make sure that none file / directory name has bad chars or is longer than 255 chars.
|
|
||||||
try:
|
try:
|
||||||
# sanitize_filename can shorten the file name actually
|
# sanitize_filename can shorten the file name actually
|
||||||
filename_sanitized: str = sanitize_filename(
|
sanitized_filename = sanitize_filename(
|
||||||
name + suffix, replacement_text=" ", validate_after_sanitize=True, platform="auto"
|
sanitized_filename, replacement_text="_", validate_after_sanitize=True, platform="auto"
|
||||||
)
|
)
|
||||||
|
|
||||||
# Check if the file extension was removed by shortening the filename length
|
# Check if the file extension was removed by shortening the filename length
|
||||||
if not filename_sanitized.endswith(suffix):
|
if not sanitized_filename.endswith(path_file.suffix):
|
||||||
# Add the original file extension
|
# Add the original file extension
|
||||||
file_suffix: str = FILENAME_SANITIZE_PLACEHOLDER + path_file.suffix
|
file_suffix: str = FILENAME_SANITIZE_PLACEHOLDER + path_file.suffix
|
||||||
filename_sanitized = filename_sanitized[: -len(file_suffix)] + file_suffix
|
sanitized_filename = sanitized_filename[: -len(file_suffix)] + file_suffix
|
||||||
except ValidationError as e:
|
except ValidationError as e:
|
||||||
if adapt:
|
if adapt:
|
||||||
# TODO: Implement proper exception handling and logging.
|
# TODO: Implement proper exception handling and logging.
|
||||||
# Hacky stuff, since the sanitizing function does not shorten the filename (filename too long)
|
# Hacky stuff, since the sanitizing function does not shorten the filename (filename too long)
|
||||||
if str(e).startswith("[PV1101]"):
|
if str(e).startswith("[PV1101]"):
|
||||||
byte_ct: int = len(name.encode(sys.getfilesystemencoding())) - FILENAME_LENGTH_MAX
|
byte_ct: int = len(sanitized_filename.encode(sys.getfilesystemencoding())) - FILENAME_LENGTH_MAX
|
||||||
filename_sanitized = (
|
sanitized_filename = (
|
||||||
name[: -byte_ct - len(FILENAME_SANITIZE_PLACEHOLDER) - len(suffix)]
|
sanitized_filename[: -byte_ct - len(FILENAME_SANITIZE_PLACEHOLDER) - len(path_file.suffix)]
|
||||||
+ FILENAME_SANITIZE_PLACEHOLDER
|
+ FILENAME_SANITIZE_PLACEHOLDER
|
||||||
+ suffix
|
+ path_file.suffix
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
raise
|
raise
|
||||||
else:
|
else:
|
||||||
raise
|
raise
|
||||||
finally:
|
|
||||||
sanitized_path_file = sanitized_path_file / filename_sanitized
|
|
||||||
|
|
||||||
# Sanitize the whole path. The whole path with filename is not allowed to be longer then the max path length depending on the OS.
|
# Sanitize the path.
|
||||||
try:
|
try:
|
||||||
sanitized_path_file: pathlib.Path = sanitize_filepath(
|
sanitized_path: pathlib.Path = sanitize_filepath(
|
||||||
sanitized_path_file, replacement_text=" ", validate_after_sanitize=True, platform="auto"
|
sanitized_path, replacement_text="_", validate_after_sanitize=True, platform="auto"
|
||||||
)
|
)
|
||||||
except ValidationError as e:
|
except ValidationError as e:
|
||||||
# If adaption of path is allowed in case of an error set path to HOME.
|
# If adaption of path is allowed in case of an error set path to HOME.
|
||||||
if adapt:
|
if adapt:
|
||||||
if str(e).startswith("[PV1101]"):
|
if str(e).startswith("[PV1101]"):
|
||||||
sanitized_path_file = pathlib.Path.home() / sanitized_path_file.name
|
sanitized_path = pathlib.Path.home()
|
||||||
else:
|
else:
|
||||||
raise
|
raise
|
||||||
else:
|
else:
|
||||||
raise
|
raise
|
||||||
|
|
||||||
|
result = sanitized_path / sanitized_filename
|
||||||
|
|
||||||
# Uniquify
|
# Uniquify
|
||||||
if uniquify:
|
if uniquify:
|
||||||
sanitized_path_file = path_file_uniquify(sanitized_path_file)
|
result = path_file_uniquify(result)
|
||||||
|
|
||||||
return sanitized_path_file
|
return result
|
||||||
|
|
||||||
|
|
||||||
def path_file_uniquify(path_file: pathlib.Path) -> pathlib.Path:
|
def path_file_uniquify(path_file: pathlib.Path) -> pathlib.Path:
|
||||||
@@ -340,17 +339,3 @@ def url_to_filename(url: str) -> str:
|
|||||||
raise ValueError # reject '%2f' or 'dir%5Cbasename.ext' on Windows
|
raise ValueError # reject '%2f' or 'dir%5Cbasename.ext' on Windows
|
||||||
|
|
||||||
return basename
|
return basename
|
||||||
|
|
||||||
|
|
||||||
def path_split_parts_suffix(p: pathlib.Path) -> [[str, str]]:
|
|
||||||
"""Splits the path to file in parts and also splits the suffix from the file stem.
|
|
||||||
|
|
||||||
:param p: Path to file which should be split in parts.
|
|
||||||
:type p: pathlib.Path
|
|
||||||
:return: List of tuples (Stem, Suffix) of each path part.
|
|
||||||
"""
|
|
||||||
result: [[str, str]] = [[part, ""] for part in p.parts]
|
|
||||||
result[-1][0] = p.stem
|
|
||||||
result[-1][-1] = p.suffix
|
|
||||||
|
|
||||||
return result
|
|
||||||
|
|||||||
Reference in New Issue
Block a user