Fix: check the file name length. (#6083)

### What problem does this PR solve?

#6060

### Type of change

- [x] Bug Fix (non-breaking change which fixes an issue)
This commit is contained in:
Kevin Hu 2025-03-14 15:01:37 +08:00 committed by GitHub
parent f0601afa75
commit 5c8ad6702a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 11 additions and 3 deletions

View File

@ -136,6 +136,10 @@ def upload(dataset_id, tenant_id):
return get_result(
message="No file selected!", code=settings.RetCode.ARGUMENT_ERROR
)
if len(file_obj.filename.encode("utf-8")) >= 128:
return get_result(
message="File name should be less than 128 bytes.", code=settings.RetCode.ARGUMENT_ERROR
)
'''
# total size
total_size = 0
@ -246,6 +250,11 @@ def update_doc(tenant_id, dataset_id, document_id):
DocumentService.update_meta_fields(document_id, req["meta_fields"])
if "name" in req and req["name"] != doc.name:
if len(req["name"].encode("utf-8")) >= 128:
return get_result(
message="The name should be less than 128 bytes.",
code=settings.RetCode.ARGUMENT_ERROR,
)
if (
pathlib.Path(req["name"].lower()).suffix
!= pathlib.Path(doc.name.lower()).suffix

View File

@ -139,10 +139,9 @@ class TestUploadDocuments:
# filename_length = 129
fp = create_txt_file(tmp_path / f"{'a' * (DOCUMENT_NAME_LIMIT - 3)}.txt")
res = upload_documnets(get_http_api_auth, ids[0], [fp])
assert res["code"] == 500
assert res["code"] == 101
assert (
res["message"]
== f"{'a' * (DOCUMENT_NAME_LIMIT - 3)}.txt: Exceed the maximum length of file name!"
res["message"].find("128") >= 0
)
def test_invalid_dataset_id(self, get_http_api_auth, tmp_path):