cope with utf-8 and very long filenames (bug #649817)

This commit is contained in:
Andy McKay 2011-04-18 14:51:47 -07:00
Родитель 110db14638
Коммит c44df820dd
5 изменённых файлов: 45 добавлений и 4 удалений

Двоичные данные
apps/files/fixtures/files/unicode-filenames.xpi Normal file

Двоичный файл не отображается.

Просмотреть файл

@ -6,6 +6,7 @@ import stat
from django.conf import settings
from django.utils.datastructures import SortedDict
from django.utils.encoding import smart_unicode
import commonware.log
from jingo import register
@ -100,6 +101,22 @@ class FileViewer:
return {}
return self._get_files()
def truncate(self, filename, pre_length=15,
post_length=10, ellipsis=u'..'):
"""
Truncates a filename so that
somelongfilename.htm
becomes:
some...htm
as it truncates around the extension.
"""
root, ext = os.path.splitext(filename)
if len(root) > pre_length:
root = root[:pre_length] + ellipsis
if len(ext) > post_length:
ext = ext[:post_length] + ellipsis
return root + ext
@memoize(prefix='file-viewer')
def _get_files(self):
all_files, res = [], SortedDict()
@ -108,7 +125,8 @@ class FileViewer:
all_files.append([os.path.join(root, filename), filename])
for path, filename in sorted(all_files):
short = path[len(self.dest) + 1:]
filename = smart_unicode(filename)
short = smart_unicode(path[len(self.dest) + 1:])
mime, encoding = mimetypes.guess_type(filename)
binary = self.is_binary(mime, filename)
# TODO: handling for st.a.m.o will be going in here
@ -116,6 +134,7 @@ class FileViewer:
directory = os.path.isdir(path)
res[short] = {'full': path,
'filename': filename,
'truncated': self.truncate(filename),
'short': short,
'directory': directory,
'url': url,

Просмотреть файл

@ -22,7 +22,8 @@
style="padding-left: {{ value['depth'] }}em"
class="{% if value['depth'] %}hidden{% endif %}">
<a class="{{ file_viewer_class(value, selected) }}"
href="{{ value['url'] }}">{{ value['filename'] }}</a>
href="{{ value['url'] }}"
title="{{ value['filename'] }}">{{ value['truncated'] }}</a>
</li>
{% endfor %}
{% endif %}

Просмотреть файл

@ -1,3 +1,4 @@
# -*- coding: utf-8 -*-
import os
import mimetypes
import tempfile
@ -60,6 +61,16 @@ class TestFileHelper(test_utils.TestCase):
m, encoding = mimetypes.guess_type(f)
assert binary(m, f), '%s should be binary' % f
def test_truncate(self):
truncate = self.viewer.truncate
for x, y in (['foo.rdf', 'foo.rdf'],
['somelongfilename.rdf', 'somelongfilenam...rdf'],
[u'unicode삮.txt', u'unicode\uc0ae.txt'],
[u'unicodesomelong삮.txt', u'unicodesomelong...txt'],
['somelongfilename.somelongextension',
'somelongfilenam...somelonge..'],):
eq_(truncate(x), y)
def test_get_files_not_extracted(self):
assert not self.viewer.get_files()

Просмотреть файл

@ -1,12 +1,13 @@
import json
import os
import shutil
import tempfile
from django.conf import settings
from django.core.cache import cache
from django.utils.encoding import iri_to_uri
from nose.tools import eq_
import os
from pyquery import PyQuery as pq
import test_utils
@ -17,6 +18,10 @@ from files.models import File
from users.models import UserProfile
dictionary = 'apps/files/fixtures/files/dictionary-test.xpi'
unicode_filenames = 'apps/files/fixtures/files/unicode-filenames.xpi'
class FilesBase:
def login_as_editor(self):
@ -42,7 +47,6 @@ class FilesBase:
settings.TMP_PATH = tempfile.mkdtemp()
settings.ADDONS_PATH = tempfile.mkdtemp()
dictionary = 'apps/files/fixtures/files/dictionary-test.xpi'
for file_obj in [self.file, self.file_two]:
src = os.path.join(settings.ROOT, dictionary)
try:
@ -240,6 +244,12 @@ class TestFileViewer(FilesBase, test_utils.TestCase):
res = self.client.get(self.file_url('doesnotexist.js'))
eq_(res.status_code, 404)
def test_unicode(self):
self.file_viewer.src = unicode_filenames
self.file_viewer.extract()
res = self.client.get(self.file_url(iri_to_uri(u'\u1109\u1161\u11a9')))
eq_(res.status_code, 200)
class TestDiffViewer(FilesBase, test_utils.TestCase):
fixtures = ['base/addon_3615', 'base/users']