Skip to content

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

Fix: Strip query params in URL for uploaded files (#1867)

Musthaq Ahamadee12d2536416
diff --git a/.changeset/khaki-birds-lay.md b/.changeset/khaki-birds-lay.md
new file mode 100644
index 000000000..2cee0168e
--- /dev/null
+++ b/.changeset/khaki-birds-lay.md
@@ -0,0 +1,5 @@
+---
+'@composio/core': patch
+---
+
+Fix long filenames while uploading files from URL
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 97578f41f..e63fd5e91 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -270,6 +270,22 @@ importers:
specifier: ^5.8.3
version: 5.8.3
+ ts/examples/file-handling:
+ dependencies:
+ '@composio/core':
+ specifier: workspace:*
+ version: link:../../packages/core
+ dotenv:
+ specifier: ^16.4.1
+ version: 16.6.1
+ devDependencies:
+ '@types/bun':
+ specifier: ^1.2.9
+ version: 1.2.16
+ typescript:
+ specifier: ^5.8.3
+ version: 5.8.3
+
ts/examples/google:
dependencies:
'@composio/core':
@@ -8745,7 +8761,7 @@ snapshots:
cohere-ai: 7.17.1
date-fns: 3.6.0
- dotenv: 16.5.0
+ dotenv: 16.6.1
hono: 4.8.0
json-schema: 0.4.0
@@ -11121,14 +11137,6 @@ snapshots:
chai: 5.2.0
tinyrainbow: 2.0.0
- dependencies:
- '@vitest/spy': 3.2.4
- estree-walker: 3.0.3
- magic-string: 0.30.17
- optionalDependencies:
-
dependencies:
'@vitest/spy': 3.2.4
@@ -11137,14 +11145,6 @@ snapshots:
optionalDependencies:
- dependencies:
- '@vitest/spy': 3.2.4
- estree-walker: 3.0.3
- magic-string: 0.30.17
- optionalDependencies:
-
'@vitest/[email protected]':
dependencies:
tinyrainbow: 2.0.0
@@ -11174,7 +11174,7 @@ snapshots:
sirv: 3.0.1
tinyglobby: 0.2.14
tinyrainbow: 2.0.0
'@vitest/[email protected]':
dependencies:
@@ -14336,7 +14336,7 @@ snapshots:
dependencies:
'@types/chai': 5.2.2
'@vitest/expect': 3.2.4
'@vitest/pretty-format': 3.2.4
'@vitest/runner': 3.2.4
'@vitest/snapshot': 3.2.4
@@ -14420,7 +14420,7 @@ snapshots:
dependencies:
'@types/chai': 5.2.2
'@vitest/expect': 3.2.4
'@vitest/pretty-format': 3.2.4
'@vitest/runner': 3.2.4
'@vitest/snapshot': 3.2.4
diff --git a/ts/examples/file-handling/.env.example b/ts/examples/file-handling/.env.example
new file mode 100644
index 000000000..0e728257c
--- /dev/null
+++ b/ts/examples/file-handling/.env.example
@@ -0,0 +1,6 @@
+# Composio API Key - Get it from https://app.composio.dev
+COMPOSIO_API_KEY=your_composio_api_key_here
+
+# Add other environment variables as needed for your example
+# OPENAI_API_KEY=your_openai_api_key_here
+# ANTHROPIC_API_KEY=your_anthropic_api_key_here
diff --git a/ts/examples/file-handling/README.md b/ts/examples/file-handling/README.md
new file mode 100644
index 000000000..eebeb0e33
--- /dev/null
+++ b/ts/examples/file-handling/README.md
@@ -0,0 +1,53 @@
+# File-handling Example
+
+This example demonstrates how to use Composio SDK for file-handling.
+
+## Setup
+
+1. **Install dependencies:**
+ ```bash
+ pnpm install
+ ```
+
+2. **Configure environment:**
+ ```bash
+ cp .env.example .env
+ ```
+
+ Then edit `.env` and add your API keys:
+ - `COMPOSIO_API_KEY`: Get it from [Composio Dashboard](https://app.composio.dev)
+
+## Running the Example
+
+```bash
+# Run the example
+pnpm start
+
+# Run in development mode (with file watching)
+pnpm dev
+```
+
+## What This Example Does
+
+- Initializes Composio SDK
+- Fetches available tools
+- Demonstrates basic usage patterns
+
+## Customization
+
+Edit `src/index.ts` to:
+- Add specific apps you want to integrate with
+- Implement your business logic
+- Add error handling and logging
+
+## Related Examples
+
+- [OpenAI Example](../openai) - Shows integration with OpenAI
+- [LangChain Example](../langchain) - Shows integration with LangChain
+- [More Examples](../) - Browse all available examples
+
+## Support
+
+- [Documentation](https://docs.composio.dev)
+- [Discord Community](https://discord.gg/composio)
+- [GitHub Issues](https://github.com/composio/composio/issues)
diff --git a/ts/examples/file-handling/package.json b/ts/examples/file-handling/package.json
new file mode 100644
index 000000000..f13c9400b
--- /dev/null
+++ b/ts/examples/file-handling/package.json
@@ -0,0 +1,24 @@
+{
+ "name": "file-handling-example",
+ "private": true,
+ "version": "0.1.0",
+ "description": "Example project for File-handling",
+ "main": "src/index.ts",
+ "scripts": {
+ "start": "bun src/index.ts",
+ "dev": "bun --watch src/index.ts",
+ "test": "echo \"Error: no test specified\" && exit 1"
+ },
+ "keywords": ["composio", "example", "file-handling"],
+ "author": "",
+ "license": "ISC",
+ "packageManager": "[email protected]",
+ "dependencies": {
+ "@composio/core": "workspace:*",
+ "dotenv": "^16.4.1"
+ },
+ "devDependencies": {
+ "@types/bun": "^1.2.9",
+ "typescript": "^5.8.3"
+ }
+}
diff --git a/ts/examples/file-handling/pepe-silvia.png b/ts/examples/file-handling/pepe-silvia.png
new file mode 100644
index 000000000..eac7b9a44
Binary files /dev/null and b/ts/examples/file-handling/pepe-silvia.png differ
diff --git a/ts/examples/file-handling/src/index.ts b/ts/examples/file-handling/src/index.ts
new file mode 100644
index 000000000..dca8afb7e
--- /dev/null
+++ b/ts/examples/file-handling/src/index.ts
@@ -0,0 +1,55 @@
+/**
+ * File-handling Example
+ *
+ * This example demonstrates how to use Composio SDK for file-handling.
+ *
+ * Prerequisites:
+ * 1. Set up your COMPOSIO_API_KEY in the .env file
+ * 3. Run the example: pnpm start
+ */
+
+import { Composio } from '@composio/core';
+import 'dotenv/config';
+import path from 'path';
+
+/**
+ * Initialize Composio
+ */
+const composio = new Composio({
+ apiKey: process.env.COMPOSIO_API_KEY,
+});
+
+/**
+ * Main function to run the example
+ */
+async function main() {
+ try {
+ console.log('πŸš€ Starting File-handling Example...');
+
+ // Get available tools
+ const tools = await composio.tools.get('default', 'GMAIL_SEND_EMAIL');
+
+ console.log(`βœ… Found ${tools.length} tools`);
+
+ const filePath = path.join(__dirname, '..', 'pepe-silvia.png');
+ console.log(`Sending file from ${filePath}`);
+
+ const result = await composio.tools.execute('GMAIL_SEND_EMAIL', {
+ userId: 'default',
+ arguments: {
+ attachment: filePath,
+ recipient_email: '[email protected]',
+ user_id: 'me',
+ body: 'Hello, this is a test email with a file attachment.',
+ subject: 'Test Email with Attachment',
+ },
+ });
+
+ console.log(result);
+ } catch (error) {
+ console.error('❌ Error running example:', error);
+ }
+}
+
+// Run the example
+main().catch(console.error);
diff --git a/ts/examples/file-handling/src/url.ts b/ts/examples/file-handling/src/url.ts
new file mode 100644
index 000000000..7866b5d79
--- /dev/null
+++ b/ts/examples/file-handling/src/url.ts
@@ -0,0 +1,58 @@
+/**
+ * File-handling Example
+ *
+ * This example demonstrates how to use Composio SDK for file-handling.
+ *
+ * Prerequisites:
+ * 1. Set up your COMPOSIO_API_KEY in the .env file
+ * 3. Run the example: pnpm start
+ */
+
+import { Composio } from '@composio/core';
+import { file } from 'bun';
+import 'dotenv/config';
+import path from 'path';
+
+/**
+ * Initialize Composio
+ */
+const composio = new Composio({
+ apiKey: process.env.COMPOSIO_API_KEY,
+});
+
+/**
+ * Main function to run the example
+ */
+async function main() {
+ try {
+ console.log('πŸš€ Starting File-handling Example...');
+
+ // Get available tools
+ const tools = await composio.tools.get('default', 'GMAIL_SEND_EMAIL');
+
+ console.log(`βœ… Found ${tools.length} tools`);
+
+ const filePath =
+ 'https://avatars.githubusercontent.com/u/128464815?v=4&id=sdsdjljkljlsdfjslkfjlksdjflkdjflksdjflksdfwfowdfk;dfkl;sdfk;slkf;lskf;lsdkf;lk';
+
+ console.log(`Sending file from ${filePath}`);
+
+ const result = await composio.tools.execute('GMAIL_SEND_EMAIL', {
+ userId: 'default',
+ arguments: {
+ attachment: filePath,
+ recipient_email: '[email protected]',
+ user_id: 'me',
+ body: 'Hello, this is a test email with a file attachment.',
+ subject: 'Test Email with Attachment',
+ },
+ });
+
+ console.log(JSON.stringify(result, null, 2));
+ } catch (error) {
+ console.error('❌ Error running example:', error);
+ }
+}
+
+// Run the example
+main().catch(console.error);
diff --git a/ts/examples/file-handling/tsconfig.json b/ts/examples/file-handling/tsconfig.json
new file mode 100644
index 000000000..158c157c8
--- /dev/null
+++ b/ts/examples/file-handling/tsconfig.json
@@ -0,0 +1,16 @@
+{
+ "compilerOptions": {
+ "target": "ESNext",
+ "module": "ESNext",
+ "declaration": true,
+ "declarationDir": "./dist",
+ "outDir": "./dist",
+ "rootDir": "./src",
+ "strict": true,
+ "esModuleInterop": true,
+ "moduleResolution": "node",
+ "skipLibCheck": true,
+ "resolveJsonModule": true
+ },
+ "include": ["src"]
+}
diff --git a/ts/packages/core/src/utils/fileUtils.ts b/ts/packages/core/src/utils/fileUtils.ts
index 9d18d6cb9..01ffe37c5 100644
--- a/ts/packages/core/src/utils/fileUtils.ts
+++ b/ts/packages/core/src/utils/fileUtils.ts
@@ -4,6 +4,93 @@ import ComposioClient from '@composio/client';
import { COMPOSIO_DIR, TEMP_FILES_DIRECTORY_NAME } from './constants';
import logger from './logger';
import { FileDownloadData, FileUploadData } from '../types/files.types';
+import { getRandomShortId, getRandomUUID } from './uuid';
+
+// Helper function to get file extension from MIME type
+const getExtensionFromMimeType = (mimeType: string): string => {
+ const mimeToExt: Record<string, string> = {
+ 'text/plain': 'txt',
+ 'text/html': 'html',
+ 'text/css': 'css',
+ 'text/javascript': 'js',
+ 'application/json': 'json',
+ 'application/xml': 'xml',
+ 'application/pdf': 'pdf',
+ 'application/zip': 'zip',
+ 'application/x-zip-compressed': 'zip',
+ 'application/gzip': 'gz',
+ 'application/x-tar': 'tar',
+ 'application/msword': 'doc',
+ 'application/vnd.openxmlformats-officedocument.wordprocessingml.document': 'docx',
+ 'application/vnd.ms-excel': 'xls',
+ 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet': 'xlsx',
+ 'application/vnd.ms-powerpoint': 'ppt',
+ 'application/vnd.openxmlformats-officedocument.presentationml.presentation': 'pptx',
+ 'image/jpeg': 'jpg',
+ 'image/jpg': 'jpg',
+ 'image/png': 'png',
+ 'image/gif': 'gif',
+ 'image/svg+xml': 'svg',
+ 'image/webp': 'webp',
+ 'image/bmp': 'bmp',
+ 'image/tiff': 'tiff',
+ 'audio/mpeg': 'mp3',
+ 'audio/wav': 'wav',
+ 'audio/ogg': 'ogg',
+ 'video/mp4': 'mp4',
+ 'video/mpeg': 'mpeg',
+ 'video/quicktime': 'mov',
+ 'video/x-msvideo': 'avi',
+ 'video/webm': 'webm',
+ };
+
+ // Clean the MIME type by removing parameters (e.g., "text/plain; charset=utf-8" -> "text/plain")
+ const cleanMimeType = mimeType.split(';')[0].toLowerCase().trim();
+
+ // Try exact match first
+ if (mimeToExt[cleanMimeType]) {
+ return mimeToExt[cleanMimeType];
+ }
+
+ // Try to extract from the MIME type (e.g., "image/png" -> "png")
+ const parts = cleanMimeType.split('/');
+ if (parts.length === 2) {
+ const subtype = parts[1].toLowerCase();
+ const cleanSubtype = subtype; // Already cleaned above
+
+ // Handle structured MIME types with + (e.g., "application/vnd.api+json" -> "json")
+ if (cleanSubtype.includes('+')) {
+ const plusParts = cleanSubtype.split('+');
+ const prefix = plusParts[0];
+ const suffix = plusParts[plusParts.length - 1]; // Get the part after the last +
+
+ // For specific formats, prefer the prefix (e.g., "atom+xml" -> "atom")
+ const knownPrefixes = ['svg', 'atom', 'rss'];
+ if (knownPrefixes.includes(prefix)) {
+ return prefix;
+ }
+
+ // For common structured suffixes, use the suffix (e.g., "vnd.api+json" -> "json")
+ const structuredSuffixes = ['json', 'xml', 'yaml', 'zip', 'gzip'];
+ if (structuredSuffixes.includes(suffix)) {
+ return suffix;
+ }
+
+ // Default to the suffix for other structured types
+ return suffix;
+ }
+
+ return cleanSubtype || 'txt';
+ }
+
+ return 'txt'; // Default fallback
+};
+
+// Helper function to generate a filename with timestamp and random ID
+const generateTimestampedFilename = (extension: string, prefix?: string): string => {
+ const basePrefix = prefix || 'file_ts';
+ return `${basePrefix}${Date.now()}${getRandomShortId()}.${extension}`;
+};
const readFileContent = async (
path: string
@@ -11,7 +98,7 @@ const readFileContent = async (
try {
const content = require('fs').readFileSync(path);
return {
- fileName: pathModule.basename(path) || `file_${Date.now()}.${path.split('.').pop()}`,
+ fileName: generateTimestampedFilename(path.split('.').pop() || 'txt'),
content: content.toString('base64'),
mimeType: 'application/octet-stream',
};
@@ -30,10 +117,30 @@ const readFileContentFromURL = async (
const arrayBuffer = await response.arrayBuffer();
const content = Buffer.from(arrayBuffer);
const mimeType = response.headers.get('content-type') || 'application/octet-stream';
+
+ // Extract clean filename from URL, removing query parameters
+ const url = new URL(path);
+ const pathname = url.pathname;
+ let fileName = pathModule.basename(pathname);
+
+ // If no filename from URL, generate one with appropriate extension
+ if (!fileName || fileName === '/') {
+ // Try to get extension from mimeType
+ const extension = getExtensionFromMimeType(mimeType);
+ fileName = generateTimestampedFilename(extension);
+ } else {
+ // If filename has no extension, try to add one from mimeType
+ const hasExtension = fileName.includes('.');
+ if (!hasExtension) {
+ const extension = getExtensionFromMimeType(mimeType);
+ fileName = generateTimestampedFilename(extension);
+ }
+ }
+
return {
content: content.toString('base64'),
mimeType,
- fileName: pathModule.basename(path) || `file_${Date.now()}.${path.split('.').pop()}`,
+ fileName,
};
};
@@ -152,8 +259,8 @@ export const downloadFileFromS3 = async ({
}
const data = await response.arrayBuffer();
- const extension = mimeType.split('/')[1] || 'txt';
- const fileName = `${toolSlug}_${Date.now()}.${extension}`;
+ const extension = getExtensionFromMimeType(mimeType);
+ const fileName = generateTimestampedFilename(extension, `${toolSlug}_`);
const filePath = saveFile(fileName, Buffer.from(data), true);
return {
name: fileName,
diff --git a/ts/packages/core/src/utils/uuid.ts b/ts/packages/core/src/utils/uuid.ts
index c302f70f3..72ffa904f 100644
--- a/ts/packages/core/src/utils/uuid.ts
+++ b/ts/packages/core/src/utils/uuid.ts
@@ -3,3 +3,7 @@ import { v4 as uuidv4 } from 'uuid';
export function getRandomUUID(): string {
return uuidv4();
}
+
+export function getRandomShortId(): string {
+ return getRandomUUID().slice(0, 8).replace(/-/g, '');
+}
diff --git a/ts/packages/core/test/utils/fileUtils.test.ts b/ts/packages/core/test/utils/fileUtils.test.ts
new file mode 100644
index 000000000..029ab4a5b
--- /dev/null
+++ b/ts/packages/core/test/utils/fileUtils.test.ts
@@ -0,0 +1,424 @@
+import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
+import { getFileDataAfterUploadingToS3, downloadFileFromS3 } from '../../src/utils/fileUtils';
+import ComposioClient from '@composio/client';
+import * as uuid from '../../src/utils/uuid';
+
+// Mock the uuid module
+vi.mock('../../src/utils/uuid', () => ({
+ getRandomShortId: vi.fn(() => 'abc12345'),
+ getRandomUUID: vi.fn(() => '12345678-1234-1234-1234-123456789012'),
+}));
+
+// Mock fs module
+vi.mock('fs', () => ({
+ default: {
+ readFileSync: vi.fn(),
+ writeFileSync: vi.fn(),
+ existsSync: vi.fn(() => true),
+ mkdirSync: vi.fn(),
+ },
+ readFileSync: vi.fn(),
+ writeFileSync: vi.fn(),
+ existsSync: vi.fn(() => true),
+ mkdirSync: vi.fn(),
+}));
+
+// Mock os module
+vi.mock('os', () => ({
+ default: {
+ homedir: vi.fn(() => '/home/test'),
+ },
+ homedir: vi.fn(() => '/home/test'),
+}));
+
+// Mock path module
+vi.mock('path', () => ({
+ default: {
+ join: vi.fn((...args) => args.join('/')),
+ basename: vi.fn(path => path.split('/').pop()),
+ },
+ join: vi.fn((...args) => args.join('/')),
+ basename: vi.fn(path => path.split('/').pop()),
+}));
+
+// Mock global fetch
+const mockFetch = vi.fn();
+global.fetch = mockFetch;
+
+describe('fileUtils', () => {
+ let mockClient: ComposioClient;
+
+ beforeEach(() => {
+ mockClient = {
+ files: {
+ createPresignedURL: vi.fn(),
+ },
+ } as unknown as ComposioClient;
+
+ vi.clearAllMocks();
+
+ // Mock Date.now to return a consistent timestamp
+ vi.spyOn(Date, 'now').mockReturnValue(1640995200000); // 2022-01-01 00:00:00
+ });
+
+ afterEach(() => {
+ vi.restoreAllMocks();
+ });
+
+ describe('URL filename generation with query parameters', () => {
+ beforeEach(() => {
+ // Mock successful fetch response
+ mockFetch.mockResolvedValue({
+ ok: true,
+ arrayBuffer: () => Promise.resolve(new ArrayBuffer(10)),
+ headers: new Map([['content-type', 'application/pdf']]),
+ });
+
+ // Mock successful S3 upload
+ (mockClient.files.createPresignedURL as any).mockResolvedValue({
+ key: 'test-key',
+ type: 'new',
+ new_presigned_url: 'https://s3.example.com/upload',
+ });
+
+ // Mock successful PUT to S3
+ mockFetch.mockImplementation((url, options) => {
+ if (options?.method === 'PUT') {
+ return Promise.resolve({ ok: true });
+ }
+ // For the initial file fetch
+ return Promise.resolve({
+ ok: true,
+ arrayBuffer: () => Promise.resolve(new ArrayBuffer(10)),
+ headers: new Map([['content-type', 'application/pdf']]),
+ });
+ });
+ });
+
+ it('should handle URLs with query parameters correctly', async () => {
+ const urlWithQuery =
+ 'https://example.com/document.pdf?param1=value1&param2=value2&token=abc123';
+
+ const result = await getFileDataAfterUploadingToS3(urlWithQuery, {
+ toolSlug: 'test-tool',
+ toolkitSlug: 'test-toolkit',
+ client: mockClient,
+ });
+
+ expect(result.name).toBe('document.pdf');
+ expect(mockFetch).toHaveBeenCalledWith(urlWithQuery);
+ });
+
+ it('should generate filename when URL has no filename', async () => {
+ const urlWithoutFilename = 'https://example.com/?download=true&format=pdf';
+
+ const result = await getFileDataAfterUploadingToS3(urlWithoutFilename, {
+ toolSlug: 'test-tool',
+ toolkitSlug: 'test-toolkit',
+ client: mockClient,
+ });
+
+ expect(result.name).toBe('file_ts1640995200000abc12345.pdf');
+ });
+
+ it('should generate filename when URL path ends with slash', async () => {
+ const urlEndingWithSlash = 'https://example.com/folder/?type=document';
+
+ const result = await getFileDataAfterUploadingToS3(urlEndingWithSlash, {
+ toolSlug: 'test-tool',
+ toolkitSlug: 'test-toolkit',
+ client: mockClient,
+ });
+
+ expect(result.name).toBe('file_ts1640995200000abc12345.pdf');
+ });
+
+ it('should add extension to filename without extension based on MIME type', async () => {
+ const urlWithoutExtension = 'https://example.com/document?format=pdf';
+
+ const result = await getFileDataAfterUploadingToS3(urlWithoutExtension, {
+ toolSlug: 'test-tool',
+ toolkitSlug: 'test-toolkit',
+ client: mockClient,
+ });
+
+ expect(result.name).toBe('file_ts1640995200000abc12345.pdf');
+ });
+ });
+
+ describe('MIME type extension handling', () => {
+ it('should handle structured MIME types with + correctly', async () => {
+ const testCases = [
+ { mimeType: 'application/vnd.api+json', expectedExt: 'json' },
+ { mimeType: 'application/ld+json', expectedExt: 'json' },
+ { mimeType: 'image/svg+xml', expectedExt: 'svg' },
+ { mimeType: 'application/atom+xml', expectedExt: 'atom' },
+ { mimeType: 'application/rss+xml', expectedExt: 'rss' },
+ { mimeType: 'application/hal+json', expectedExt: 'json' },
+ { mimeType: 'application/vnd.collection+json', expectedExt: 'json' },
+ { mimeType: 'application/vnd.custom+zip', expectedExt: 'zip' },
+ ];
+
+ for (const { mimeType, expectedExt } of testCases) {
+ // Reset mocks for each iteration
+ vi.clearAllMocks();
+
+ // Mock successful fetch response
+ mockFetch.mockResolvedValueOnce({
+ ok: true,
+ arrayBuffer: () => Promise.resolve(new ArrayBuffer(10)),
+ headers: new Map([['content-type', mimeType]]),
+ });
+
+ // Mock successful S3 upload
+ (mockClient.files.createPresignedURL as any).mockResolvedValueOnce({
+ key: 'test-key',
+ type: 'new',
+ new_presigned_url: 'https://s3.example.com/upload',
+ });
+
+ // Mock successful PUT to S3
+ mockFetch.mockResolvedValueOnce({ ok: true });
+
+ const result = await getFileDataAfterUploadingToS3('https://example.com/file', {
+ toolSlug: 'test-tool',
+ toolkitSlug: 'test-toolkit',
+ client: mockClient,
+ });
+
+ expect(result.name).toBe(`file_ts1640995200000abc12345.${expectedExt}`);
+ }
+ });
+
+ it('should handle MIME types with parameters', async () => {
+ // Reset mocks
+ vi.clearAllMocks();
+
+ mockFetch.mockResolvedValueOnce({
+ ok: true,
+ arrayBuffer: () => Promise.resolve(new ArrayBuffer(10)),
+ headers: new Map([['content-type', 'text/plain; charset=utf-8']]),
+ });
+
+ // Mock successful S3 upload
+ (mockClient.files.createPresignedURL as any).mockResolvedValueOnce({
+ key: 'test-key',
+ type: 'new',
+ new_presigned_url: 'https://s3.example.com/upload',
+ });
+
+ // Mock successful PUT to S3
+ mockFetch.mockResolvedValueOnce({ ok: true });
+
+ const result = await getFileDataAfterUploadingToS3('https://example.com/file', {
+ toolSlug: 'test-tool',
+ toolkitSlug: 'test-toolkit',
+ client: mockClient,
+ });
+
+ expect(result.name).toBe('file_ts1640995200000abc12345.txt');
+ });
+
+ it('should handle common MIME types', async () => {
+ const testCases = [
+ { mimeType: 'application/pdf', expectedExt: 'pdf' },
+ { mimeType: 'image/jpeg', expectedExt: 'jpg' },
+ { mimeType: 'image/png', expectedExt: 'png' },
+ { mimeType: 'text/html', expectedExt: 'html' },
+ { mimeType: 'application/json', expectedExt: 'json' },
+ { mimeType: 'video/mp4', expectedExt: 'mp4' },
+ ];
+
+ for (const { mimeType, expectedExt } of testCases) {
+ // Reset mocks for each iteration
+ vi.clearAllMocks();
+
+ mockFetch.mockResolvedValueOnce({
+ ok: true,
+ arrayBuffer: () => Promise.resolve(new ArrayBuffer(10)),
+ headers: new Map([['content-type', mimeType]]),
+ });
+
+ // Mock successful S3 upload
+ (mockClient.files.createPresignedURL as any).mockResolvedValueOnce({
+ key: 'test-key',
+ type: 'new',
+ new_presigned_url: 'https://s3.example.com/upload',
+ });
+
+ // Mock successful PUT to S3
+ mockFetch.mockResolvedValueOnce({ ok: true });
+
+ const result = await getFileDataAfterUploadingToS3('https://example.com/file', {
+ toolSlug: 'test-tool',
+ toolkitSlug: 'test-toolkit',
+ client: mockClient,
+ });
+
+ expect(result.name).toBe(`file_ts1640995200000abc12345.${expectedExt}`);
+ }
+ });
+
+ it('should fallback to generic extraction for unknown MIME types', async () => {
+ // Reset mocks
+ vi.clearAllMocks();
+
+ mockFetch.mockResolvedValueOnce({
+ ok: true,
+ arrayBuffer: () => Promise.resolve(new ArrayBuffer(10)),
+ headers: new Map([['content-type', 'application/custom-format']]),
+ });
+
+ // Mock successful S3 upload
+ (mockClient.files.createPresignedURL as any).mockResolvedValueOnce({
+ key: 'test-key',
+ type: 'new',
+ new_presigned_url: 'https://s3.example.com/upload',
+ });
+
+ // Mock successful PUT to S3
+ mockFetch.mockResolvedValueOnce({ ok: true });
+
+ const result = await getFileDataAfterUploadingToS3('https://example.com/file', {
+ toolSlug: 'test-tool',
+ toolkitSlug: 'test-toolkit',
+ client: mockClient,
+ });
+
+ expect(result.name).toBe('file_ts1640995200000abc12345.custom-format');
+ });
+ });
+
+ describe('downloadFileFromS3', () => {
+ it('should generate filename with tool slug prefix', async () => {
+ mockFetch.mockResolvedValue({
+ ok: true,
+ arrayBuffer: () => Promise.resolve(new ArrayBuffer(10)),
+ });
+
+ const result = await downloadFileFromS3({
+ toolSlug: 'github',
+ s3Url: 'https://s3.example.com/file.txt',
+ mimeType: 'text/plain',
+ });
+
+ expect(result.name).toBe('github_1640995200000abc12345.txt');
+ });
+
+ it('should handle MIME types with + in downloadFileFromS3', async () => {
+ mockFetch.mockResolvedValue({
+ ok: true,
+ arrayBuffer: () => Promise.resolve(new ArrayBuffer(10)),
+ });
+
+ const result = await downloadFileFromS3({
+ toolSlug: 'api-tool',
+ s3Url: 'https://s3.example.com/data.json',
+ mimeType: 'application/vnd.api+json',
+ });
+
+ expect(result.name).toBe('api-tool_1640995200000abc12345.json');
+ });
+
+ it('should handle download failure', async () => {
+ mockFetch.mockResolvedValue({
+ ok: false,
+ statusText: 'Not Found',
+ });
+
+ await expect(
+ downloadFileFromS3({
+ toolSlug: 'test-tool',
+ s3Url: 'https://s3.example.com/nonexistent.txt',
+ mimeType: 'text/plain',
+ })
+ ).rejects.toThrow('Failed to download file: Not Found');
+ });
+ });
+
+ describe('File object handling', () => {
+ it('should handle File objects correctly', async () => {
+ // Reset mocks
+ vi.clearAllMocks();
+
+ // Mock successful S3 upload
+ (mockClient.files.createPresignedURL as any).mockResolvedValueOnce({
+ key: 'test-key',
+ type: 'new',
+ new_presigned_url: 'https://s3.example.com/upload',
+ });
+
+ // Mock successful PUT to S3
+ mockFetch.mockResolvedValueOnce({ ok: true });
+
+ const fileContent = 'test file content';
+ const file = new File([fileContent], 'test.txt', { type: 'text/plain' });
+
+ const result = await getFileDataAfterUploadingToS3(file, {
+ toolSlug: 'test-tool',
+ toolkitSlug: 'test-toolkit',
+ client: mockClient,
+ });
+
+ expect(result.name).toBe('test.txt');
+ expect(result.mimetype).toBe('text/plain');
+ });
+ });
+
+ // Note: Local file handling tests are covered in the existing fileModifiers.test.ts
+
+ describe('Error handling', () => {
+ it('should handle fetch errors for URLs', async () => {
+ mockFetch.mockResolvedValue({
+ ok: false,
+ statusText: 'Internal Server Error',
+ });
+
+ await expect(
+ getFileDataAfterUploadingToS3('https://example.com/file.pdf', {
+ toolSlug: 'test-tool',
+ toolkitSlug: 'test-toolkit',
+ client: mockClient,
+ })
+ ).rejects.toThrow('Failed to fetch file: Internal Server Error');
+ });
+
+ it('should handle S3 upload errors', async () => {
+ mockFetch.mockResolvedValueOnce({
+ ok: true,
+ arrayBuffer: () => Promise.resolve(new ArrayBuffer(10)),
+ headers: new Map([['content-type', 'application/pdf']]),
+ });
+
+ (mockClient.files.createPresignedURL as any).mockResolvedValue({
+ key: 'test-key',
+ type: 'new',
+ new_presigned_url: 'https://s3.example.com/upload',
+ });
+
+ // Mock failed S3 upload
+ mockFetch.mockResolvedValueOnce({
+ ok: false,
+ statusText: 'Upload Failed',
+ });
+
+ await expect(
+ getFileDataAfterUploadingToS3('https://example.com/file.pdf', {
+ toolSlug: 'test-tool',
+ toolkitSlug: 'test-toolkit',
+ client: mockClient,
+ })
+ ).rejects.toThrow('Failed to upload file to S3: Upload Failed');
+ });
+
+ it('should handle invalid file types', async () => {
+ await expect(
+ getFileDataAfterUploadingToS3(123 as any, {
+ toolSlug: 'test-tool',
+ toolkitSlug: 'test-toolkit',
+ client: mockClient,
+ })
+ ).rejects.toThrow('Invalid file type');
+ });
+ });
+});