From b08c19d9266b633aa6e10096a3be82a1034988f3 Mon Sep 17 00:00:00 2001 From: Jyong <76649700+JohnJyong@users.noreply.github.com> Date: Sat, 17 Jun 2023 15:21:48 +0800 Subject: [PATCH] fix encoding is none (#394) --- api/controllers/console/datasets/file.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/api/controllers/console/datasets/file.py b/api/controllers/console/datasets/file.py index 5db0446175..2b13e9e09e 100644 --- a/api/controllers/console/datasets/file.py +++ b/api/controllers/console/datasets/file.py @@ -143,7 +143,10 @@ class FilePreviewApi(Resource): with open(filepath, "rb") as fp: data = fp.read() encoding = chardet.detect(data)['encoding'] - text = data.decode(encoding=encoding).strip() if data else '' + if encoding: + text = data.decode(encoding=encoding).strip() if data else '' + else: + text = data.decode(encoding='utf-8').strip() if data else '' text = text[0:PREVIEW_WORDS_LIMIT] if text else '' return {'content': text}