Commit c8f67197 authored by Tim Heap's avatar Tim Heap
Browse files

Fix long filename trimming logic

Instead of fixing anything it just entered an infinite loop.
parent 4b051e3d
......@@ -163,6 +163,21 @@ class TestVideoAddView(TestCase, WagtailTestUtils):
)
)
def test_add_too_long_filename(self):
video_file = create_test_video_file()
name = 'a_very_long_filename_' + ('x' * 100) + '.mp4'
response = self.post({
'title': "Test video",
'file': SimpleUploadedFile(name, video_file.read(), "video/mp4"),
})
# Should be valid
self.assertEqual(response.status_code, 302)
video = Video.objects.get()
self.assertEqual(len(video.file.name), Video._meta.get_field('file').max_length)
def test_add_with_collections(self):
root_collection = Collection.get_first_root_node()
evil_plans_collection = root_collection.add_child(name="Evil plans")
......
......@@ -12,6 +12,7 @@ import tempfile
import threading
from django.conf import settings
from django.core.exceptions import SuspiciousFileOperation
from django.core.files.base import ContentFile
from django.core.urlresolvers import reverse
from django.db import models
......@@ -126,12 +127,18 @@ class AbstractVideo(CollectionMember, index.Indexed, models.Model):
def get_upload_to(self, filename):
folder_name = 'original_videos'
filename = self.file.field.storage.get_valid_name(filename)
max_length = self._meta.get_field('file').max_length
# Truncate filename so it fits in the 100 character limit
# https://code.djangoproject.com/ticket/9893
while len(os.path.join(folder_name, filename)) >= 95:
prefix, dot, extension = filename.rpartition('.')
filename = prefix[:-1] + dot + extension
file_path = os.path.join(folder_name, filename)
too_long = len(file_path) - max_length
if too_long > 0:
head, ext = os.path.splitext(filename)
if too_long > len(head) + 1:
raise SuspiciousFileOperation('File name can not be shortened to a safe length')
filename = head[:-too_long] + ext
file_path = os.path.join(folder_name, filename)
return os.path.join(folder_name, filename)
def get_usage(self):
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment