Permalink
Browse files

fix bug 741660: Accept demo ZIP uploads with filenames containing non…

…-ASCII characters
  • Loading branch information...
1 parent 955446f commit 31cbcfb634dc5c05bc8f9c6b893abcb8d0a1b18a @lmorchard lmorchard committed Feb 8, 2013
Binary file not shown.
View
@@ -2,8 +2,9 @@
from datetime import datetime
from time import time, gmtime, strftime
from os import unlink, makedirs
+import os.path
from os.path import basename, dirname, isfile, isdir
-from shutil import rmtree
+from shutil import rmtree, copyfileobj
import re
import logging
@@ -696,22 +697,27 @@ def process_demo_package(self):
valid_entries = Submission.get_valid_demo_zipfile_entries(zf)
for zi in valid_entries:
+ if type(zi.filename) is unicode:
+ zi_filename = zi.filename
+ else:
+ zi_filename = zi.filename.decode('utf-8', 'ignore')
# HACK: Normalize demo.html to index.html
- if zi.filename == 'demo.html':
- zi.filename = 'index.html'
+ if zi_filename == u'demo.html':
+ zi_filename = u'index.html'
# Relocate all files from detected root dir to a directory named
# for the zip file in storage
- out_fn = '%s/%s' % ( new_root_dir, zi.filename )
+ out_fn = u'%s/%s' % (new_root_dir, zi_filename)
out_dir = dirname(out_fn)
# Create parent directories where necessary.
if not isdir(out_dir):
- makedirs(out_dir, 0775)
+ makedirs(out_dir.encode('utf-8'), 0775)
# Extract the file from the zip into the desired location.
- open(out_fn, 'w').write(zf.read(zi))
+ fout = open(out_fn.encode('utf-8'), 'wb')
+ copyfileobj(zf.open(zi), fout)
def update_submission_comment_count(sender, instance, **kwargs):
"""Update the denormalized count of comments for a submission on comment save/delete"""
@@ -1,3 +1,8 @@
+# coding=utf-8
+
+# This Python file uses the following encoding: utf-8
+# see also: http://www.python.org/dev/peps/pep-0263/
+import sys
import logging
import re
import urlparse
@@ -272,6 +277,61 @@ def test_demo_html_normalized(self):
rmtree(path)
+ def test_demo_unicode_filenames(self):
+ """Bug 741660: Demo package containing filenames with non-ASCII
+ characters works"""
+
+ fout = StringIO()
+ zf = zipfile.ZipFile(fout, 'w')
+ zf.writestr('demo.html', """<html></html""")
+ zf.writestr('css/예제.css', 'h1 { color: red }')
+ zf.writestr('js/示例.js', 'alert("HELLO WORLD");')
+ zf.close()
+
+ s = Submission(title='Hello world', slug='hello-world',
+ description='This is a hello world demo',
+ creator=self.user)
+
+ s.demo_package.save('play_demo.zip', ContentFile(fout.getvalue()))
+ s.demo_package.close()
+ s.clean()
+ s.save()
+
+ s.process_demo_package()
+
+ path = s.demo_package.path.replace('.zip', '')
+
+ ok_(isdir(path))
+ ok_(isfile((u'%s/index.html' % path).encode('utf-8')))
+ ok_(isfile((u'%s/css/예제.css' % path).encode('utf-8')))
+ ok_(isfile((u'%s/js/示例.js' % path).encode('utf-8')))
+
+ rmtree(path)
+
+ def test_demo_unicode_filenames_2(self):
+ """Bug 741660: Try testing a real .zip with non-ASCII filenames"""
+ zip_fn = '%s/fixtures/css3_clock.zip' % dirname(dirname(__file__))
+
+ zf = zipfile.ZipFile(zip_fn)
+
+ s = Submission(title='Hello world', slug='hello-world',
+ description='This is a hello world demo',
+ creator=self.user)
+
+ s.demo_package.save('play_demo.zip', ContentFile(open(zip_fn).read()))
+ s.demo_package.close()
+ s.clean()
+ s.save()
+
+ s.process_demo_package()
+
+ path = s.demo_package.path.replace('.zip', '')
+
+ ok_(isdir(path))
+ ok_(isfile((u'%s/stylé.css' % path).encode('utf-8')))
+
+ rmtree(path)
+
def test_demo_file_size_limit(self):
"""Demo package with any individual file >1MB in size is invalid"""
s = self.submission
@@ -10,6 +10,15 @@
# Do some dirty, dirty things to make development nicer.
class dev_hacks {
+ exec { 'locale-gen':
+ command => "/usr/sbin/locale-gen en_US.utf8"
+ }
+
+ exec { 'update-locale':
+ command => "/usr/sbin/update-locale LC_ALL='en_US.utf8'",
+ require => Exec['locale-gen']
+ }
+
file { "/home/vagrant":
owner => "vagrant", group => "vagrant", mode => 0755;
}

0 comments on commit 31cbcfb

Please sign in to comment.