2023-10-22 09:07:17 +02:00
|
|
|
from string import printable
|
2023-10-13 20:38:50 +02:00
|
|
|
|
2024-01-24 02:57:13 +01:00
|
|
|
from pathvalidate import sanitize_filename, sanitize_filepath # type: ignore
|
2023-10-13 20:38:50 +02:00
|
|
|
|
2023-10-22 09:07:17 +02:00
|
|
|
ALLOWED_CHARS = set(printable)
|
2023-10-13 20:38:50 +02:00
|
|
|
|
2023-10-22 09:07:17 +02:00
|
|
|
|
|
|
|
def clean_filename(fn: str, restrict: bool = False) -> str:
|
2023-10-13 20:38:50 +02:00
|
|
|
path = str(sanitize_filename(fn))
|
|
|
|
if restrict:
|
2023-10-22 09:07:17 +02:00
|
|
|
path = "".join(c for c in path if c in ALLOWED_CHARS)
|
2023-10-13 20:38:50 +02:00
|
|
|
|
|
|
|
return path
|
2024-01-23 01:13:02 +01:00
|
|
|
|
2024-01-24 02:57:13 +01:00
|
|
|
|
2024-01-23 01:13:02 +01:00
|
|
|
def clean_filepath(fn: str, restrict: bool = False) -> str:
|
|
|
|
path = str(sanitize_filepath(fn))
|
|
|
|
if restrict:
|
|
|
|
path = "".join(c for c in path if c in ALLOWED_CHARS)
|
|
|
|
|
|
|
|
return path
|