Commit 7f5397c6df3c9dad670a097cf6f58a15f24072ea
1 parent
e1e633fa
Exists in
master
fix: don't crash when encoding is not supported
Showing
2 changed files
with
22 additions
and
1 deletions
Show diff stats
flaskr/controllers/main_controller.py
... | ... | @@ -6,6 +6,7 @@ from io import StringIO |
6 | 6 | from os import unlink, getenv |
7 | 7 | from os.path import join |
8 | 8 | |
9 | +import chardet | |
9 | 10 | import geopy |
10 | 11 | import pandas |
11 | 12 | import sqlalchemy |
... | ... | @@ -83,7 +84,14 @@ def gather_addresses(from_list, from_file): |
83 | 84 | addresses = [] |
84 | 85 | if from_file: |
85 | 86 | file_mimetype = from_file.mimetype |
86 | - file_contents = from_file.read().decode() | |
87 | + file_contents_raw = from_file.read() | |
88 | + detected = chardet.detect(file_contents_raw) | |
89 | + if detected['encoding']: | |
90 | + file_contents = file_contents_raw.decode( | |
91 | + encoding=detected['encoding'] | |
92 | + ) | |
93 | + else: | |
94 | + file_contents = file_contents_raw.decode() | |
87 | 95 | |
88 | 96 | rows_dicts = None |
89 | 97 | |
... | ... | @@ -198,6 +206,12 @@ def estimate(): # register new estimation request, more accurately |
198 | 206 | except validators.ValidationError as e: |
199 | 207 | form.origin_addresses_file.errors.append(str(e)) |
200 | 208 | return show_form() |
209 | + except UnicodeDecodeError as e: | |
210 | + form.origin_addresses_file.errors.append( | |
211 | + "We only accept UTF-8 and UTF-16 encoded files, \n" + | |
212 | + "or files we can detect encoding from." | |
213 | + ) | |
214 | + return show_form() | |
201 | 215 | |
202 | 216 | try: |
203 | 217 | estimation.destination_addresses = gather_addresses( |
... | ... | @@ -207,6 +221,12 @@ def estimate(): # register new estimation request, more accurately |
207 | 221 | except validators.ValidationError as e: |
208 | 222 | form.destination_addresses_file.errors.append(str(e)) |
209 | 223 | return show_form() |
224 | + except UnicodeDecodeError as e: | |
225 | + form.origin_addresses_file.errors.append( | |
226 | + "We only accept UTF-8 and UTF-16 encoded files, \n" + | |
227 | + "or files we can detect encoding from." | |
228 | + ) | |
229 | + return show_form() | |
210 | 230 | |
211 | 231 | estimation.use_train_below_km = form.use_train_below_km.data |
212 | 232 | ... | ... |
requirements.txt