From 67166861e954e8f899374e4b223ffa11926142e2 Mon Sep 17 00:00:00 2001 From: Robert Honz Date: Sun, 21 Apr 2024 17:35:22 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20`pathvalidate`=20library=20is=20cur?= =?UTF-8?q?rently=20not=20able=20to=20shorten=20filenames.=20Implemented?= =?UTF-8?q?=20a=20hacky=20workaround=20for=20now.=20Fixes=20#91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tidal_dl_ng/helper/path.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/tidal_dl_ng/helper/path.py b/tidal_dl_ng/helper/path.py index e857801..b79d08e 100644 --- a/tidal_dl_ng/helper/path.py +++ b/tidal_dl_ng/helper/path.py @@ -207,9 +207,17 @@ def path_file_sanitize(path_file: str, adapt: bool = False, uniquify: bool = Fal filename_sanitized = filename_sanitized[: -len(file_suffix)] + file_suffix except ValidationError as e: # TODO: Implement proper exception handling and logging. - print(e) - - raise + # print(e) + # Hacky stuff, since the sanitizing function does not shorten the filename somehow (bug?) + # Remove after pathvalidate update. + # If filename too long + if e.description.startswith("[PV1101]"): + byte_ct: int = len(filename.encode("utf-8")) - 255 + filename_sanitized = ( + filename[: -byte_ct - len(FILENAME_SANITIZE_PLACEHOLDER) - len(file_extension)] + + FILENAME_SANITIZE_PLACEHOLDER + + file_extension + ) # Join path and filename result: str = os.path.join(pathname_sanitized, filename_sanitized)