Slugify file names on local storage

Just stripping anything non-ASCII leads to errors with UTF-8 only
filenames.

Closes #1181
This commit is contained in:
Gina Häußge 2016-01-11 16:42:05 +01:00
parent 59027d349f
commit 7743372ee4
3 changed files with 12 additions and 11 deletions

View file

@ -32,7 +32,8 @@ INSTALL_REQUIRES = [
"pkginfo==1.2.1",
"requests==2.7.0",
"semantic_version==2.4.2",
"psutil==3.2.1"
"psutil==3.2.1",
"awesome-slugify>=1.6.5,<1.7"
]
# Additional requirements for optional install options

View file

@ -311,6 +311,10 @@ class LocalFileStorage(StorageInterface):
self._metadata_cache = pylru.lrucache(10)
from slugify import Slugify
self._slugify = Slugify()
self._slugify.safe_chars = "-_.() "
self._old_metadata = None
self._initialize_metadata()
@ -609,9 +613,9 @@ class LocalFileStorage(StorageInterface):
def sanitize_name(self, name):
"""
Raises a :class:`ValueError` for a ``name`` containing ``/`` or ``\``. Otherwise strips any characters from the
given ``name`` that are not any of the ASCII characters, digits, ``-``, ``_``, ``.``, ``(``, ``)`` or space and
replaces and spaces with ``_``.
Raises a :class:`ValueError` for a ``name`` containing ``/`` or ``\``. Otherwise
slugifies the given ``name`` by converting it to ASCII, leaving ``-``, ``_``, ``.``,
``(``, and ``)`` as is.
"""
if name is None:
return None
@ -619,11 +623,7 @@ class LocalFileStorage(StorageInterface):
if "/" in name or "\\" in name:
raise ValueError("name must not contain / or \\")
import string
valid_chars = "-_.() {ascii}{digits}".format(ascii=string.ascii_letters, digits=string.digits)
sanitized_name = ''.join(c for c in name if c in valid_chars)
sanitized_name = sanitized_name.replace(" ", "_")
return sanitized_name
return self._slugify(name).replace(" ", "_")
def sanitize_path(self, path):
"""

View file

@ -309,8 +309,8 @@ class LocalStorageTest(unittest.TestCase):
@data(
("some_file.gco", "some_file.gco"),
("some_file with (parentheses) and ümläuts and digits 123.gco", "some_file_with_(parentheses)_and_mluts_and_digits_123.gco"),
("pengüino pequeño.stl", "pengino_pequeo.stl")
("some_file with (parentheses) and ümläuts and digits 123.gco", "some_file_with_(parentheses)_and_umlauts_and_digits_123.gco"),
("pengüino pequeño.stl", "penguino_pequeno.stl")
)
@unpack
def test_sanitize_name(self, input, expected):