@devries-cameron20 / composio Mirror · read-only / commits / 30f8ca137
Fork 0

Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.

Mirrored from https://github.com/Camo67/composio.git

Synced Nov 21, 07:15 UTC

fix(py): keep the extension of files fetched from extensionless URLs (#5119)

This PR: - Closes #5105 - makes `_get_extension_from_mimetype` in `python/composio/core/models/_files.py` look types up in the shared `composio.utils.mimetypes` table (the one behind `get_extension_from_mime_type`, ported from the TypeScript `getExtensionFromMimeType`) and deletes the private 20-entry copy - an export URL such as `.../export?format=xlsx` now uploads as `file_<ts>_<id>.xlsx`; docx, pptx, xls, doc, ppt, csv, tar, mov, and ogg are covered too - keeps the empty extension for `application/octet-stream` and unknown types: the shared helper's subtype fallback is not used on this path, so no invented extensions - adds `text/csv` → `csv` to the Python table and both TypeScript tables (`mime.ts`, `fileUtils.node.ts`) so they stay identical; TypeScript already returned `csv` through its subtype fallback, so its output is unchanged (patch changeset for `@composio/core`/`@composio/slim` per repo convention) - @yunusarda-sudo posted a fix on their fork (https://github.com/yunusarda-sudo/composio/tree/fix/url-download-mimetype-extension), which also added `text/csv` to both SDKs; this PR takes a smaller table-only lookup instead of their fallback chain, so their code is not reused Verification (from `python/`, `composio.__file__` checked to be this checkout): - new `test_files.py` cases against the old code: 11 failed, 5 passed; with the fix all pass - `pytest tests/test_files.py tests/test_mimetypes.py tests/test_tool_router_session_files.py`: 292 passed - `ruff check` and `ruff format --check` (`config/ruff.toml`) clean on touched files; `mypy==2.3.1 --config-file config/mypy.ini composio/` and on `tests/test_files.py`: no issues - `ts/packages/core`: `vitest run test/utils` 375 passed; Prettier clean; `pnpm validate:changesets` passed

Alberto Schiabel committed Oct 28, 2026 at 18:44 UTC 30f8ca1373389a2bf2f6a3425711f545aba3f4d0
7 files changed +91 -24
.changeset/mime-text-csv.md new
+6
@@ -0,0 +1,6 @@
1 +---
2 +'@composio/core': patch
3 +'@composio/slim': patch
4 +---
5 +
6 +`getExtensionFromMimeType` now maps `text/csv` to `csv` from its table, matching the Python SDK. The returned extension is unchanged.
python/composio/core/models/_files.py
+11 -24
@@ -301,30 +301,17 @@ def _is_url(value: str) -> bool:
301
302
303 def _get_extension_from_mimetype(mimetype: str) -> str:
304 - """Get file extension from mimetype."""
305 - mime_to_ext = {
306 - "image/jpeg": ".jpg",
307 - "image/png": ".png",
308 - "image/gif": ".gif",
309 - "image/webp": ".webp",
310 - "image/svg+xml": ".svg",
311 - "image/bmp": ".bmp",
312 - "image/tiff": ".tiff",
313 - "application/pdf": ".pdf",
314 - "application/json": ".json",
315 - "application/xml": ".xml",
316 - "text/plain": ".txt",
317 - "text/html": ".html",
318 - "text/css": ".css",
319 - "text/javascript": ".js",
320 - "application/zip": ".zip",
321 - "application/gzip": ".gz",
322 - "audio/mpeg": ".mp3",
323 - "audio/wav": ".wav",
324 - "video/mp4": ".mp4",
325 - "video/webm": ".webm",
326 - }
327 - return mime_to_ext.get(mimetype.lower(), "")
304 + """Return the dotted extension for a known content type, or ``""``.
305 +
306 + Looks the type up in the table shared with ``get_extension_from_mime_type``
307 + but skips that helper's subtype fallback: an unknown or generic binary type
308 + leaves the generated name extensionless instead of inventing an extension.
309 + """
310 + clean = mimetype.lower()
311 + if clean == "application/octet-stream":
312 + return ""
313 + extension = mimetypes._MIME_TO_EXT.get(clean)
314 + return f".{extension}" if extension else ""
315
316
317 def _generate_timestamped_filename(extension: str) -> str:
python/composio/utils/mimetypes.py
+1
@@ -333,6 +333,7 @@ _MIME_TO_EXT: t.Dict[str, str] = {
333 "text/plain": "txt",
334 "text/html": "html",
335 "text/css": "css",
336 + "text/csv": "csv",
337 "text/javascript": "js",
338 "application/json": "json",
339 "application/xml": "xml",
python/tests/test_files.py
+70
@@ -1781,11 +1781,43 @@ class TestUrlHelperFunctions:
1781 assert _get_extension_from_mimetype("text/plain") == ".txt"
1782 assert _get_extension_from_mimetype("application/json") == ".json"
1783
1784 + @pytest.mark.parametrize(
1785 + ("mimetype", "expected"),
1786 + [
1787 + (
1788 + "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
1789 + ".xlsx",
1790 + ),
1791 + (
1792 + "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
1793 + ".docx",
1794 + ),
1795 + (
1796 + "application/vnd.openxmlformats-officedocument.presentationml.presentation",
1797 + ".pptx",
1798 + ),
1799 + ("application/vnd.ms-excel", ".xls"),
1800 + ("application/msword", ".doc"),
1801 + ("application/vnd.ms-powerpoint", ".ppt"),
1802 + ("text/csv", ".csv"),
1803 + ("application/x-tar", ".tar"),
1804 + ("video/quicktime", ".mov"),
1805 + ("audio/ogg", ".ogg"),
1806 + ],
1807 + )
1808 + def test_get_extension_from_mimetype_uses_shared_table(self, mimetype, expected):
1809 + """Types the shared MIME table knows keep their extension (issue #5105)."""
1810 + assert _get_extension_from_mimetype(mimetype) == expected
1811 +
1812 def test_get_extension_from_mimetype_unknown_type(self):
1813 """Test _get_extension_from_mimetype returns empty for unknown types."""
1814 assert _get_extension_from_mimetype("application/unknown") == ""
1815 assert _get_extension_from_mimetype("custom/type") == ""
1816
1817 + def test_get_extension_from_mimetype_generic_binary_type(self):
1818 + """A generic binary type does not name a format, so no extension is added."""
1819 + assert _get_extension_from_mimetype("application/octet-stream") == ""
1820 +
1821 def test_get_extension_from_mimetype_case_insensitive(self):
1822 """Test _get_extension_from_mimetype handles case-insensitive mimetypes (RFC 2045)."""
1823 # Uppercase variations
@@ -1891,6 +1923,44 @@ class TestFetchFileFromUrl:
1923 assert filename.startswith("file_")
1924 assert filename.endswith(".pdf")
1925
1926 + @patch("composio.core.models._files.safe_get")
1927 + def test_fetch_file_from_url_keeps_office_extension_when_url_has_none(
1928 + self, mock_get
1929 + ):
1930 + """An export URL without an extension keeps the xlsx extension (issue #5105)."""
1931 + xlsx = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
1932 + mock_response = MagicMock()
1933 + mock_response.ok = True
1934 + mock_response.status_code = 200
1935 + mock_response.headers = {"content-type": xlsx}
1936 + mock_response.iter_content.return_value = [b"xlsx data"]
1937 + mock_response.close = MagicMock()
1938 + mock_get.return_value = mock_response
1939 +
1940 + filename, content, mimetype = _fetch_file_from_url(
1941 + "https://docs.google.com/spreadsheets/d/abc123/export?format=xlsx"
1942 + )
1943 +
1944 + assert filename.startswith("file_")
1945 + assert filename.endswith(".xlsx")
1946 + assert mimetype == xlsx
1947 +
1948 + @patch("composio.core.models._files.safe_get")
1949 + def test_fetch_file_from_url_octet_stream_without_extension(self, mock_get):
1950 + """A generic binary response from an extensionless URL stays extensionless."""
1951 + mock_response = MagicMock()
1952 + mock_response.ok = True
1953 + mock_response.status_code = 200
1954 + mock_response.headers = {"content-type": "application/octet-stream"}
1955 + mock_response.iter_content.return_value = [b"binary data"]
1956 + mock_response.close = MagicMock()
1957 + mock_get.return_value = mock_response
1958 +
1959 + filename, _, _ = _fetch_file_from_url("https://example.com/download")
1960 +
1961 + assert filename.startswith("file_")
1962 + assert "." not in filename
1963 +
1964 @patch("composio.core.models._files.safe_get")
1965 def test_fetch_file_from_url_failure(self, mock_get):
1966 """Test error handling when URL fetch fails."""
ts/packages/core/src/utils/fileUtils.node.ts
+1
@@ -67,6 +67,7 @@ const getExtensionFromMimeType = (mimeType: string): string => {
67 'text/plain': 'txt',
68 'text/html': 'html',
69 'text/css': 'css',
70 + 'text/csv': 'csv',
71 'text/javascript': 'js',
72 'application/json': 'json',
73 'application/xml': 'xml',
ts/packages/core/src/utils/mime.ts
+1
@@ -7,6 +7,7 @@ export function getExtensionFromMimeType(mimeType: string): string {
7 'text/plain': 'txt',
8 'text/html': 'html',
9 'text/css': 'css',
10 + 'text/csv': 'csv',
11 'text/javascript': 'js',
12 'application/json': 'json',
13 'application/xml': 'xml',
ts/packages/core/test/utils/mime.test.ts
+1
@@ -6,6 +6,7 @@ describe('getExtensionFromMimeType', () => {
6 it('should return extension for known mimetype', () => {
7 expect(getExtensionFromMimeType('image/png')).toBe('png');
8 expect(getExtensionFromMimeType('application/pdf')).toBe('pdf');
9 + expect(getExtensionFromMimeType('text/csv')).toBe('csv');
10 });
11
12 it.each(['constructor', '__proto__', 'toString', 'hasOwnProperty'])(