Commit ad4e3d50 by Omar Al-Ithawi Committed by Salah Alomari

Allow uploading subtitles with unicode filenames

parent 6fa189dc
...@@ -86,11 +86,19 @@ class TestSaveSubsToStore(SharedModuleStoreTestCase): ...@@ -86,11 +86,19 @@ class TestSaveSubsToStore(SharedModuleStoreTestCase):
def clear_subs_content(self): def clear_subs_content(self):
"""Remove, if subtitles content exists.""" """Remove, if subtitles content exists."""
try: for content_location in [self.content_location, self.content_copied_location]:
content = contentstore().find(self.content_location) try:
contentstore().delete(content.location) content = contentstore().find(content_location)
except NotFoundError: contentstore().delete(content.location)
pass except NotFoundError:
pass
@classmethod
def sub_id_to_location(cls, sub_id):
"""
A helper to compute a static file location from a subtitle id.
"""
return StaticContent.compute_location(cls.course.id, u'subs_{0}.srt.sjson'.format(sub_id))
@classmethod @classmethod
def setUpClass(cls): def setUpClass(cls):
...@@ -110,22 +118,31 @@ class TestSaveSubsToStore(SharedModuleStoreTestCase): ...@@ -110,22 +118,31 @@ class TestSaveSubsToStore(SharedModuleStoreTestCase):
] ]
} }
cls.subs_id = str(uuid4()) # Prefix it to ensure that unicode filenames are allowed
filename = 'subs_{0}.srt.sjson'.format(cls.subs_id) cls.subs_id = u'uniçøde_{}'.format(uuid4())
cls.content_location = StaticContent.compute_location(cls.course.id, filename) cls.subs_copied_id = u'cøpy_{}'.format(uuid4())
cls.content_location = cls.sub_id_to_location(cls.subs_id)
cls.content_copied_location = cls.sub_id_to_location(cls.subs_copied_id)
# incorrect subs # incorrect subs
cls.unjsonable_subs = {1} # set can't be serialized cls.unjsonable_subs = {1} # set can't be serialized
cls.unjsonable_subs_id = str(uuid4()) cls.unjsonable_subs_id = str(uuid4())
filename_unjsonable = 'subs_{0}.srt.sjson'.format(cls.unjsonable_subs_id) cls.content_location_unjsonable = cls.sub_id_to_location(cls.unjsonable_subs_id)
cls.content_location_unjsonable = StaticContent.compute_location(cls.course.id, filename_unjsonable)
def setUp(self): def setUp(self):
super(TestSaveSubsToStore, self).setUp() super(TestSaveSubsToStore, self).setUp()
self.addCleanup(self.clear_subs_content) self.addCleanup(self.clear_subs_content)
self.clear_subs_content() self.clear_subs_content()
def test_save_unicode_filename(self):
# Mock a video item
item = Mock(location=Mock(course_key=self.course.id))
transcripts_utils.save_subs_to_store(self.subs, self.subs_id, self.course)
transcripts_utils.copy_or_rename_transcript(self.subs_copied_id, self.subs_id, item)
self.assertTrue(contentstore().find(self.content_copied_location))
def test_save_subs_to_store(self): def test_save_subs_to_store(self):
with self.assertRaises(NotFoundError): with self.assertRaises(NotFoundError):
contentstore().find(self.content_location) contentstore().find(self.content_location)
......
...@@ -283,7 +283,7 @@ def copy_or_rename_transcript(new_name, old_name, item, delete_old=False, user=N ...@@ -283,7 +283,7 @@ def copy_or_rename_transcript(new_name, old_name, item, delete_old=False, user=N
If `old_name` is not found in storage, raises `NotFoundError`. If `old_name` is not found in storage, raises `NotFoundError`.
If `delete_old` is True, removes `old_name` files from storage. If `delete_old` is True, removes `old_name` files from storage.
""" """
filename = 'subs_{0}.srt.sjson'.format(old_name) filename = u'subs_{0}.srt.sjson'.format(old_name)
content_location = StaticContent.compute_location(item.location.course_key, filename) content_location = StaticContent.compute_location(item.location.course_key, filename)
transcripts = contentstore().find(content_location).data transcripts = contentstore().find(content_location).data
save_subs_to_store(json.loads(transcripts), new_name, item) save_subs_to_store(json.loads(transcripts), new_name, item)
......
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