/**
 * Image MIME type detection by reading file magic bytes.
 *
 * Only recognizes image formats supported by the LLM vision APIs
 * (JPEG, PNG, GIF, WebP). Returns null for unsupported or non-image files.
 */
import { fileTypeFromBuffer } from 'file-type';
import { open } from 'node:fs/promises';
/** MIME types recognized as supported image formats. */
const IMAGE_MIME_TYPES = new Set(['image/jpeg', 'image/png', 'image/gif', 'image/webp']);
/** Number of bytes to read for magic-byte detection (covers all common image headers). */
const FILE_TYPE_SNIFF_BYTES = 4100;
/** Detect if a file is a supported image by sniffing its first bytes. Returns the MIME type or null. */
export async function detectSupportedImageMimeTypeFromFile(filePath) {
    const fileHandle = await open(filePath, 'r');
    try {
        const buffer = Buffer.alloc(FILE_TYPE_SNIFF_BYTES);
        const { bytesRead } = await fileHandle.read(buffer, 0, FILE_TYPE_SNIFF_BYTES, 0);
        if (bytesRead === 0) {
            return null;
        }
        const fileType = await fileTypeFromBuffer(buffer.subarray(0, bytesRead));
        if (!fileType) {
            return null;
        }
        if (!IMAGE_MIME_TYPES.has(fileType.mime)) {
            return null;
        }
        return fileType.mime;
    }
    finally {
        await fileHandle.close();
    }
}
//# sourceMappingURL=mime.js.map