Skip to content

Commit f88c798

Browse files
committed
fix: ignore invalid bytes according to utf-8
Signed-off-by: Anupam Kumar <kyteinsky@gmail.com>
1 parent a836e4a commit f88c798

2 files changed

Lines changed: 5 additions & 5 deletions

File tree

context_chat_backend/chain/ingest/doc_loader.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def _temp_file_wrapper(file: BinaryIO, loader: Callable, sep: str = '\n') -> str
3131
os.remove(tmp.name)
3232

3333
if isinstance(docs, str) or isinstance(docs, bytes):
34-
return docs.decode('utf-8') if isinstance(docs, bytes) else docs # pyright: ignore[reportReturnType]
34+
return docs.decode('utf-8', 'ignore') if isinstance(docs, bytes) else docs # pyright: ignore[reportReturnType]
3535

3636
return sep.join(d.page_content for d in docs)
3737

@@ -64,11 +64,11 @@ def _load_ppt_x(file: BinaryIO) -> str:
6464

6565

6666
def _load_rtf(file: BinaryIO) -> str:
67-
return striprtf.rtf_to_text(file.read().decode('utf-8')).strip()
67+
return striprtf.rtf_to_text(file.read().decode('utf-8', 'ignore')).strip()
6868

6969

7070
def _load_xml(file: BinaryIO) -> str:
71-
data = file.read().decode('utf-8')
71+
data = file.read().decode('utf-8', 'ignore')
7272
data = re.sub(r'</.+>', '', data)
7373
return data.strip()
7474

@@ -134,7 +134,7 @@ def decode_source(source: UploadFile) -> str | None:
134134
source.file.close()
135135
return result
136136

137-
result = source.file.read().decode('utf-8')
137+
result = source.file.read().decode('utf-8', 'ignore')
138138
source.file.close()
139139
return result
140140
except Exception:

context_chat_backend/ocs_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def _verify_signature(headers: Headers) -> str | None:
4444
)
4545
return None
4646

47-
auth_aa = b64decode(headers.get('AUTHORIZATION-APP-API', '')).decode('UTF-8')
47+
auth_aa = b64decode(headers.get('AUTHORIZATION-APP-API', '')).decode('UTF-8', 'ignore')
4848
username, app_secret = auth_aa.split(':', maxsplit=1)
4949

5050
if app_secret != getenv('APP_SECRET'):

0 commit comments

Comments
 (0)