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.
Python agentagentic-aiagentic-frameworkagentic-workflowaiautomationclaudedeepseekgenaigptllmlow-codemcpnextjsno-codeopenaipythonskillsworkflow
fix(files): normalize remote file Content-Type media types (#44677)
Winoren5f5f40afc235
diff --git a/api/core/file/remote_file_metadata.py b/api/core/file/remote_file_metadata.pyindex d4d14860fcc..517a0f36d95 100644--- a/api/core/file/remote_file_metadata.py+++ b/api/core/file/remote_file_metadata.py@@ -9,6 +9,8 @@ from uuid import uuid4import httpx+from libs.http_headers import content_type_media_type+try:import magicexcept ImportError:@@ -67,7 +69,7 @@ def guess_file_info_from_response(response: httpx.Response) -> FileInfo:mimetype, _ = mimetypes.guess_type(url)if mimetype is None:# If guessing fails, use Content-Type from response headers- mimetype = response.headers.get("Content-Type", "application/octet-stream")+ mimetype = content_type_media_type(response.headers.get("Content-Type", "application/octet-stream"))# Use python-magic to guess MIME type if still unknown or genericif mimetype == "application/octet-stream" and magic is not None:diff --git a/api/libs/http_headers.py b/api/libs/http_headers.pynew file mode 100644index 00000000000..35095e1dbee--- /dev/null+++ b/api/libs/http_headers.py@@ -0,0 +1,9 @@+"""Business-independent parsing of HTTP header values."""++from werkzeug.http import parse_options_header+++def content_type_media_type(content_type: str) -> str:+ """Return the lowercase media type without Content-Type parameters."""+ media_type, _ = parse_options_header(content_type)+ return media_type.lower()diff --git a/api/tests/unit_tests/core/file/test_remote_file_metadata.py b/api/tests/unit_tests/core/file/test_remote_file_metadata.pyindex 2e9425d65dc..a3659742f63 100644--- a/api/tests/unit_tests/core/file/test_remote_file_metadata.py+++ b/api/tests/unit_tests/core/file/test_remote_file_metadata.py@@ -158,6 +158,48 @@ class TestGuessFileInfoFromResponse:assert isinstance(info, FileInfo)[email protected]("url", ["https://example.com/download", "https://example.com/"])+ ("content_type", "expected_mimetype", "expected_extension"),+ [+ ("image/png; charset=binary", "image/png", ".png"),+ ('application/pdf; name="a;b.pdf"', "application/pdf", ".pdf"),+ ("Text/Plain; charset=UTF-8", "text/plain", ".txt"),+ ("application/octet-stream; version=1", "application/octet-stream", ".bin"),+ ("image/png", "image/png", ".png"),+ (None, "application/octet-stream", ".bin"),+ ],+)+def test_header_media_type_drives_extensionless_metadata(+ monkeypatch: pytest.MonkeyPatch,+ url: str,+ content_type: str | None,+ expected_mimetype: str,+ expected_extension: str,+) -> None:+ # Metadata must work without optional content sniffing, including HEAD responses.+ monkeypatch.setattr(remote_file_metadata, "magic", None)+ headers = {"Content-Type": content_type} if content_type is not None else {}+ response = httpx.Response(200, request=httpx.Request("HEAD", url), headers=headers)++ info = guess_file_info_from_response(response)++ assert info.mimetype == expected_mimetype+ assert info.extension == expected_extension+ assert info.filename.endswith(expected_extension)+++def test_filename_mimetype_keeps_priority_over_parameterized_header() -> None:+ response = make_response(+ url="https://example.com/report.pdf",+ headers={"Content-Type": "text/plain; charset=utf-8"},+ )++ info = guess_file_info_from_response(response)++ assert (info.filename, info.extension, info.mimetype) == ("report.pdf", ".pdf", "application/pdf")++class TestMagicImportWarnings:@pytest.mark.parametrize(("platform_name", "expected_message"),diff --git a/api/tests/unit_tests/services/test_remote_file_service.py b/api/tests/unit_tests/services/test_remote_file_service.pyindex 0b380b3936b..d96f39df1a6 100644--- a/api/tests/unit_tests/services/test_remote_file_service.py+++ b/api/tests/unit_tests/services/test_remote_file_service.py@@ -371,3 +371,37 @@ def test_upload_rejects_file_that_exceeds_size_limit(size_check.assert_called_once_with(extension=".pdf", file_size=1024)upload.assert_not_called()+++def test_upload_parameterized_content_type_uses_media_extension_and_mimetype(+ remote_file_service: RemoteFileService,+ upload: MagicMock,+ size_check: MagicMock,+ storage: Storage,+ sqlite_session: Session,+) -> None:+ url = "https://example.com/download"+ content = b"%PDF-1.7\nremote document"+ head = httpx.Response(+ 200,+ request=httpx.Request("HEAD", url),+ headers={"Content-Type": "application/pdf; charset=binary", "Content-Length": str(len(content))},+ )+ body = httpx.Response(200, request=httpx.Request("GET", url), content=content)+ with (+ patch("services.remote_file_service.remote_fetcher.make_request", side_effect=[head, body]),+ patch("services.remote_file_service.file_helpers.get_signed_file_url", return_value="signed-url"),+ ):+ result = remote_file_service.upload_from_url(url=url, user=_account())++ size_check.assert_called_once_with(extension=".pdf", file_size=len(content))+ assert upload.call_args.kwargs["filename"] == "download.pdf"+ assert upload.call_args.kwargs["mimetype"] == "application/pdf"+ upload_file = sqlite_session.get(UploadFile, result.id)+ assert upload_file is not None+ assert (upload_file.name, upload_file.extension, upload_file.mime_type) == (+ "download.pdf",+ "pdf",+ "application/pdf",+ )+ assert storage.load_once(upload_file.key) == content