Added a workaround for libmagic mislabelling plain text content with a line beginning with 'virtual' as text/x-c++.

- Legacy-Id: 14586
This commit is contained in:
Henrik Levkowetz 2018-01-30 08:29:21 +00:00
parent fbb4b4447f
commit 2687c8d839
2 changed files with 8 additions and 2 deletions

View file

@ -1635,7 +1635,7 @@ class MaterialsTests(TestCase):
q = PyQuery(r.content) q = PyQuery(r.content)
self.assertTrue(q('form .has-error')) self.assertTrue(q('form .has-error'))
test_file = StringIO('this is some text for a test') test_file = StringIO(u'This is some text for a test, with the word\nvirtual at the beginning of a line.')
test_file.name = "not_really.txt" test_file.name = "not_really.txt"
r = self.client.post(url,dict(file=test_file,apply_to_all=False)) r = self.client.post(url,dict(file=test_file,apply_to_all=False))
self.assertEqual(r.status_code, 302) self.assertEqual(r.status_code, 302)

View file

@ -73,7 +73,13 @@ def validate_file_size(file):
def validate_mime_type(file, valid): def validate_mime_type(file, valid):
file.open() file.open()
mime_type, encoding = get_mime_type(file.read()) raw = file.read()
mime_type, encoding = get_mime_type(raw)
# work around mis-identification of text where a line has 'virtual' as
# the first word:
if mime_type == 'text/x-c++' and re.search('(?m)^virtual\s', raw):
mod = raw.replace(str('virtual'), str(' virtual'))
mime_type, encoding = get_mime_type(mod)
if not mime_type in valid: if not mime_type in valid:
raise ValidationError('Found content with unexpected mime type: %s. Expected one of %s.' % raise ValidationError('Found content with unexpected mime type: %s. Expected one of %s.' %
(mime_type, ', '.join(valid) )) (mime_type, ', '.join(valid) ))