Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

fix: preserve UTF-8 byte size for text uploads (#40034)

pineapple88006670051950ab2a
diff --git a/api/services/file_service.py b/api/services/file_service.py
index 9409d2ac64d..acedc049518 100644
--- a/api/services/file_service.py
+++ b/api/services/file_service.py
@@ -170,9 +170,10 @@ class FileService:
# user uuid as file name
file_uuid = str(uuid.uuid4())
file_key = "upload_files/" + tenant_id + "/" + file_uuid + ".txt"
+ content = text.encode("utf-8")
# save file to storage
- storage.save(file_key, text.encode("utf-8"))
+ storage.save(file_key, content)
# save file to db
upload_file = UploadFile(
@@ -180,7 +181,7 @@ class FileService:
storage_type=StorageType(dify_config.STORAGE_TYPE),
key=file_key,
name=text_name,
- size=len(text),
+ size=len(content),
extension="txt",
mime_type="text/plain",
created_by=user_id,
diff --git a/api/tests/unit_tests/services/test_file_service.py b/api/tests/unit_tests/services/test_file_service.py
index 0278887e010..f1d36f5e9bf 100644
--- a/api/tests/unit_tests/services/test_file_service.py
+++ b/api/tests/unit_tests/services/test_file_service.py
@@ -112,6 +112,20 @@ class TestFileService:
assert persisted is not None
assert persisted.hash == result.hash
+ @pytest.mark.parametrize("text", ["ASCII text", "包含多字节 UTF-8 文本 🚀"])
+ def test_upload_text_uses_utf8_byte_length(self, text: str, file_service: FileService):
+ with patch("services.file_service.storage") as mock_storage:
+ result = file_service.upload_text(
+ text=text,
+ text_name="test.txt",
+ user_id="user_id",
+ tenant_id="tenant_id",
+ )
+
+ expected_content = text.encode("utf-8")
+ assert result.size == len(expected_content)
+ mock_storage.save.assert_called_once_with(result.key, expected_content)
+
def test_upload_file_uses_explicit_resource_tenant(self, file_service: FileService):
user = MagicMock(spec=Account)
user.id = "user-id"