feat(download screen):implement download capabilities

This commit is contained in:
Benex254
2024-06-12 16:06:45 +03:00
parent 018bccbaab
commit ef0083fa5a
11 changed files with 172 additions and 49 deletions

View File

@@ -1,7 +1,7 @@
import os
import shutil
from datetime import datetime
import re
# TODO: make it use color_text instead of fixed vals
# from .kivy_markup_helper import color_text
@@ -34,3 +34,62 @@ def move_file(source_path, dest_path):
return (1, path)
except Exception as e:
return (0, e)
def sanitize_filename(filename: str):
"""
Sanitize a string to be safe for use as a file name.
:param filename: The original filename string.
:return: A sanitized filename string.
"""
# List of characters not allowed in filenames on various operating systems
invalid_chars = r'[<>:"/\\|?*\0]'
reserved_names = {
"CON",
"PRN",
"AUX",
"NUL",
"COM1",
"COM2",
"COM3",
"COM4",
"COM5",
"COM6",
"COM7",
"COM8",
"COM9",
"LPT1",
"LPT2",
"LPT3",
"LPT4",
"LPT5",
"LPT6",
"LPT7",
"LPT8",
"LPT9",
}
# Replace invalid characters with an underscore
sanitized = re.sub(invalid_chars, "_", filename)
# Remove leading and trailing whitespace
sanitized = sanitized.strip()
# Check for reserved filenames
name, ext = os.path.splitext(sanitized)
if name.upper() in reserved_names:
name += "_file"
sanitized = name + ext
# Ensure the filename is not empty
if not sanitized:
sanitized = "default_filename"
return sanitized
# Example usage
unsafe_filename = "CON:example?file*name.txt"
safe_filename = sanitize_filename(unsafe_filename)
print(safe_filename) # Output: 'CON_example_file_name.txt'