From 79eadf74da9e5a898b5c2c1e46f29f0b8a976a47 Mon Sep 17 00:00:00 2001 From: Derek Furst Date: Wed, 15 Apr 2026 12:44:54 -0400 Subject: [PATCH] added a function to handle decoding as utf-16 as a fallback if decoding with utf-8 fails --- src/app.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/app.py b/src/app.py index cb7d8fc7..2297807b 100644 --- a/src/app.py +++ b/src/app.py @@ -2991,7 +2991,8 @@ def bulk_samples_upload_and_validate(): file.filename = utils.secure_filename(file.filename) file_location = commons_file_helper.ensureTrailingSlash( app.config['FILE_UPLOAD_TEMP_DIR']) + temp_id + os.sep + file.filename - with open(file_location, newline='') as tsvfile: + tsvfile = open_tsv(file_location) + with tsvfile: reader = csv.DictReader(tsvfile, delimiter='\t') first = True for row in reader: @@ -3047,7 +3048,8 @@ def create_samples_from_bulk(): tsvfile_name = tsv_directory + temp_file_name records = [] headers = [] - with open(tsvfile_name, newline='') as tsvfile: + tsvfile = open_tsv(tsvfile_name) + with tsvfile: reader = csv.DictReader(tsvfile, delimiter='\t') first = True for row in reader: @@ -3114,6 +3116,15 @@ def create_samples_from_bulk(): return Response(json.dumps(response, sort_keys=True), status_code, mimetype='application/json') +def open_tsv(path): + try: + f = open(path, newline='', encoding='utf-8-sig') + f.read() + f.seek(0) + except UnicodeDecodeError: + return open(path, newline='', encoding='utf-16') + + def validate_samples(headers, records, header): error_msg = [] file_is_valid = True