From 2c7941d1ef0525b1994cdc81cfbb0ef61f6ed01c Mon Sep 17 00:00:00 2001 From: Mushtaq Ali Date: Tue, 13 Dec 2016 14:57:36 +0500 Subject: [PATCH] Prevent files larger than 5 GB Prevent file containig containing non-ascii or special characters in file name TNL-5855 TNL-5957 --- .../contentstore/views/tests/test_videos.py | 21 ++++ cms/djangoapps/contentstore/views/videos.py | 11 +- cms/static/js/factories/videos_index.js | 4 +- .../views/active_video_upload_list_spec.js | 68 +++++++++--- .../js/views/active_video_upload_list.js | 105 ++++++++++++------ cms/templates/videos_index.html | 8 +- 6 files changed, 161 insertions(+), 56 deletions(-) diff --git a/cms/djangoapps/contentstore/views/tests/test_videos.py b/cms/djangoapps/contentstore/views/tests/test_videos.py index 6db155fbf8..c49c4c5a3c 100644 --- a/cms/djangoapps/contentstore/views/tests/test_videos.py +++ b/cms/djangoapps/contentstore/views/tests/test_videos.py @@ -286,6 +286,27 @@ class VideosHandlerTestCase(VideoUploadTestMixin, CourseTestCase): self.assertIn('error', response) self.assertEqual(response['error'], "Request 'files' entry contain unsupported content_type") + @override_settings(AWS_ACCESS_KEY_ID='test_key_id', AWS_SECRET_ACCESS_KEY='test_secret') + @patch('boto.s3.connection.S3Connection') + def test_upload_with_non_ascii_charaters(self, mock_conn): + """ + Test that video uploads throws error message when file name contains special characters. + """ + file_name = u'test\u2019_file.mp4' + files = [{'file_name': file_name, 'content_type': 'video/mp4'}] + + bucket = Mock() + mock_conn.return_value = Mock(get_bucket=Mock(return_value=bucket)) + + response = self.client.post( + self.url, + json.dumps({'files': files}), + content_type='application/json' + ) + self.assertEqual(response.status_code, 400) + response = json.loads(response.content) + self.assertEqual(response['error'], 'The file name for %s must contain only ASCII characters.' % file_name) + @override_settings(AWS_ACCESS_KEY_ID="test_key_id", AWS_SECRET_ACCESS_KEY="test_secret") @patch("boto.s3.key.Key") @patch("boto.s3.connection.S3Connection") diff --git a/cms/djangoapps/contentstore/views/videos.py b/cms/djangoapps/contentstore/views/videos.py index ada8081449..df268c9796 100644 --- a/cms/djangoapps/contentstore/views/videos.py +++ b/cms/djangoapps/contentstore/views/videos.py @@ -34,6 +34,8 @@ VIDEO_SUPPORTED_FILE_FORMATS = { '.mov': 'video/quicktime', } +VIDEO_UPLOAD_MAX_FILE_SIZE_GB = 5 + class StatusDisplayStrings(object): """ @@ -262,7 +264,8 @@ def videos_index_html(course): "encodings_download_url": reverse_course_url("video_encodings_download", unicode(course.id)), "previous_uploads": _get_index_videos(course), "concurrent_upload_limit": settings.VIDEO_UPLOAD_PIPELINE.get("CONCURRENT_UPLOAD_LIMIT", 0), - "video_supported_file_formats": VIDEO_SUPPORTED_FILE_FORMATS.keys() + "video_supported_file_formats": VIDEO_SUPPORTED_FILE_FORMATS.keys(), + "video_upload_max_file_size": VIDEO_UPLOAD_MAX_FILE_SIZE_GB } ) @@ -328,6 +331,12 @@ def videos_post(course, request): for req_file in req_files: file_name = req_file["file_name"] + try: + file_name.encode('ascii') + except UnicodeEncodeError: + error_msg = 'The file name for %s must contain only ASCII characters.' % file_name + return JsonResponse({'error': error_msg}, status=400) + edx_video_id = unicode(uuid4()) key = storage_service_key(bucket, file_name=edx_video_id) for metadata_name, value in [ diff --git a/cms/static/js/factories/videos_index.js b/cms/static/js/factories/videos_index.js index 47cd356912..3db118037f 100644 --- a/cms/static/js/factories/videos_index.js +++ b/cms/static/js/factories/videos_index.js @@ -10,13 +10,15 @@ define([ concurrentUploadLimit, uploadButton, previousUploads, - videoSupportedFileFormats + videoSupportedFileFormats, + videoUploadMaxFileSizeInGB ) { var activeView = new ActiveVideoUploadListView({ postUrl: videoHandlerUrl, concurrentUploadLimit: concurrentUploadLimit, uploadButton: uploadButton, videoSupportedFileFormats: videoSupportedFileFormats, + videoUploadMaxFileSizeInGB: videoUploadMaxFileSizeInGB, onFileUploadDone: function(activeVideos) { $.ajax({ url: videoHandlerUrl, diff --git a/cms/static/js/spec/views/active_video_upload_list_spec.js b/cms/static/js/spec/views/active_video_upload_list_spec.js index 8e1ab82927..cfb0dec156 100644 --- a/cms/static/js/spec/views/active_video_upload_list_spec.js +++ b/cms/static/js/spec/views/active_video_upload_list_spec.js @@ -18,16 +18,16 @@ define( this.postUrl = '/test/post/url'; this.uploadButton = $('