Commit f71688ce by zubair-arbi

Merge pull request #2075 from zubair-arbi/zub/bugfix/std878-wrongcontenttype

Assign valid content_type to static resource (import_static_content)
parents 249a6182 b998a161
...@@ -68,6 +68,14 @@ class AssetsToyCourseTestCase(CourseTestCase): ...@@ -68,6 +68,14 @@ class AssetsToyCourseTestCase(CourseTestCase):
self.assertEquals(len(assets), expected_length) self.assertEquals(len(assets), expected_length)
self.assertEquals(json_response['totalCount'], expected_total) self.assertEquals(json_response['totalCount'], expected_total)
# Test valid contentType for pdf asset (textbook.pdf)
self.assertContains(resp, "/c4x/edX/toy/asset/textbook.pdf")
asset_location = StaticContent.get_location_from_path('/c4x/edX/toy/asset/textbook.pdf')
content = contentstore().find(asset_location)
# Check after import textbook.pdf has valid contentType ('application/pdf')
# Note: Actual contentType for textbook.pdf in asset.json is 'text/pdf'
self.assertEqual(content.content_type, 'application/pdf')
class UploadTestCase(CourseTestCase): class UploadTestCase(CourseTestCase):
""" """
......
...@@ -33,6 +33,7 @@ def import_static_content( ...@@ -33,6 +33,7 @@ def import_static_content(
policy = {} policy = {}
verbose = True verbose = True
mimetypes_list = mimetypes.types_map.values()
for dirname, _, filenames in os.walk(static_dir): for dirname, _, filenames in os.walk(static_dir):
for filename in filenames: for filename in filenames:
...@@ -64,10 +65,11 @@ def import_static_content( ...@@ -64,10 +65,11 @@ def import_static_content(
policy_ele = policy.get(content_loc.name, {}) policy_ele = policy.get(content_loc.name, {})
displayname = policy_ele.get('displayname', filename) displayname = policy_ele.get('displayname', filename)
locked = policy_ele.get('locked', False) locked = policy_ele.get('locked', False)
mime_type = policy_ele.get( mime_type = policy_ele.get('contentType')
'contentType',
mimetypes.guess_type(filename)[0] # Check extracted contentType in list of all valid mimetypes
) if not mime_type or mime_type not in mimetypes_list:
mime_type = mimetypes.guess_type(filename)[0] # Assign guessed mimetype
content = StaticContent( content = StaticContent(
content_loc, displayname, mime_type, data, content_loc, displayname, mime_type, data,
import_path=fullname_with_subpath, locked=locked import_path=fullname_with_subpath, locked=locked
......
{
"textbook.pdf":{
"contentType":"text/pdf",
"displayname":"textbook.pdf",
"locked":false,
"filename":"/c4x/edx/toy/asset/textbook.pdf",
"import_path":null,
"thumbnail_location":null
}
}
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