2023-10-21 20:30:47 +01:00
|
|
|
import unicodedata
|
|
|
|
from pathlib import PurePosixPath
|
|
|
|
|
2023-10-23 02:51:39 +01:00
|
|
|
from pathvalidate import sanitize_filepath
|
|
|
|
|
|
|
|
|
2023-10-21 20:30:47 +01:00
|
|
|
def sanitize(filename: str) -> str:
|
|
|
|
filename = unicodedata.normalize("NFC", filename)
|
|
|
|
# UNIX filenames can contain backslashes but for compatibility we replace them with dashes
|
|
|
|
filename = filename.replace("\\", "-")
|
|
|
|
filename = sanitize_filepath(filename)
|
|
|
|
filename = filename.strip("/")
|
2023-11-08 00:47:41 +00:00
|
|
|
p = PurePosixPath(filename)
|
|
|
|
if any(n.startswith(".") for n in p.parts):
|
|
|
|
raise ValueError("Filenames starting with dot are not allowed")
|
|
|
|
return p.as_posix()
|