From 607d3120f695d058ba5e688212d3b87cc1112c76 Mon Sep 17 00:00:00 2001 From: Gyrfalcon Date: Mon, 31 Oct 2016 23:11:25 -0500 Subject: [PATCH] thumbnails: Handle non-ASCII filenames The WSGI specification requires that URL-paths be encoded as ISO-8859-1 (Latin-1). For filenames that only use ASCII characters, this works correctly. When a filename includes characters encoded in other character sets, however, the thumbnail controller will return always HTTP 404 if the local filesystem does not also use the Latin-1 character set. To work around this discrepancy and ensure that requested file names are looked up on the local filesystem correctly, the string must be re-encoded in the proper character set. --- src/dcow/thumbnails.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/dcow/thumbnails.py b/src/dcow/thumbnails.py index a09c395..17df876 100644 --- a/src/dcow/thumbnails.py +++ b/src/dcow/thumbnails.py @@ -3,6 +3,10 @@ from milla import controllers import milla import os import webob.static +import sys + + +fs_encoding = sys.getfilesystemencoding() class ThumbnailController(milla.controllers.HTTPVerbController): @@ -11,6 +15,7 @@ class ThumbnailController(milla.controllers.HTTPVerbController): THUMB_SIZE = (262, 148) def GET(self, request, image): + image = image.encode('latin-1').decode(fs_encoding) thumb_dir = request.config['gallery.thumbnail_dir'] image_dir = request.config['gallery.screenshot_dir'] screenshot = os.path.join(image_dir, image)