Skip to content

Commit 278d868

Browse files
committed
Added handling for when file magic doesn't return a definitive encoding for a file. Added a test case to excercise error cases.
- Legacy-Id: 14782
1 parent 8cc61e0 commit 278d868

2 files changed

Lines changed: 11 additions & 4 deletions

File tree

ietf/meeting/tests_views.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1745,6 +1745,12 @@ def test_upload_minutes_agenda(self):
17451745
self.assertEqual(doc.rev,'02')
17461746
self.assertTrue(session2.sessionpresentation_set.filter(document__type_id=doctype))
17471747

1748+
# Test bad encoding
1749+
test_file = StringIO(u'<html><h1>Title</h1><section>Some\x93text</section></html>'.encode('latin1'))
1750+
test_file.name = "some.html"
1751+
r = self.client.post(url,dict(file=test_file))
1752+
self.assertContains(r, 'Could not identify the file encoding')
1753+
17481754
def test_upload_minutes_agenda_unscheduled(self):
17491755
for doctype in ('minutes','agenda'):
17501756
session = SessionFactory(meeting__type_id='ietf', add_to_schedule=False)

ietf/secr/proceedings/utils.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,15 @@ def handle_upload_file(file,filename,meeting,subdir, request=None, encoding=None
3939
file.open()
4040
text = file.read()
4141
if encoding:
42-
text = text.decode(encoding)
42+
try:
43+
text = text.decode(encoding)
44+
except LookupError as e:
45+
return "Failure trying to save '%s': Could not identify the file encoding, got '%s'. Hint: Try to upload as UTF-8." % (filename, str(e)[:120])
4346
else:
4447
try:
4548
text = smart_text(text)
4649
except UnicodeDecodeError as e:
47-
msg = "Failure trying to save '%s': %s..." % (filename, str(e)[:120])
48-
return msg
49-
50+
return "Failure trying to save '%s'. Hint: Try to upload as UTF-8: %s..." % (filename, str(e)[:120])
5051
# Whole file sanitization; add back what's missing from a complete
5152
# document (sanitize will remove these).
5253
clean = sanitize_document(text)

0 commit comments

Comments
 (0)