56 lines
1.3 KiB
Python
56 lines
1.3 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
Utility functions for ParentZone Downloader
|
|
|
|
This module contains shared utility functions used across multiple modules.
|
|
"""
|
|
|
|
|
|
def sanitize_filename(filename: str) -> str:
|
|
"""
|
|
Sanitize filename by removing invalid characters.
|
|
|
|
Args:
|
|
filename: The filename to sanitize
|
|
|
|
Returns:
|
|
Sanitized filename safe for filesystem use
|
|
"""
|
|
# Remove or replace invalid characters
|
|
invalid_chars = '<>:"/\\|?*'
|
|
for char in invalid_chars:
|
|
filename = filename.replace(char, "_")
|
|
|
|
# Remove leading/trailing spaces and dots
|
|
filename = filename.strip(". ")
|
|
|
|
# Ensure filename is not empty
|
|
if not filename:
|
|
filename = "file"
|
|
|
|
return filename
|
|
|
|
|
|
def get_extension_from_mime(mime_type: str) -> str:
|
|
"""
|
|
Get file extension from MIME type.
|
|
|
|
Args:
|
|
mime_type: The MIME type string (e.g., 'image/jpeg')
|
|
|
|
Returns:
|
|
File extension including the dot (e.g., '.jpg')
|
|
"""
|
|
mime_to_ext = {
|
|
"image/jpeg": ".jpg",
|
|
"image/jpg": ".jpg",
|
|
"image/png": ".png",
|
|
"image/gif": ".gif",
|
|
"image/webp": ".webp",
|
|
"image/bmp": ".bmp",
|
|
"image/tiff": ".tiff",
|
|
"image/svg+xml": ".svg",
|
|
}
|
|
return mime_to_ext.get(mime_type.lower(), ".jpg")
|
|
|