agent-ecosystem/src/renderer/utils/previewRegistry.ts
iliya e1585b4f71 feat: add binary file preview functionality and improve editor performance
- Introduced a new IPC handler for reading binary files as base64 for inline previews, enhancing the editor's capability to display images directly.
- Implemented a placeholder component for non-previewable binary files, providing users with file information and an option to open in the system viewer.
- Enhanced the EditorFileWatcher to ignore permission errors and adjusted debounce timing for better performance during file system events.
- Updated the ProjectFileService to include a method for reading binary previews with security checks and MIME type validation.
- Improved the EditorImagePreview component to handle loading states and display images with a checkerboard background for transparency.
- Refactored the EditorBinaryState component to utilize the new binary preview functionality, streamlining the display of binary files.
2026-03-01 23:51:51 +02:00

45 lines
1.2 KiB
TypeScript

/**
* Preview type registry for binary files.
*
* Extensible: add a new PreviewType + extension set + component to support new formats.
*/
export type PreviewType = 'image' | 'unknown';
const IMAGE_EXTENSIONS = new Set(['png', 'jpg', 'jpeg', 'gif', 'webp', 'svg', 'bmp', 'ico']);
const IMAGE_MAX_SIZE = 10 * 1024 * 1024; // 10 MB
const MIME_MAP: Record<string, string> = {
png: 'image/png',
jpg: 'image/jpeg',
jpeg: 'image/jpeg',
gif: 'image/gif',
webp: 'image/webp',
svg: 'image/svg+xml',
bmp: 'image/bmp',
ico: 'image/x-icon',
};
function getExtension(fileName: string): string {
const dot = fileName.lastIndexOf('.');
if (dot === -1) return '';
return fileName.slice(dot + 1).toLowerCase();
}
export function getPreviewType(fileName: string): PreviewType {
const ext = getExtension(fileName);
if (IMAGE_EXTENSIONS.has(ext)) return 'image';
return 'unknown';
}
export function isPreviewable(fileName: string, size: number): boolean {
const type = getPreviewType(fileName);
if (type === 'image') return size <= IMAGE_MAX_SIZE;
return false;
}
export function getMimeType(fileName: string): string | null {
const ext = getExtension(fileName);
return MIME_MAP[ext] ?? null;
}