Skip to content

Commit

Permalink
Handle non-ascii filenames on non-utf8 filesystems. Convert filenames…
Browse files Browse the repository at this point in the history
… and warn when saving them, and raise exceptions if trying to access them and the filesystem encoding has changed. Closes stephenmcd#186.
  • Loading branch information
stephenmcd committed Apr 7, 2012
1 parent 4db8567 commit f1ea7d4
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
13 changes: 11 additions & 2 deletions mezzanine/core/templatetags/mezzanine_tags.py
Expand Up @@ -119,10 +119,19 @@ def thumbnail(image_url, width, height):
thumb_url = "%s/%s/%s" % (os.path.dirname(image_url),
settings.THUMBNAILS_DIR_NAME, thumb_name)

# Abort if thumbnail exists or original image doesn't exist.
if os.path.exists(thumb_path):
try:
thumb_exists = os.path.exists(thumb_path)
except UnicodeEncodeError:
# The image that was saved to a filesystem with utf-8 support,
# but somehow the locale has changed and the filesystem does not
# support utf-8.
from mezzanine.core.exceptions import FileSystemEncodingChanged
raise FileSystemEncodingChanged()
if thumb_exists:
# Thumbnail exists, don't generate it.
return thumb_url
elif not default_storage.exists(image_url):
# Requested image does not exist, just return its URL.
return image_url

image = Image.open(default_storage.open(image_url))
Expand Down
16 changes: 13 additions & 3 deletions mezzanine/galleries/models.py
Expand Up @@ -59,9 +59,19 @@ def save(self, delete_zip_import=True, *args, **kwargs):
image.verify()
except:
continue
path = default_storage.save(os.path.join(GALLERIES_UPLOAD_DIR,
self.slug, name.decode("utf-8")), ContentFile(data))
self.images.add(GalleryImage(file=path))
path = os.path.join(GALLERIES_UPLOAD_DIR, self.slug,
name.decode("utf-8"))
try:
saved_path = default_storage.save(path, ContentFile(data))
except UnicodeEncodeError:
from warnings import warn
warn("Your filesystem encoding doesn't seem to support "
"utf-8. You may need to set LC_CTYPE to a correct "
"value via your terminal, eg: en_US.utf8")
path = os.path.join(GALLERIES_UPLOAD_DIR, self.slug,
unicode(name, errors="ignore"))
saved_path = default_storage.save(path, ContentFile(data))
self.images.add(GalleryImage(file=saved_path))
if delete_zip_import:
zip_file.close()
self.zip_import.delete(save=True)
Expand Down

0 comments on commit f1ea7d4

Please sign in to comment.