Auto-update: Thu Aug 8 05:25:50 PDT 2024

This commit is contained in:
sanj 2024-08-08 05:25:50 -07:00
parent b45553e954
commit 6cf9b5c3e4

View file

@ -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."""