fixes for file naming
formatting_check / formatting_check (push) Failing after 6s
Python Test / test (.sh, ubuntu-latest, 3.9) (push) Failing after 16s
Python Test / test (.ps1, windows-latest, 3.9) (push) Has been cancelled
Python Test / test (.sh, macos-latest, 3.9) (push) Has been cancelled

This commit is contained in:
2026-07-14 21:11:24 +12:00
parent 8f8e2c744d
commit f3dc8e46fd
16 changed files with 236 additions and 117 deletions
+37 -1
View File
@@ -36,6 +36,7 @@ class FileNameFormatter:
directory_format_string: str,
time_format_string: str,
restriction_scheme: Optional[str] = None,
strip_unicode: bool = True,
):
if not self.validate_string(file_format_string):
raise BulkDownloaderException(f'"{file_format_string}" is not a valid format string')
@@ -43,6 +44,7 @@ class FileNameFormatter:
self.directory_format_string: list[str] = directory_format_string.split("/")
self.time_format_string = time_format_string
self.restiction_scheme = restriction_scheme.lower().strip() if restriction_scheme else None
self.strip_unicode = strip_unicode
if self.restiction_scheme == "windows":
self.max_path = self.WINDOWS_MAX_PATH_LENGTH
else:
@@ -65,12 +67,22 @@ class FileNameFormatter:
result = result.replace("/", "")
# Strip Unicode characters that cause Windows SMB issues if enabled
if self.strip_unicode:
result = FileNameFormatter._strip_unicode_chars(result)
if self.restiction_scheme is None:
if platform.system() == "Windows":
result = FileNameFormatter._format_for_windows(result)
# Strip emojis on Windows if strip_unicode is enabled (for backward compatibility)
if self.strip_unicode:
result = FileNameFormatter._strip_emojis(result)
elif self.restiction_scheme == "windows":
logger.debug("Forcing Windows-compatible filenames")
result = FileNameFormatter._format_for_windows(result)
# Strip emojis when forcing Windows compatibility if strip_unicode is enabled
if self.strip_unicode:
result = FileNameFormatter._strip_emojis(result)
return result
@staticmethod
@@ -219,9 +231,33 @@ class FileNameFormatter:
invalid_characters = r'<>:"\/|?*'
for char in invalid_characters:
input_string = input_string.replace(char, "")
input_string = FileNameFormatter._strip_emojis(input_string)
return input_string
@staticmethod
def _strip_unicode_chars(input_string: str) -> str:
"""Strip Unicode characters that cause Windows SMB to create 8.3 short names"""
import unicodedata
# Remove emoji and symbols that cause Windows SMB issues
result = []
for char in input_string:
# Keep ASCII characters
if ord(char) < 0x80:
result.append(char)
# Keep common Unicode letters, numbers, and punctuation
elif unicodedata.category(char) in ['Lu', 'Ll', 'Lt', 'Lm', 'Lo', 'Nd', 'Nl', 'No', 'Pc', 'Pd', 'Ps', 'Pe', 'Pi', 'Pf', 'Po']:
result.append(char)
# Strip emoji, symbols, and other special characters that cause 8.3 names
elif unicodedata.category(char).startswith(('S', 'So', 'Sk', 'Sm')): # Symbols
continue
elif ord(char) > 0x1F000: # High Unicode ranges often contain emoji
continue
else:
# Keep other Unicode characters that are generally safe
result.append(char)
return ''.join(result)
@staticmethod
def _strip_emojis(input_string: str) -> str:
result = input_string.encode("ascii", errors="ignore").decode("utf-8")