From 31f4d44c73e7974acbc45e17efe1bebc4fe3a0c0 Mon Sep 17 00:00:00 2001 From: Jin Hai Date: Fri, 30 May 2025 14:25:59 +0800 Subject: [PATCH] Update upload filename length limit from 128 to 256, which is aligned with os (#7971) ### What problem does this PR solve? Change filename length limit from 128 to 256 ### Type of change - [x] Bug Fix (non-breaking change which fixes an issue) Signed-off-by: Jin Hai --- api/apps/kb_app.py | 2 +- api/constants.py | 1 + api/db/services/file_service.py | 4 ++-- sdk/python/test/test_frontend_api/test_dataset.py | 2 +- 4 files changed, 5 insertions(+), 4 deletions(-) diff --git a/api/apps/kb_app.py b/api/apps/kb_app.py index 206af56c1..3971dfdf0 100644 --- a/api/apps/kb_app.py +++ b/api/apps/kb_app.py @@ -47,7 +47,7 @@ def create(): return get_data_error_result(message="Dataset name must be string.") if dataset_name == "": return get_data_error_result(message="Dataset name can't be empty.") - if len(dataset_name) >= DATASET_NAME_LIMIT: + if len(dataset_name.encode("utf-8")) >= DATASET_NAME_LIMIT: return get_data_error_result( message=f"Dataset name length is {len(dataset_name)} which is large than {DATASET_NAME_LIMIT}") diff --git a/api/constants.py b/api/constants.py index e6a97e2c1..f5de6d98b 100644 --- a/api/constants.py +++ b/api/constants.py @@ -25,3 +25,4 @@ REQUEST_WAIT_SEC = 2 REQUEST_MAX_WAIT_SEC = 300 DATASET_NAME_LIMIT = 128 +FILE_NAME_LEN_LIMIT = 256 diff --git a/api/db/services/file_service.py b/api/db/services/file_service.py index 803164f9e..664cd1bdb 100644 --- a/api/db/services/file_service.py +++ b/api/db/services/file_service.py @@ -30,7 +30,7 @@ from api.db.services.file2document_service import File2DocumentService from api.utils import get_uuid from api.utils.file_utils import filename_type, read_potential_broken_pdf, thumbnail_img from rag.utils.storage_factory import STORAGE_IMPL - +from api.constants import FILE_NAME_LEN_LIMIT class FileService(CommonService): # Service class for managing file operations and storage @@ -412,7 +412,7 @@ class FileService(CommonService): MAX_FILE_NUM_PER_USER = int(os.environ.get("MAX_FILE_NUM_PER_USER", 0)) if MAX_FILE_NUM_PER_USER > 0 and DocumentService.get_doc_count(kb.tenant_id) >= MAX_FILE_NUM_PER_USER: raise RuntimeError("Exceed the maximum file number of a free user!") - if len(file.filename.encode("utf-8")) >= 128: + if len(file.filename.encode("utf-8")) >= FILE_NAME_LEN_LIMIT: raise RuntimeError("Exceed the maximum length of file name!") filename = duplicate_name(DocumentService.query, name=file.filename, kb_id=kb.id) diff --git a/sdk/python/test/test_frontend_api/test_dataset.py b/sdk/python/test/test_frontend_api/test_dataset.py index 3baaf4e8c..c60dc4d0c 100644 --- a/sdk/python/test/test_frontend_api/test_dataset.py +++ b/sdk/python/test/test_frontend_api/test_dataset.py @@ -108,7 +108,7 @@ def test_invalid_name_dataset(get_auth): long_string = "" - while len(long_string) <= DATASET_NAME_LIMIT: + while len(long_string.encode("utf-8")) <= DATASET_NAME_LIMIT: long_string += random.choice(string.ascii_letters + string.digits) res = create_dataset(get_auth, long_string)