Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • jplusplus/sanitize-filename
  • laggykiller/sanitize-filename
2 results
Show changes
Commits on Source (3)
......@@ -39,6 +39,14 @@ Examples:
## Changelog
- 1.1.0
- Try to preserve filename extensions if possible
- 1.0.1
- First release (as 1.0.1 due to a version number mix-up in 1.0.0)
- 1.0.0-dev3
- Black list low code point characters (<32)
......
"""A permissive filename sanitizer."""
import unicodedata
from os import path
def sanitize(filename):
......@@ -29,6 +30,12 @@ def sanitize(filename):
if len(filename) == 0:
filename = "__"
if len(filename) > 255:
filename = filename[:255]
filename = filename.rstrip(". ") # Re-check last character
(base, ext) = path.splitext(filename)
if len(ext) > 254:
ext = ext[254:]
maxl = 255 - len(ext)
filename = filename[:maxl]
filename = filename + ext
# Re-check last character (if there was no extension)
filename = filename.rstrip(". ")
return filename
......@@ -5,7 +5,7 @@ with open("README.md", "r") as fh:
setuptools.setup(
name="sanitize_filename",
version="1.0.1",
version="1.1.0",
author="Leo Wallentin | J++ Stockholm",
author_email="mejl@leowallentin.se",
description="A permissive filename sanitizer.",
......
......@@ -12,6 +12,7 @@ def test_invalid_suffix():
"""Dots are not allowed at the end."""
assert(sanitize("def.") == "def")
assert(sanitize("def.ghi") == "def.ghi")
assert(sanitize("X" * 1000 + ".").endswith("X"))
def test_reserved_words():
......@@ -23,8 +24,20 @@ def test_reserved_words():
def test_long_names():
"""Make sure long names are truncated."""
assert(len(sanitize("X" * 300)) == 255)
assert(len(sanitize(".".join(["X" * 100, "X" * 100, "X" * 100]))) == 255)
assert(len(sanitize(".".join(["X" * 300, "X" * 300, "X" * 300]))) == 255)
def test_unicode_normalization():
"""Names should be NFKD normalized."""
assert(sanitize("ў") == chr(1091)+chr(774))
def test_extensions():
"""Filename extensions should be preserved when possible."""
really_long_name = "X" * 1000 + ".pdf"
assert(sanitize(really_long_name).endswith(".pdf"))
assert(sanitize("X" * 1000).endswith("X"))
assert(sanitize("X" * 100 + "." + "X" * 100 + ".pdf").endswith(".pdf"))
assert(sanitize("X" * 100 + "." + "X" * 400).endswith("X"))
assert(sanitize("X" * 100 + "." + "X" * 400 + ".pdf").endswith(".pdf"))