From 6cf9b5c3e48c19dfd96d04c38ebe12a7d0552c17 Mon Sep 17 00:00:00 2001 From: sanj <67624670+iodrift@users.noreply.github.com> Date: Thu, 8 Aug 2024 05:25:50 -0700 Subject: [PATCH] Auto-update: Thu Aug 8 05:25:50 PDT 2024 --- sijapi/utilities.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/sijapi/utilities.py b/sijapi/utilities.py index 5b1182e..80e3f4f 100644 --- a/sijapi/utilities.py +++ b/sijapi/utilities.py @@ -237,10 +237,16 @@ def sanitize_filename(text, extension: str = None, max_length: int = MAX_PATH_LE """Sanitize a string to be used as a safe filename while protecting the file extension.""" debug(f"Filename before sanitization: {text}") + # Ensure text is a string + text = str(text) + text = re.sub(r'\s+', ' ', text) sanitized = re.sub(ALLOWED_FILENAME_CHARS, '', text) sanitized = sanitized.strip() - base_name, extension = os.path.splitext(sanitized) + base_name, file_extension = os.path.splitext(sanitized) + + # Use the provided extension if given, otherwise use the original + extension = extension or file_extension max_base_length = max_length - len(extension) if len(base_name) > max_base_length: @@ -251,6 +257,7 @@ def sanitize_filename(text, extension: str = None, max_length: int = MAX_PATH_LE return final_filename + def check_file_name(file_name, max_length=255): """Check if the file name needs sanitization based on the criteria of the second sanitize_filename function."""