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 22, 20:45 UTC
Fix: Strip query params in URL for uploaded files (#1867)
Musthaq Ahamad committed
Aug 25, 2025 at 12:51 UTC
ee12d253641673fdf5b3d06721dc6565e4798cc5
12 files changed
+776
-24
.changeset/khaki-birds-lay.md
new
+5
@@ -0,0 +1,5 @@
1
+---
2
+'@composio/core': patch
3
+---
4
+
5
+Fix long filenames while uploading files from URL
pnpm-lock.yaml
+20
-20
@@ -270,6 +270,22 @@ importers:
270
specifier: ^5.8.3
271
version: 5.8.3
272
273
+ ts/examples/file-handling:
274
+ dependencies:
275
+ '@composio/core':
276
+ specifier: workspace:*
277
+ version: link:../../packages/core
278
+ dotenv:
279
+ specifier: ^16.4.1
280
+ version: 16.6.1
281
+ devDependencies:
282
+ '@types/bun':
283
+ specifier: ^1.2.9
284
+ version: 1.2.16
285
+ typescript:
286
+ specifier: ^5.8.3
287
+ version: 5.8.3
288
+
289
ts/examples/google:
290
dependencies:
291
'@composio/core':
@@ -8745,7 +8761,7 @@ snapshots:
8761
ai: 4.3.16(react@18.3.1)(zod@3.25.67)
8762
cohere-ai: 7.17.1
8763
date-fns: 3.6.0
8748
- dotenv: 16.5.0
8764
+ dotenv: 16.6.1
8765
hono: 4.8.0
8766
hono-openapi: 0.4.8(effect@3.16.16)(hono@4.8.0)(openapi-types@12.1.3)(zod@3.25.67)
8767
json-schema: 0.4.0
@@ -11121,14 +11137,6 @@ snapshots:
11137
chai: 5.2.0
11138
tinyrainbow: 2.0.0
11139
11124
- '@vitest/mocker@3.2.4(vite@6.3.5(@types/node@20.19.1)(jiti@1.21.7)(tsx@4.20.3)(yaml@2.8.0))':
11125
- dependencies:
11126
- '@vitest/spy': 3.2.4
11127
- estree-walker: 3.0.3
11128
- magic-string: 0.30.17
11129
- optionalDependencies:
11130
- vite: 6.3.5(@types/node@20.19.1)(jiti@1.21.7)(tsx@4.20.3)(yaml@2.8.0)
11131
-
11140
'@vitest/mocker@3.2.4(vite@6.3.5(@types/node@22.15.32)(jiti@1.21.7)(tsx@4.20.3)(yaml@2.8.0))':
11141
dependencies:
11142
'@vitest/spy': 3.2.4
@@ -11137,14 +11145,6 @@ snapshots:
11145
optionalDependencies:
11146
vite: 6.3.5(@types/node@22.15.32)(jiti@1.21.7)(tsx@4.20.3)(yaml@2.8.0)
11147
11140
- '@vitest/mocker@3.2.4(vite@6.3.5(@types/node@24.0.3)(jiti@1.21.7)(tsx@4.20.3)(yaml@2.8.0))':
11141
- dependencies:
11142
- '@vitest/spy': 3.2.4
11143
- estree-walker: 3.0.3
11144
- magic-string: 0.30.17
11145
- optionalDependencies:
11146
- vite: 6.3.5(@types/node@24.0.3)(jiti@1.21.7)(tsx@4.20.3)(yaml@2.8.0)
11147
-
11148
'@vitest/pretty-format@3.2.4':
11149
dependencies:
11150
tinyrainbow: 2.0.0
@@ -11174,7 +11174,7 @@ snapshots:
11174
sirv: 3.0.1
11175
tinyglobby: 0.2.14
11176
tinyrainbow: 2.0.0
11177
- vitest: 3.2.4(@types/node@20.19.1)(@vitest/ui@3.2.4)(jiti@1.21.7)(tsx@4.20.3)(yaml@2.8.0)
11177
+ vitest: 3.2.4(@types/node@22.15.32)(@vitest/ui@3.2.4)(jiti@1.21.7)(tsx@4.20.3)(yaml@2.8.0)
11178
11179
'@vitest/utils@3.2.4':
11180
dependencies:
@@ -14336,7 +14336,7 @@ snapshots:
14336
dependencies:
14337
'@types/chai': 5.2.2
14338
'@vitest/expect': 3.2.4
14339
- '@vitest/mocker': 3.2.4(vite@6.3.5(@types/node@20.19.1)(jiti@1.21.7)(tsx@4.20.3)(yaml@2.8.0))
14339
+ '@vitest/mocker': 3.2.4(vite@6.3.5(@types/node@22.15.32)(jiti@1.21.7)(tsx@4.20.3)(yaml@2.8.0))
14340
'@vitest/pretty-format': 3.2.4
14341
'@vitest/runner': 3.2.4
14342
'@vitest/snapshot': 3.2.4
@@ -14420,7 +14420,7 @@ snapshots:
14420
dependencies:
14421
'@types/chai': 5.2.2
14422
'@vitest/expect': 3.2.4
14423
- '@vitest/mocker': 3.2.4(vite@6.3.5(@types/node@24.0.3)(jiti@1.21.7)(tsx@4.20.3)(yaml@2.8.0))
14423
+ '@vitest/mocker': 3.2.4(vite@6.3.5(@types/node@22.15.32)(jiti@1.21.7)(tsx@4.20.3)(yaml@2.8.0))
14424
'@vitest/pretty-format': 3.2.4
14425
'@vitest/runner': 3.2.4
14426
'@vitest/snapshot': 3.2.4
ts/examples/file-handling/.env.example
new
+6
@@ -0,0 +1,6 @@
1
+# Composio API Key - Get it from https://app.composio.dev
2
+COMPOSIO_API_KEY=your_composio_api_key_here
3
+
4
+# Add other environment variables as needed for your example
5
+# OPENAI_API_KEY=your_openai_api_key_here
6
+# ANTHROPIC_API_KEY=your_anthropic_api_key_here
ts/examples/file-handling/README.md
new
+53
@@ -0,0 +1,53 @@
1
+# File-handling Example
2
+
3
+This example demonstrates how to use Composio SDK for file-handling.
4
+
5
+## Setup
6
+
7
+1. **Install dependencies:**
8
+ ```bash
9
+ pnpm install
10
+ ```
11
+
12
+2. **Configure environment:**
13
+ ```bash
14
+ cp .env.example .env
15
+ ```
16
+
17
+ Then edit `.env` and add your API keys:
18
+ - `COMPOSIO_API_KEY`: Get it from [Composio Dashboard](https://app.composio.dev)
19
+
20
+## Running the Example
21
+
22
+```bash
23
+# Run the example
24
+pnpm start
25
+
26
+# Run in development mode (with file watching)
27
+pnpm dev
28
+```
29
+
30
+## What This Example Does
31
+
32
+- Initializes Composio SDK
33
+- Fetches available tools
34
+- Demonstrates basic usage patterns
35
+
36
+## Customization
37
+
38
+Edit `src/index.ts` to:
39
+- Add specific apps you want to integrate with
40
+- Implement your business logic
41
+- Add error handling and logging
42
+
43
+## Related Examples
44
+
45
+- [OpenAI Example](../openai) - Shows integration with OpenAI
46
+- [LangChain Example](../langchain) - Shows integration with LangChain
47
+- [More Examples](../) - Browse all available examples
48
+
49
+## Support
50
+
51
+- [Documentation](https://docs.composio.dev)
52
+- [Discord Community](https://discord.gg/composio)
53
+- [GitHub Issues](https://github.com/composio/composio/issues)
ts/examples/file-handling/package.json
new
+24
@@ -0,0 +1,24 @@
1
+{
2
+ "name": "file-handling-example",
3
+ "private": true,
4
+ "version": "0.1.0",
5
+ "description": "Example project for File-handling",
6
+ "main": "src/index.ts",
7
+ "scripts": {
8
+ "start": "bun src/index.ts",
9
+ "dev": "bun --watch src/index.ts",
10
+ "test": "echo \"Error: no test specified\" && exit 1"
11
+ },
12
+ "keywords": ["composio", "example", "file-handling"],
13
+ "author": "",
14
+ "license": "ISC",
15
+ "packageManager": "pnpm@10.8.0",
16
+ "dependencies": {
17
+ "@composio/core": "workspace:*",
18
+ "dotenv": "^16.4.1"
19
+ },
20
+ "devDependencies": {
21
+ "@types/bun": "^1.2.9",
22
+ "typescript": "^5.8.3"
23
+ }
24
+}
ts/examples/file-handling/pepe-silvia.png
Binary files /dev/null and b/ts/examples/file-handling/pepe-silvia.png differ
ts/examples/file-handling/src/index.ts
new
+55
@@ -0,0 +1,55 @@
1
+/**
2
+ * File-handling Example
3
+ *
4
+ * This example demonstrates how to use Composio SDK for file-handling.
5
+ *
6
+ * Prerequisites:
7
+ * 1. Set up your COMPOSIO_API_KEY in the .env file
8
+ * 3. Run the example: pnpm start
9
+ */
10
+
11
+import { Composio } from '@composio/core';
12
+import 'dotenv/config';
13
+import path from 'path';
14
+
15
+/**
16
+ * Initialize Composio
17
+ */
18
+const composio = new Composio({
19
+ apiKey: process.env.COMPOSIO_API_KEY,
20
+});
21
+
22
+/**
23
+ * Main function to run the example
24
+ */
25
+async function main() {
26
+ try {
27
+ console.log('🚀 Starting File-handling Example...');
28
+
29
+ // Get available tools
30
+ const tools = await composio.tools.get('default', 'GMAIL_SEND_EMAIL');
31
+
32
+ console.log(`✅ Found ${tools.length} tools`);
33
+
34
+ const filePath = path.join(__dirname, '..', 'pepe-silvia.png');
35
+ console.log(`Sending file from ${filePath}`);
36
+
37
+ const result = await composio.tools.execute('GMAIL_SEND_EMAIL', {
38
+ userId: 'default',
39
+ arguments: {
40
+ attachment: filePath,
41
+ recipient_email: 'musthaq@composio.dev',
42
+ user_id: 'me',
43
+ body: 'Hello, this is a test email with a file attachment.',
44
+ subject: 'Test Email with Attachment',
45
+ },
46
+ });
47
+
48
+ console.log(result);
49
+ } catch (error) {
50
+ console.error('❌ Error running example:', error);
51
+ }
52
+}
53
+
54
+// Run the example
55
+main().catch(console.error);
ts/examples/file-handling/src/url.ts
new
+58
@@ -0,0 +1,58 @@
1
+/**
2
+ * File-handling Example
3
+ *
4
+ * This example demonstrates how to use Composio SDK for file-handling.
5
+ *
6
+ * Prerequisites:
7
+ * 1. Set up your COMPOSIO_API_KEY in the .env file
8
+ * 3. Run the example: pnpm start
9
+ */
10
+
11
+import { Composio } from '@composio/core';
12
+import { file } from 'bun';
13
+import 'dotenv/config';
14
+import path from 'path';
15
+
16
+/**
17
+ * Initialize Composio
18
+ */
19
+const composio = new Composio({
20
+ apiKey: process.env.COMPOSIO_API_KEY,
21
+});
22
+
23
+/**
24
+ * Main function to run the example
25
+ */
26
+async function main() {
27
+ try {
28
+ console.log('🚀 Starting File-handling Example...');
29
+
30
+ // Get available tools
31
+ const tools = await composio.tools.get('default', 'GMAIL_SEND_EMAIL');
32
+
33
+ console.log(`✅ Found ${tools.length} tools`);
34
+
35
+ const filePath =
36
+ 'https://avatars.githubusercontent.com/u/128464815?v=4&id=sdsdjljkljlsdfjslkfjlksdjflkdjflksdjflksdfwfowdfk;dfkl;sdfk;slkf;lskf;lsdkf;lk';
37
+
38
+ console.log(`Sending file from ${filePath}`);
39
+
40
+ const result = await composio.tools.execute('GMAIL_SEND_EMAIL', {
41
+ userId: 'default',
42
+ arguments: {
43
+ attachment: filePath,
44
+ recipient_email: 'musthaq@composio.dev',
45
+ user_id: 'me',
46
+ body: 'Hello, this is a test email with a file attachment.',
47
+ subject: 'Test Email with Attachment',
48
+ },
49
+ });
50
+
51
+ console.log(JSON.stringify(result, null, 2));
52
+ } catch (error) {
53
+ console.error('❌ Error running example:', error);
54
+ }
55
+}
56
+
57
+// Run the example
58
+main().catch(console.error);
ts/examples/file-handling/tsconfig.json
new
+16
@@ -0,0 +1,16 @@
1
+{
2
+ "compilerOptions": {
3
+ "target": "ESNext",
4
+ "module": "ESNext",
5
+ "declaration": true,
6
+ "declarationDir": "./dist",
7
+ "outDir": "./dist",
8
+ "rootDir": "./src",
9
+ "strict": true,
10
+ "esModuleInterop": true,
11
+ "moduleResolution": "node",
12
+ "skipLibCheck": true,
13
+ "resolveJsonModule": true
14
+ },
15
+ "include": ["src"]
16
+}
ts/packages/core/src/utils/fileUtils.ts
+111
-4
@@ -4,6 +4,93 @@ import ComposioClient from '@composio/client';
4
import { COMPOSIO_DIR, TEMP_FILES_DIRECTORY_NAME } from './constants';
5
import logger from './logger';
6
import { FileDownloadData, FileUploadData } from '../types/files.types';
7
+import { getRandomShortId, getRandomUUID } from './uuid';
8
+
9
+// Helper function to get file extension from MIME type
10
+const getExtensionFromMimeType = (mimeType: string): string => {
11
+ const mimeToExt: Record<string, string> = {
12
+ 'text/plain': 'txt',
13
+ 'text/html': 'html',
14
+ 'text/css': 'css',
15
+ 'text/javascript': 'js',
16
+ 'application/json': 'json',
17
+ 'application/xml': 'xml',
18
+ 'application/pdf': 'pdf',
19
+ 'application/zip': 'zip',
20
+ 'application/x-zip-compressed': 'zip',
21
+ 'application/gzip': 'gz',
22
+ 'application/x-tar': 'tar',
23
+ 'application/msword': 'doc',
24
+ 'application/vnd.openxmlformats-officedocument.wordprocessingml.document': 'docx',
25
+ 'application/vnd.ms-excel': 'xls',
26
+ 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet': 'xlsx',
27
+ 'application/vnd.ms-powerpoint': 'ppt',
28
+ 'application/vnd.openxmlformats-officedocument.presentationml.presentation': 'pptx',
29
+ 'image/jpeg': 'jpg',
30
+ 'image/jpg': 'jpg',
31
+ 'image/png': 'png',
32
+ 'image/gif': 'gif',
33
+ 'image/svg+xml': 'svg',
34
+ 'image/webp': 'webp',
35
+ 'image/bmp': 'bmp',
36
+ 'image/tiff': 'tiff',
37
+ 'audio/mpeg': 'mp3',
38
+ 'audio/wav': 'wav',
39
+ 'audio/ogg': 'ogg',
40
+ 'video/mp4': 'mp4',
41
+ 'video/mpeg': 'mpeg',
42
+ 'video/quicktime': 'mov',
43
+ 'video/x-msvideo': 'avi',
44
+ 'video/webm': 'webm',
45
+ };
46
+
47
+ // Clean the MIME type by removing parameters (e.g., "text/plain; charset=utf-8" -> "text/plain")
48
+ const cleanMimeType = mimeType.split(';')[0].toLowerCase().trim();
49
+
50
+ // Try exact match first
51
+ if (mimeToExt[cleanMimeType]) {
52
+ return mimeToExt[cleanMimeType];
53
+ }
54
+
55
+ // Try to extract from the MIME type (e.g., "image/png" -> "png")
56
+ const parts = cleanMimeType.split('/');
57
+ if (parts.length === 2) {
58
+ const subtype = parts[1].toLowerCase();
59
+ const cleanSubtype = subtype; // Already cleaned above
60
+
61
+ // Handle structured MIME types with + (e.g., "application/vnd.api+json" -> "json")
62
+ if (cleanSubtype.includes('+')) {
63
+ const plusParts = cleanSubtype.split('+');
64
+ const prefix = plusParts[0];
65
+ const suffix = plusParts[plusParts.length - 1]; // Get the part after the last +
66
+
67
+ // For specific formats, prefer the prefix (e.g., "atom+xml" -> "atom")
68
+ const knownPrefixes = ['svg', 'atom', 'rss'];
69
+ if (knownPrefixes.includes(prefix)) {
70
+ return prefix;
71
+ }
72
+
73
+ // For common structured suffixes, use the suffix (e.g., "vnd.api+json" -> "json")
74
+ const structuredSuffixes = ['json', 'xml', 'yaml', 'zip', 'gzip'];
75
+ if (structuredSuffixes.includes(suffix)) {
76
+ return suffix;
77
+ }
78
+
79
+ // Default to the suffix for other structured types
80
+ return suffix;
81
+ }
82
+
83
+ return cleanSubtype || 'txt';
84
+ }
85
+
86
+ return 'txt'; // Default fallback
87
+};
88
+
89
+// Helper function to generate a filename with timestamp and random ID
90
+const generateTimestampedFilename = (extension: string, prefix?: string): string => {
91
+ const basePrefix = prefix || 'file_ts';
92
+ return `${basePrefix}${Date.now()}${getRandomShortId()}.${extension}`;
93
+};
94
95
const readFileContent = async (
96
path: string
@@ -11,7 +98,7 @@ const readFileContent = async (
98
try {
99
const content = require('fs').readFileSync(path);
100
return {
14
- fileName: pathModule.basename(path) || `file_${Date.now()}.${path.split('.').pop()}`,
101
+ fileName: generateTimestampedFilename(path.split('.').pop() || 'txt'),
102
content: content.toString('base64'),
103
mimeType: 'application/octet-stream',
104
};
@@ -30,10 +117,30 @@ const readFileContentFromURL = async (
117
const arrayBuffer = await response.arrayBuffer();
118
const content = Buffer.from(arrayBuffer);
119
const mimeType = response.headers.get('content-type') || 'application/octet-stream';
120
+
121
+ // Extract clean filename from URL, removing query parameters
122
+ const url = new URL(path);
123
+ const pathname = url.pathname;
124
+ let fileName = pathModule.basename(pathname);
125
+
126
+ // If no filename from URL, generate one with appropriate extension
127
+ if (!fileName || fileName === '/') {
128
+ // Try to get extension from mimeType
129
+ const extension = getExtensionFromMimeType(mimeType);
130
+ fileName = generateTimestampedFilename(extension);
131
+ } else {
132
+ // If filename has no extension, try to add one from mimeType
133
+ const hasExtension = fileName.includes('.');
134
+ if (!hasExtension) {
135
+ const extension = getExtensionFromMimeType(mimeType);
136
+ fileName = generateTimestampedFilename(extension);
137
+ }
138
+ }
139
+
140
return {
141
content: content.toString('base64'),
142
mimeType,
36
- fileName: pathModule.basename(path) || `file_${Date.now()}.${path.split('.').pop()}`,
143
+ fileName,
144
};
145
};
146
@@ -152,8 +259,8 @@ export const downloadFileFromS3 = async ({
259
}
260
const data = await response.arrayBuffer();
261
155
- const extension = mimeType.split('/')[1] || 'txt';
156
- const fileName = `${toolSlug}_${Date.now()}.${extension}`;
262
+ const extension = getExtensionFromMimeType(mimeType);
263
+ const fileName = generateTimestampedFilename(extension, `${toolSlug}_`);
264
const filePath = saveFile(fileName, Buffer.from(data), true);
265
return {
266
name: fileName,
ts/packages/core/src/utils/uuid.ts
+4
@@ -3,3 +3,7 @@ import { v4 as uuidv4 } from 'uuid';
3
export function getRandomUUID(): string {
4
return uuidv4();
5
}
6
+
7
+export function getRandomShortId(): string {
8
+ return getRandomUUID().slice(0, 8).replace(/-/g, '');
9
+}
ts/packages/core/test/utils/fileUtils.test.ts
new
+424
@@ -0,0 +1,424 @@
1
+import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
2
+import { getFileDataAfterUploadingToS3, downloadFileFromS3 } from '../../src/utils/fileUtils';
3
+import ComposioClient from '@composio/client';
4
+import * as uuid from '../../src/utils/uuid';
5
+
6
+// Mock the uuid module
7
+vi.mock('../../src/utils/uuid', () => ({
8
+ getRandomShortId: vi.fn(() => 'abc12345'),
9
+ getRandomUUID: vi.fn(() => '12345678-1234-1234-1234-123456789012'),
10
+}));
11
+
12
+// Mock fs module
13
+vi.mock('fs', () => ({
14
+ default: {
15
+ readFileSync: vi.fn(),
16
+ writeFileSync: vi.fn(),
17
+ existsSync: vi.fn(() => true),
18
+ mkdirSync: vi.fn(),
19
+ },
20
+ readFileSync: vi.fn(),
21
+ writeFileSync: vi.fn(),
22
+ existsSync: vi.fn(() => true),
23
+ mkdirSync: vi.fn(),
24
+}));
25
+
26
+// Mock os module
27
+vi.mock('os', () => ({
28
+ default: {
29
+ homedir: vi.fn(() => '/home/test'),
30
+ },
31
+ homedir: vi.fn(() => '/home/test'),
32
+}));
33
+
34
+// Mock path module
35
+vi.mock('path', () => ({
36
+ default: {
37
+ join: vi.fn((...args) => args.join('/')),
38
+ basename: vi.fn(path => path.split('/').pop()),
39
+ },
40
+ join: vi.fn((...args) => args.join('/')),
41
+ basename: vi.fn(path => path.split('/').pop()),
42
+}));
43
+
44
+// Mock global fetch
45
+const mockFetch = vi.fn();
46
+global.fetch = mockFetch;
47
+
48
+describe('fileUtils', () => {
49
+ let mockClient: ComposioClient;
50
+
51
+ beforeEach(() => {
52
+ mockClient = {
53
+ files: {
54
+ createPresignedURL: vi.fn(),
55
+ },
56
+ } as unknown as ComposioClient;
57
+
58
+ vi.clearAllMocks();
59
+
60
+ // Mock Date.now to return a consistent timestamp
61
+ vi.spyOn(Date, 'now').mockReturnValue(1640995200000); // 2022-01-01 00:00:00
62
+ });
63
+
64
+ afterEach(() => {
65
+ vi.restoreAllMocks();
66
+ });
67
+
68
+ describe('URL filename generation with query parameters', () => {
69
+ beforeEach(() => {
70
+ // Mock successful fetch response
71
+ mockFetch.mockResolvedValue({
72
+ ok: true,
73
+ arrayBuffer: () => Promise.resolve(new ArrayBuffer(10)),
74
+ headers: new Map([['content-type', 'application/pdf']]),
75
+ });
76
+
77
+ // Mock successful S3 upload
78
+ (mockClient.files.createPresignedURL as any).mockResolvedValue({
79
+ key: 'test-key',
80
+ type: 'new',
81
+ new_presigned_url: 'https://s3.example.com/upload',
82
+ });
83
+
84
+ // Mock successful PUT to S3
85
+ mockFetch.mockImplementation((url, options) => {
86
+ if (options?.method === 'PUT') {
87
+ return Promise.resolve({ ok: true });
88
+ }
89
+ // For the initial file fetch
90
+ return Promise.resolve({
91
+ ok: true,
92
+ arrayBuffer: () => Promise.resolve(new ArrayBuffer(10)),
93
+ headers: new Map([['content-type', 'application/pdf']]),
94
+ });
95
+ });
96
+ });
97
+
98
+ it('should handle URLs with query parameters correctly', async () => {
99
+ const urlWithQuery =
100
+ 'https://example.com/document.pdf?param1=value1¶m2=value2&token=abc123';
101
+
102
+ const result = await getFileDataAfterUploadingToS3(urlWithQuery, {
103
+ toolSlug: 'test-tool',
104
+ toolkitSlug: 'test-toolkit',
105
+ client: mockClient,
106
+ });
107
+
108
+ expect(result.name).toBe('document.pdf');
109
+ expect(mockFetch).toHaveBeenCalledWith(urlWithQuery);
110
+ });
111
+
112
+ it('should generate filename when URL has no filename', async () => {
113
+ const urlWithoutFilename = 'https://example.com/?download=true&format=pdf';
114
+
115
+ const result = await getFileDataAfterUploadingToS3(urlWithoutFilename, {
116
+ toolSlug: 'test-tool',
117
+ toolkitSlug: 'test-toolkit',
118
+ client: mockClient,
119
+ });
120
+
121
+ expect(result.name).toBe('file_ts1640995200000abc12345.pdf');
122
+ });
123
+
124
+ it('should generate filename when URL path ends with slash', async () => {
125
+ const urlEndingWithSlash = 'https://example.com/folder/?type=document';
126
+
127
+ const result = await getFileDataAfterUploadingToS3(urlEndingWithSlash, {
128
+ toolSlug: 'test-tool',
129
+ toolkitSlug: 'test-toolkit',
130
+ client: mockClient,
131
+ });
132
+
133
+ expect(result.name).toBe('file_ts1640995200000abc12345.pdf');
134
+ });
135
+
136
+ it('should add extension to filename without extension based on MIME type', async () => {
137
+ const urlWithoutExtension = 'https://example.com/document?format=pdf';
138
+
139
+ const result = await getFileDataAfterUploadingToS3(urlWithoutExtension, {
140
+ toolSlug: 'test-tool',
141
+ toolkitSlug: 'test-toolkit',
142
+ client: mockClient,
143
+ });
144
+
145
+ expect(result.name).toBe('file_ts1640995200000abc12345.pdf');
146
+ });
147
+ });
148
+
149
+ describe('MIME type extension handling', () => {
150
+ it('should handle structured MIME types with + correctly', async () => {
151
+ const testCases = [
152
+ { mimeType: 'application/vnd.api+json', expectedExt: 'json' },
153
+ { mimeType: 'application/ld+json', expectedExt: 'json' },
154
+ { mimeType: 'image/svg+xml', expectedExt: 'svg' },
155
+ { mimeType: 'application/atom+xml', expectedExt: 'atom' },
156
+ { mimeType: 'application/rss+xml', expectedExt: 'rss' },
157
+ { mimeType: 'application/hal+json', expectedExt: 'json' },
158
+ { mimeType: 'application/vnd.collection+json', expectedExt: 'json' },
159
+ { mimeType: 'application/vnd.custom+zip', expectedExt: 'zip' },
160
+ ];
161
+
162
+ for (const { mimeType, expectedExt } of testCases) {
163
+ // Reset mocks for each iteration
164
+ vi.clearAllMocks();
165
+
166
+ // Mock successful fetch response
167
+ mockFetch.mockResolvedValueOnce({
168
+ ok: true,
169
+ arrayBuffer: () => Promise.resolve(new ArrayBuffer(10)),
170
+ headers: new Map([['content-type', mimeType]]),
171
+ });
172
+
173
+ // Mock successful S3 upload
174
+ (mockClient.files.createPresignedURL as any).mockResolvedValueOnce({
175
+ key: 'test-key',
176
+ type: 'new',
177
+ new_presigned_url: 'https://s3.example.com/upload',
178
+ });
179
+
180
+ // Mock successful PUT to S3
181
+ mockFetch.mockResolvedValueOnce({ ok: true });
182
+
183
+ const result = await getFileDataAfterUploadingToS3('https://example.com/file', {
184
+ toolSlug: 'test-tool',
185
+ toolkitSlug: 'test-toolkit',
186
+ client: mockClient,
187
+ });
188
+
189
+ expect(result.name).toBe(`file_ts1640995200000abc12345.${expectedExt}`);
190
+ }
191
+ });
192
+
193
+ it('should handle MIME types with parameters', async () => {
194
+ // Reset mocks
195
+ vi.clearAllMocks();
196
+
197
+ mockFetch.mockResolvedValueOnce({
198
+ ok: true,
199
+ arrayBuffer: () => Promise.resolve(new ArrayBuffer(10)),
200
+ headers: new Map([['content-type', 'text/plain; charset=utf-8']]),
201
+ });
202
+
203
+ // Mock successful S3 upload
204
+ (mockClient.files.createPresignedURL as any).mockResolvedValueOnce({
205
+ key: 'test-key',
206
+ type: 'new',
207
+ new_presigned_url: 'https://s3.example.com/upload',
208
+ });
209
+
210
+ // Mock successful PUT to S3
211
+ mockFetch.mockResolvedValueOnce({ ok: true });
212
+
213
+ const result = await getFileDataAfterUploadingToS3('https://example.com/file', {
214
+ toolSlug: 'test-tool',
215
+ toolkitSlug: 'test-toolkit',
216
+ client: mockClient,
217
+ });
218
+
219
+ expect(result.name).toBe('file_ts1640995200000abc12345.txt');
220
+ });
221
+
222
+ it('should handle common MIME types', async () => {
223
+ const testCases = [
224
+ { mimeType: 'application/pdf', expectedExt: 'pdf' },
225
+ { mimeType: 'image/jpeg', expectedExt: 'jpg' },
226
+ { mimeType: 'image/png', expectedExt: 'png' },
227
+ { mimeType: 'text/html', expectedExt: 'html' },
228
+ { mimeType: 'application/json', expectedExt: 'json' },
229
+ { mimeType: 'video/mp4', expectedExt: 'mp4' },
230
+ ];
231
+
232
+ for (const { mimeType, expectedExt } of testCases) {
233
+ // Reset mocks for each iteration
234
+ vi.clearAllMocks();
235
+
236
+ mockFetch.mockResolvedValueOnce({
237
+ ok: true,
238
+ arrayBuffer: () => Promise.resolve(new ArrayBuffer(10)),
239
+ headers: new Map([['content-type', mimeType]]),
240
+ });
241
+
242
+ // Mock successful S3 upload
243
+ (mockClient.files.createPresignedURL as any).mockResolvedValueOnce({
244
+ key: 'test-key',
245
+ type: 'new',
246
+ new_presigned_url: 'https://s3.example.com/upload',
247
+ });
248
+
249
+ // Mock successful PUT to S3
250
+ mockFetch.mockResolvedValueOnce({ ok: true });
251
+
252
+ const result = await getFileDataAfterUploadingToS3('https://example.com/file', {
253
+ toolSlug: 'test-tool',
254
+ toolkitSlug: 'test-toolkit',
255
+ client: mockClient,
256
+ });
257
+
258
+ expect(result.name).toBe(`file_ts1640995200000abc12345.${expectedExt}`);
259
+ }
260
+ });
261
+
262
+ it('should fallback to generic extraction for unknown MIME types', async () => {
263
+ // Reset mocks
264
+ vi.clearAllMocks();
265
+
266
+ mockFetch.mockResolvedValueOnce({
267
+ ok: true,
268
+ arrayBuffer: () => Promise.resolve(new ArrayBuffer(10)),
269
+ headers: new Map([['content-type', 'application/custom-format']]),
270
+ });
271
+
272
+ // Mock successful S3 upload
273
+ (mockClient.files.createPresignedURL as any).mockResolvedValueOnce({
274
+ key: 'test-key',
275
+ type: 'new',
276
+ new_presigned_url: 'https://s3.example.com/upload',
277
+ });
278
+
279
+ // Mock successful PUT to S3
280
+ mockFetch.mockResolvedValueOnce({ ok: true });
281
+
282
+ const result = await getFileDataAfterUploadingToS3('https://example.com/file', {
283
+ toolSlug: 'test-tool',
284
+ toolkitSlug: 'test-toolkit',
285
+ client: mockClient,
286
+ });
287
+
288
+ expect(result.name).toBe('file_ts1640995200000abc12345.custom-format');
289
+ });
290
+ });
291
+
292
+ describe('downloadFileFromS3', () => {
293
+ it('should generate filename with tool slug prefix', async () => {
294
+ mockFetch.mockResolvedValue({
295
+ ok: true,
296
+ arrayBuffer: () => Promise.resolve(new ArrayBuffer(10)),
297
+ });
298
+
299
+ const result = await downloadFileFromS3({
300
+ toolSlug: 'github',
301
+ s3Url: 'https://s3.example.com/file.txt',
302
+ mimeType: 'text/plain',
303
+ });
304
+
305
+ expect(result.name).toBe('github_1640995200000abc12345.txt');
306
+ });
307
+
308
+ it('should handle MIME types with + in downloadFileFromS3', async () => {
309
+ mockFetch.mockResolvedValue({
310
+ ok: true,
311
+ arrayBuffer: () => Promise.resolve(new ArrayBuffer(10)),
312
+ });
313
+
314
+ const result = await downloadFileFromS3({
315
+ toolSlug: 'api-tool',
316
+ s3Url: 'https://s3.example.com/data.json',
317
+ mimeType: 'application/vnd.api+json',
318
+ });
319
+
320
+ expect(result.name).toBe('api-tool_1640995200000abc12345.json');
321
+ });
322
+
323
+ it('should handle download failure', async () => {
324
+ mockFetch.mockResolvedValue({
325
+ ok: false,
326
+ statusText: 'Not Found',
327
+ });
328
+
329
+ await expect(
330
+ downloadFileFromS3({
331
+ toolSlug: 'test-tool',
332
+ s3Url: 'https://s3.example.com/nonexistent.txt',
333
+ mimeType: 'text/plain',
334
+ })
335
+ ).rejects.toThrow('Failed to download file: Not Found');
336
+ });
337
+ });
338
+
339
+ describe('File object handling', () => {
340
+ it('should handle File objects correctly', async () => {
341
+ // Reset mocks
342
+ vi.clearAllMocks();
343
+
344
+ // Mock successful S3 upload
345
+ (mockClient.files.createPresignedURL as any).mockResolvedValueOnce({
346
+ key: 'test-key',
347
+ type: 'new',
348
+ new_presigned_url: 'https://s3.example.com/upload',
349
+ });
350
+
351
+ // Mock successful PUT to S3
352
+ mockFetch.mockResolvedValueOnce({ ok: true });
353
+
354
+ const fileContent = 'test file content';
355
+ const file = new File([fileContent], 'test.txt', { type: 'text/plain' });
356
+
357
+ const result = await getFileDataAfterUploadingToS3(file, {
358
+ toolSlug: 'test-tool',
359
+ toolkitSlug: 'test-toolkit',
360
+ client: mockClient,
361
+ });
362
+
363
+ expect(result.name).toBe('test.txt');
364
+ expect(result.mimetype).toBe('text/plain');
365
+ });
366
+ });
367
+
368
+ // Note: Local file handling tests are covered in the existing fileModifiers.test.ts
369
+
370
+ describe('Error handling', () => {
371
+ it('should handle fetch errors for URLs', async () => {
372
+ mockFetch.mockResolvedValue({
373
+ ok: false,
374
+ statusText: 'Internal Server Error',
375
+ });
376
+
377
+ await expect(
378
+ getFileDataAfterUploadingToS3('https://example.com/file.pdf', {
379
+ toolSlug: 'test-tool',
380
+ toolkitSlug: 'test-toolkit',
381
+ client: mockClient,
382
+ })
383
+ ).rejects.toThrow('Failed to fetch file: Internal Server Error');
384
+ });
385
+
386
+ it('should handle S3 upload errors', async () => {
387
+ mockFetch.mockResolvedValueOnce({
388
+ ok: true,
389
+ arrayBuffer: () => Promise.resolve(new ArrayBuffer(10)),
390
+ headers: new Map([['content-type', 'application/pdf']]),
391
+ });
392
+
393
+ (mockClient.files.createPresignedURL as any).mockResolvedValue({
394
+ key: 'test-key',
395
+ type: 'new',
396
+ new_presigned_url: 'https://s3.example.com/upload',
397
+ });
398
+
399
+ // Mock failed S3 upload
400
+ mockFetch.mockResolvedValueOnce({
401
+ ok: false,
402
+ statusText: 'Upload Failed',
403
+ });
404
+
405
+ await expect(
406
+ getFileDataAfterUploadingToS3('https://example.com/file.pdf', {
407
+ toolSlug: 'test-tool',
408
+ toolkitSlug: 'test-toolkit',
409
+ client: mockClient,
410
+ })
411
+ ).rejects.toThrow('Failed to upload file to S3: Upload Failed');
412
+ });
413
+
414
+ it('should handle invalid file types', async () => {
415
+ await expect(
416
+ getFileDataAfterUploadingToS3(123 as any, {
417
+ toolSlug: 'test-tool',
418
+ toolkitSlug: 'test-toolkit',
419
+ client: mockClient,
420
+ })
421
+ ).rejects.toThrow('Invalid file type');
422
+ });
423
+ });
424
+});