From 2687c8d839f9a4959c68eab0114a86355f50ad7e Mon Sep 17 00:00:00 2001 From: Henrik Levkowetz Date: Tue, 30 Jan 2018 08:29:21 +0000 Subject: [PATCH] Added a workaround for libmagic mislabelling plain text content with a line beginning with 'virtual' as text/x-c++. - Legacy-Id: 14586 --- ietf/meeting/tests_views.py | 2 +- ietf/utils/validators.py | 8 +++++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/ietf/meeting/tests_views.py b/ietf/meeting/tests_views.py index ed05853c0..09af58d67 100644 --- a/ietf/meeting/tests_views.py +++ b/ietf/meeting/tests_views.py @@ -1635,7 +1635,7 @@ class MaterialsTests(TestCase): q = PyQuery(r.content) 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" r = self.client.post(url,dict(file=test_file,apply_to_all=False)) self.assertEqual(r.status_code, 302) diff --git a/ietf/utils/validators.py b/ietf/utils/validators.py index 440c43d99..f98f8065a 100644 --- a/ietf/utils/validators.py +++ b/ietf/utils/validators.py @@ -73,7 +73,13 @@ def validate_file_size(file): def validate_mime_type(file, valid): 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: raise ValidationError('Found content with unexpected mime type: %s. Expected one of %s.' % (mime_type, ', '.join(valid) ))