diff --git a/src/components/Shakespeare/ChatPane.tsx b/src/components/Shakespeare/ChatPane.tsx index 73b4f4fe..f6bbcbae 100644 --- a/src/components/Shakespeare/ChatPane.tsx +++ b/src/components/Shakespeare/ChatPane.tsx @@ -40,7 +40,6 @@ import OpenAI from 'openai'; import { makeSystemPrompt } from '@/lib/system'; import { assistantContentEmpty } from '@/lib/ai-messages'; import { saveFileToTmp } from '@/lib/fileUtils'; -import { validateFiles, DEFAULT_MAX_SIZE, DEFAULT_ACCEPT } from '@/lib/fileValidation'; interface ChatPaneProps { projectId: string; @@ -358,39 +357,21 @@ export const ChatPane = forwardRef(({ const files = Array.from(e.dataTransfer.files); if (files.length === 0) return; - // Validate files using the same logic as FileAttachment - const { validFiles, errors } = validateFiles(files, { - maxSize: DEFAULT_MAX_SIZE, - accept: DEFAULT_ACCEPT, - maxFiles: 10 // Limit to 10 files at once - }); + // Add all files without validation + setAttachedFiles(prev => [...prev, ...files]); - // Add valid files to attachments - if (validFiles.length > 0) { - setAttachedFiles(prev => [...prev, ...validFiles]); - - // Announce files added via toast - if (validFiles.length === 1) { - toast({ - title: "File added", - description: `${validFiles[0].name} has been added to attachments.`, - }); - } else { - toast({ - title: "Files added", - description: `${validFiles.length} files have been added to attachments.`, - }); - } - } - - // Show errors for invalid files - errors.forEach(error => { + // Announce files added via toast + if (files.length === 1) { toast({ - title: "File upload error", - description: error.error, - variant: "destructive", + title: "File added", + description: `${files[0].name} has been added to attachments.`, }); - }); + } else { + toast({ + title: "Files added", + description: `${files.length} files have been added to attachments.`, + }); + } }; const handleSend = async () => { diff --git a/src/lib/fileValidation.test.ts b/src/lib/fileValidation.test.ts deleted file mode 100644 index ece41b1c..00000000 --- a/src/lib/fileValidation.test.ts +++ /dev/null @@ -1,148 +0,0 @@ -import { describe, it, expect } from 'vitest'; -import { - validateFile, - validateFiles, - formatFileSize, - isImageFile, - isTextFile, - DEFAULT_MAX_SIZE -} from './fileValidation'; - -describe('fileValidation', () => { - describe('validateFile', () => { - it('should return null for valid file', () => { - const file = new File(['test content'], 'test.txt', { type: 'text/plain' }); - const result = validateFile(file); - expect(result).toBeNull(); - }); - - it('should return error for file that is too large', () => { - const largeFile = new File(['x'.repeat(DEFAULT_MAX_SIZE + 1)], 'large.txt', { type: 'text/plain' }); - const result = validateFile(largeFile, { maxSize: DEFAULT_MAX_SIZE }); - - expect(result).not.toBeNull(); - expect(result?.error).toContain('is too large'); - expect(result?.file).toBe(largeFile); - }); - - it('should accept file within size limit', () => { - const file = new File(['test content'], 'small.txt', { type: 'text/plain' }); - const result = validateFile(file, { maxSize: 1024 }); // 1KB limit - - expect(result).toBeNull(); - }); - - it('should return error for unsupported file type', () => { - const file = new File(['test content'], 'test.exe', { type: 'application/x-executable' }); - const result = validateFile(file, { accept: 'image/*,.txt' }); - - expect(result).not.toBeNull(); - expect(result?.error).toContain('not a supported type'); - }); - - it('should accept image files when image/* is specified', () => { - const imageFile = new File(['test content'], 'test.jpg', { type: 'image/jpeg' }); - const result = validateFile(imageFile, { accept: 'image/*' }); - - expect(result).toBeNull(); - }); - - it('should accept files with specific extension', () => { - const textFile = new File(['test content'], 'test.txt', { type: 'text/plain' }); - const result = validateFile(textFile, { accept: '.txt' }); - - expect(result).toBeNull(); - }); - - it('should accept all files when accept is */*', () => { - const file = new File(['test content'], 'test.any', { type: 'application/unknown' }); - const result = validateFile(file, { accept: '*/*' }); - - expect(result).toBeNull(); - }); - }); - - describe('validateFiles', () => { - it('should return all valid files and no errors for valid input', () => { - const files = [ - new File(['content1'], 'file1.txt', { type: 'text/plain' }), - new File(['content2'], 'file2.txt', { type: 'text/plain' }), - ]; - - const result = validateFiles(files); - - expect(result.validFiles).toHaveLength(2); - expect(result.errors).toHaveLength(0); - expect(result.validFiles).toEqual(files); - }); - - it('should filter out invalid files and return errors', () => { - const validFile = new File(['valid content'], 'valid.txt', { type: 'text/plain' }); - const largeFile = new File(['x'.repeat(DEFAULT_MAX_SIZE + 1)], 'large.txt', { type: 'text/plain' }); - - const result = validateFiles([validFile, largeFile], { maxSize: DEFAULT_MAX_SIZE }); - - expect(result.validFiles).toHaveLength(1); - expect(result.errors).toHaveLength(1); - expect(result.validFiles[0]).toBe(validFile); - expect(result.errors[0].file).toBe(largeFile); - }); - - it('should respect maxFiles limit', () => { - const files = Array.from({ length: 5 }, (_, i) => - new File([`content${i}`], `file${i}.txt`, { type: 'text/plain' }) - ); - - const result = validateFiles(files, { maxFiles: 3 }); - - expect(result.validFiles).toHaveLength(3); - expect(result.errors).toHaveLength(2); // 2 files exceeded the limit - expect(result.errors[0].error).toContain('Too many files'); - }); - }); - - describe('formatFileSize', () => { - it('should format bytes correctly', () => { - expect(formatFileSize(0)).toBe('0 Bytes'); - expect(formatFileSize(1024)).toBe('1 KB'); - expect(formatFileSize(1048576)).toBe('1 MB'); - expect(formatFileSize(1073741824)).toBe('1 GB'); - }); - - it('should format decimal values correctly', () => { - expect(formatFileSize(1536)).toBe('1.5 KB'); - expect(formatFileSize(1572864)).toBe('1.5 MB'); - }); - }); - - describe('isImageFile', () => { - it('should return true for image files', () => { - expect(isImageFile(new File([''], 'test.jpg', { type: 'image/jpeg' }))).toBe(true); - expect(isImageFile(new File([''], 'test.png', { type: 'image/png' }))).toBe(true); - expect(isImageFile(new File([''], 'test.gif', { type: 'image/gif' }))).toBe(true); - }); - - it('should return false for non-image files', () => { - expect(isImageFile(new File([''], 'test.txt', { type: 'text/plain' }))).toBe(false); - expect(isImageFile(new File([''], 'test.pdf', { type: 'application/pdf' }))).toBe(false); - }); - }); - - describe('isTextFile', () => { - it('should return true for text files by MIME type', () => { - expect(isTextFile(new File([''], 'test.txt', { type: 'text/plain' }))).toBe(true); - expect(isTextFile(new File([''], 'test.js', { type: 'text/javascript' }))).toBe(true); - expect(isTextFile(new File([''], 'test.json', { type: 'application/json' }))).toBe(true); - }); - - it('should return true for text files by extension', () => { - expect(isTextFile(new File([''], 'test.md', { type: 'application/octet-stream' }))).toBe(true); - expect(isTextFile(new File([''], 'test.tsx', { type: 'application/octet-stream' }))).toBe(true); - }); - - it('should return false for non-text files', () => { - expect(isTextFile(new File([''], 'test.jpg', { type: 'image/jpeg' }))).toBe(false); - expect(isTextFile(new File([''], 'test.pdf', { type: 'application/pdf' }))).toBe(false); - }); - }); -}); \ No newline at end of file diff --git a/src/lib/fileValidation.ts b/src/lib/fileValidation.ts deleted file mode 100644 index 530d0037..00000000 --- a/src/lib/fileValidation.ts +++ /dev/null @@ -1,150 +0,0 @@ -/** - * File validation utilities for the Shakespeare application - */ - -export interface FileValidationError { - file: File; - error: string; -} - -export interface FileValidationOptions { - maxSize?: number; // in bytes - accept?: string; // MIME types or extensions - maxFiles?: number; -} - -/** - * Default file size limit: 10MB - */ -export const DEFAULT_MAX_SIZE = 10 * 1024 * 1024; - -/** - * Default accepted file types - */ -export const DEFAULT_ACCEPT = '*/*'; - -/** - * Validate a single file against the given constraints - */ -export function validateFile(file: File, options: FileValidationOptions = {}): FileValidationError | null { - const { maxSize = DEFAULT_MAX_SIZE, accept = DEFAULT_ACCEPT } = options; - - // Check file size - if (file.size > maxSize) { - return { - file, - error: `File "${file.name}" is too large. Maximum size is ${formatFileSize(maxSize)}.` - }; - } - - // Check file type if accept is specified and not wildcard - if (accept !== '*/*') { - const acceptedTypes = accept.split(',').map(type => type.trim()); - const fileExtension = '.' + file.name.split('.').pop()?.toLowerCase(); - - const isAccepted = acceptedTypes.some(acceptedType => { - if (acceptedType.startsWith('.')) { - // Extension check (e.g., ".jpg", ".png") - return fileExtension === acceptedType.toLowerCase(); - } else if (acceptedType.includes('/')) { - // MIME type check (e.g., "image/*", "application/pdf") - if (acceptedType.endsWith('/*')) { - // Wildcard MIME type (e.g., "image/*") - const mainType = acceptedType.split('/')[0]; - return file.type.startsWith(mainType + '/'); - } else { - // Exact MIME type match - return file.type === acceptedType; - } - } - return false; - }); - - if (!isAccepted) { - return { - file, - error: `File "${file.name}" is not a supported type.` - }; - } - } - - return null; -} - -/** - * Validate multiple files against the given constraints - */ -export function validateFiles(files: File[], options: FileValidationOptions = {}): { - validFiles: File[]; - errors: FileValidationError[]; -} { - const { maxFiles } = options; - const validFiles: File[] = []; - const errors: FileValidationError[] = []; - - // Check maximum number of files - if (maxFiles && files.length > maxFiles) { - const excessFiles = files.slice(maxFiles); - excessFiles.forEach(file => { - errors.push({ - file, - error: `Too many files. Maximum ${maxFiles} files allowed.` - }); - }); - files = files.slice(0, maxFiles); - } - - // Validate each file - files.forEach(file => { - const error = validateFile(file, options); - if (error) { - errors.push(error); - } else { - validFiles.push(file); - } - }); - - return { validFiles, errors }; -} - -/** - * Format file size in human-readable format - */ -export function formatFileSize(bytes: number): string { - if (bytes === 0) return '0 Bytes'; - - const k = 1024; - const sizes = ['Bytes', 'KB', 'MB', 'GB']; - const i = Math.floor(Math.log(bytes) / Math.log(k)); - - return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i]; -} - -/** - * Check if a file is an image - */ -export function isImageFile(file: File): boolean { - return file.type.startsWith('image/'); -} - -/** - * Check if a file is a text file - */ -export function isTextFile(file: File): boolean { - const textMimeTypes = [ - 'text/plain', - 'text/html', - 'text/css', - 'text/javascript', - 'application/json', - 'application/xml', - 'application/javascript', - 'application/typescript', - 'application/x-javascript', - 'text/markdown', - 'text/x-markdown' - ]; - - return textMimeTypes.includes(file.type) || - file.name.match(/\.(txt|md|markdown|html|css|js|ts|jsx|tsx|json|xml)$/i) !== null; -} \ No newline at end of file diff --git a/src/pages/Index.tsx b/src/pages/Index.tsx index a11abff9..fd84394f 100644 --- a/src/pages/Index.tsx +++ b/src/pages/Index.tsx @@ -12,7 +12,6 @@ import { AppLayout } from '@/components/AppLayout'; import { DotAI } from '@/lib/DotAI'; import type { AIMessage } from '@/lib/SessionManager'; import { saveFileToTmp } from '@/lib/fileUtils'; -import { validateFiles, DEFAULT_MAX_SIZE, DEFAULT_ACCEPT } from '@/lib/fileValidation'; import { Button } from '@/components/ui/button'; import { Textarea } from '@/components/ui/textarea'; @@ -134,39 +133,21 @@ export default function Index() { const files = Array.from(e.dataTransfer.files); if (files.length === 0) return; - // Validate files using the same logic as FileAttachment - const { validFiles, errors } = validateFiles(files, { - maxSize: DEFAULT_MAX_SIZE, - accept: DEFAULT_ACCEPT, - maxFiles: 10 // Limit to 10 files at once - }); + // Add all files without validation + setAttachedFiles(prev => [...prev, ...files]); - // Add valid files to attachments - if (validFiles.length > 0) { - setAttachedFiles(prev => [...prev, ...validFiles]); - - // Announce files added via toast - if (validFiles.length === 1) { - toast({ - title: "File added", - description: `${validFiles[0].name} has been added to attachments.`, - }); - } else { - toast({ - title: "Files added", - description: `${validFiles.length} files have been added to attachments.`, - }); - } - } - - // Show errors for invalid files - errors.forEach(error => { + // Announce files added via toast + if (files.length === 1) { toast({ - title: "File upload error", - description: error.error, - variant: "destructive", + title: "File added", + description: `${files[0].name} has been added to attachments.`, }); - }); + } else { + toast({ + title: "Files added", + description: `${files.length} files have been added to attachments.`, + }); + } }; // Handle keyboard shortcuts (physical keyboards only)