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 +6,7 @@ from io import StringIO | ||
6 | from os import unlink, getenv | 6 | from os import unlink, getenv |
7 | from os.path import join | 7 | from os.path import join |
8 | 8 | ||
9 | +import chardet | ||
9 | import geopy | 10 | import geopy |
10 | import pandas | 11 | import pandas |
11 | import sqlalchemy | 12 | import sqlalchemy |
@@ -83,7 +84,14 @@ def gather_addresses(from_list, from_file): | @@ -83,7 +84,14 @@ def gather_addresses(from_list, from_file): | ||
83 | addresses = [] | 84 | addresses = [] |
84 | if from_file: | 85 | if from_file: |
85 | file_mimetype = from_file.mimetype | 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 | rows_dicts = None | 96 | rows_dicts = None |
89 | 97 | ||
@@ -198,6 +206,12 @@ def estimate(): # register new estimation request, more accurately | @@ -198,6 +206,12 @@ def estimate(): # register new estimation request, more accurately | ||
198 | except validators.ValidationError as e: | 206 | except validators.ValidationError as e: |
199 | form.origin_addresses_file.errors.append(str(e)) | 207 | form.origin_addresses_file.errors.append(str(e)) |
200 | return show_form() | 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 | try: | 216 | try: |
203 | estimation.destination_addresses = gather_addresses( | 217 | estimation.destination_addresses = gather_addresses( |
@@ -207,6 +221,12 @@ def estimate(): # register new estimation request, more accurately | @@ -207,6 +221,12 @@ def estimate(): # register new estimation request, more accurately | ||
207 | except validators.ValidationError as e: | 221 | except validators.ValidationError as e: |
208 | form.destination_addresses_file.errors.append(str(e)) | 222 | form.destination_addresses_file.errors.append(str(e)) |
209 | return show_form() | 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 | estimation.use_train_below_km = form.use_train_below_km.data | 231 | estimation.use_train_below_km = form.use_train_below_km.data |
212 | 232 |
requirements.txt
@@ -26,6 +26,7 @@ enum34==1.1.6 | @@ -26,6 +26,7 @@ enum34==1.1.6 | ||
26 | geopy==1.23.0 | 26 | geopy==1.23.0 |
27 | certifi==2019.11.28 | 27 | certifi==2019.11.28 |
28 | python-dotenv==0.10.3 | 28 | python-dotenv==0.10.3 |
29 | +chardet==4.0.0 | ||
29 | 30 | ||
30 | # Force stable werkzeug | 31 | # Force stable werkzeug |
31 | # see https://gitlab.irap.omp.eu/carbon/travel-carbon-footprint.irap.omp.eu/issues/38 | 32 | # see https://gitlab.irap.omp.eu/carbon/travel-carbon-footprint.irap.omp.eu/issues/38 |