import type { FilePreview } from '../types'; export async function generateFilePreview(file: File): Promise { const type = detectFileType(file); const preview: FilePreview = { type, }; try { switch (type) { case 'pdf': preview.pages = await getPdfPageCount(file); preview.thumbnail = await generatePdfThumbnail(file); break; case 'excel': preview.sheets = await getExcelSheetNames(file); break; case 'csv': const csvMetadata = await getCsvMetadata(file); preview.metadata = csvMetadata; preview.csvData = csvMetadata.previewData || []; break; case 'word': preview.pages = await getWordPageCount(file); break; case 'image': preview.thumbnail = await generateImageThumbnail(file); break; case 'text': preview.metadata = await getTextMetadata(file); break; } } catch (error) { console.warn('Error generating preview:', error); } return preview; } function detectFileType(file: File): FilePreview['type'] { if (file.type === 'application/pdf') return 'pdf'; if ( file.type === 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' || file.type === 'application/vnd.ms-excel' ) { return 'excel'; } if (file.type === 'text/csv' || file.name.toLowerCase().endsWith('.csv')) { return 'csv'; } if ( file.type === 'application/vnd.openxmlformats-officedocument.wordprocessingml.document' || file.type === 'application/msword' ) { return 'word'; } if (file.type.startsWith('image/')) return 'image'; if (file.type.startsWith('text/')) return 'text'; return 'other'; } async function getPdfPageCount(file: File): Promise { // This would require PDF.js library // For now, return a placeholder try { // In a real implementation, you'd use PDF.js: // const pdf = await pdfjsLib.getDocument(await file.arrayBuffer()).promise; // return pdf.numPages; return 1; // Placeholder } catch { return 0; } } async function generatePdfThumbnail(file: File): Promise { // This would require PDF.js library // For now, return a placeholder try { // In a real implementation, you'd use PDF.js to render first page return ''; // Placeholder } catch { return ''; } } async function getExcelSheetNames(file: File): Promise { // This would require xlsx library // For now, return a placeholder try { // In a real implementation, you'd use xlsx: // const workbook = XLSX.read(await file.arrayBuffer(), { type: 'array' }); // return workbook.SheetNames; return ['Sheet1']; // Placeholder } catch { return []; } } async function getCsvMetadata(file: File): Promise> { try { const text = await file.text(); const lines = text.split('\n').filter((line: string) => line.trim()); // Parse CSV lines (handle quoted values and commas) const parseCsvLine = (line: string): string[] => { const result: string[] = []; let current = ''; let inQuotes = false; for (let i = 0; i < line.length; i++) { const char = line[i]; if (char === '"') { if (inQuotes && line[i + 1] === '"') { // Escaped quote current += '"'; i++; } else { // Toggle quote state inQuotes = !inQuotes; } } else if (char === ',' && !inQuotes) { // Field separator result.push(current.trim()); current = ''; } else { current += char; } } result.push(current.trim()); return result; }; const parsedLines = lines.map(parseCsvLine); const firstLine = parsedLines[0] || []; // Get first 100 rows for preview (to avoid performance issues) const previewRows = parsedLines.slice(0, 100); return { rows: lines.length, columns: firstLine.length, headers: firstLine, previewData: previewRows, // First 100 rows for table preview }; } catch { return {}; } } async function getWordPageCount(file: File): Promise { // Word file parsing would require mammoth.js or similar // For now, return a placeholder return 1; // Placeholder } async function generateImageThumbnail(file: File): Promise { return new Promise((resolve) => { const reader = new FileReader(); reader.onload = (e) => { resolve(e.target?.result as string); }; reader.readAsDataURL(file); }); } async function getTextMetadata(file: File): Promise> { try { const text = await file.text(); return { lines: text.split('\n').length, characters: text.length, words: text.split(/\s+/).filter((w: string) => w.length > 0).length, }; } catch { return {}; } }