From d76c3dfc361ecda3d7e7b5e79b84f466980a82d8 Mon Sep 17 00:00:00 2001 From: louyihua Date: Fri, 21 Mar 2014 22:47:00 +0800 Subject: [PATCH] Fix the url processing error when there is a query string inside the video source's url 1. In video_module.py, rewrite the get_ext() function to use the built-in urlparse module to parse the input filename first and then get the file's extension name from the parsed path. 2. In test_video.py, add two test cases (one with query string while the other without) in order to test the rewritten get_ext() function. --- common/lib/xmodule/xmodule/tests/test_video.py | 14 +++++++++++++- .../xmodule/xmodule/video_module/video_module.py | 8 ++++++-- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/common/lib/xmodule/xmodule/tests/test_video.py b/common/lib/xmodule/xmodule/tests/test_video.py index 8cc8dc859a..722067d6fe 100644 --- a/common/lib/xmodule/xmodule/tests/test_video.py +++ b/common/lib/xmodule/xmodule/tests/test_video.py @@ -20,7 +20,7 @@ from mock import Mock from . import LogicTest from lxml import etree from xmodule.modulestore import Location -from xmodule.video_module import VideoDescriptor, create_youtube_string +from xmodule.video_module import VideoDescriptor, create_youtube_string, get_ext from .test_import import DummySystem from xblock.field_data import DictFieldData from xblock.fields import ScopeIds @@ -107,6 +107,18 @@ class VideoModuleTest(LogicTest): '1.50': ''} ) + def test_get_ext(self): + """Test get the file's extension in a url without query string.""" + filename_str = 'http://www.example.com/path/video.mp4' + output = get_ext(filename_str) + self.assertEqual(output, 'mp4') + + def test_get_ext_with_query_string(self): + """Test get the file's extension in a url with query string.""" + filename_str = 'http://www.example.com/path/video.mp4?param1=1&p2=2' + output = get_ext(filename_str) + self.assertEqual(output, 'mp4') + class VideoDescriptorTest(unittest.TestCase): """Test for VideoDescriptor""" diff --git a/common/lib/xmodule/xmodule/video_module/video_module.py b/common/lib/xmodule/xmodule/video_module/video_module.py index c2067cc026..fd7b795ac6 100644 --- a/common/lib/xmodule/xmodule/video_module/video_module.py +++ b/common/lib/xmodule/xmodule/video_module/video_module.py @@ -45,6 +45,12 @@ from xmodule.modulestore.inheritance import InheritanceKeyValueStore from xblock.runtime import KvsFieldData from urlparse import urlparse +def get_ext(filename): + # Prevent incorrectly parsing urls like 'http://abc.com/path/video.mp4?xxxx'. + path = urlparse(filename).path + return path.rpartition('.')[-1] + + log = logging.getLogger(__name__) @@ -254,8 +260,6 @@ class VideoModule(VideoFields, XModule): track_url = None transcript_download_format = self.transcript_download_format - # Prevent incorrectly parsing urls like 'http://abc.com/path/video.mp4?xxxx'. - get_ext = lambda filename: urlparse(filename).path.rpartition('.')[-1] sources = {get_ext(src): src for src in self.html5_sources} if self.download_video: