Skip to content

Commit b25cade

Browse files
Merge pull request #9 from salehmuhaysin/master
Fix utf-8 decode issue when uploading machine
2 parents 0bed935 + ee05bcb commit b25cade

File tree

1 file changed

+9
-18
lines changed

1 file changed

+9
-18
lines changed

app/controllers/case_management.py

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -86,23 +86,17 @@ def md5(fname):
8686
def unzip_file(zip_path,dst_path):
8787
try:
8888
create_folders(dst_path)
89-
with zipfile.ZipFile(zip_path , 'r') as zfile:
90-
for name in zfile.namelist():
91-
p_encoded = (dst_path + name).encode('utf-8')
92-
if name.endswith('/'):
93-
create_folders(p_encoded)
94-
continue
95-
with zfile.open(name) as file:
96-
f = open((dst_path + name).encode('utf-8'), 'w')
97-
f.write(file.read())
98-
f.close()
99-
return [True , "All files of ["+zip_path+"] extracted to ["+dst_path+"]"]
100-
101-
89+
with zipfile.ZipFile(zip_path , mode='r') as zfile:
90+
zfile.extractall(path=dst_path )
91+
return [True , "All files of ["+zip_path+"] extracted to ["+dst_path+"]"]
92+
93+
except UnicodeDecodeError as e:
94+
#handle unicode errors, like utf-8 codec issues
95+
return [True , "All files of ["+zip_path+"] partialy extracted to ["+dst_path+"]"]
10296
except Exception as e:
10397
return [False, "Error extract the zip content: " + str(e)]
10498

105-
99+
106100
# ================================ list zip file content
107101
# list zip file content
108102
def list_zip_file(zip_path):
@@ -344,15 +338,13 @@ def case_upload_machine(case_id):
344338
# save the file to the raw data
345339
file.save(raw_folder + file_name)
346340

347-
zip_file_list = [] # contain the zip file list content
348341

349342
# unzip the file to machine files
350343
try:
351344
unzip_fun = unzip_file(raw_folder + file_name , files_folder + file_name + "/")
352345

353346
if unzip_fun[0] == True:
354347

355-
zip_file_list = list_zip_file(raw_folder + file_name)
356348

357349
if RemoveRawFiles:
358350
remove_file(raw_folder + file_name) # remove the raw file
@@ -370,8 +362,7 @@ def case_upload_machine(case_id):
370362
return jsonify({'result' : False , 'filename' : filename , 'message' : 'Failed to unzip the file'})
371363

372364
f = {
373-
'name': filename,
374-
'zip_content' : zip_file_list
365+
'name': filename
375366
}
376367
machine_details = {
377368
'main_case' : case_id,

0 commit comments

Comments
 (0)