Skip to content

Open-source LLM knowledge platform: turn raw documents into a queryable RAG, an autonomous reasoning agent, and a self-maintaining Wiki.

fix(preview): distinguish image and audio ISO media brands (#4515)

fengyue-xve8ed3676f11da
diff --git a/frontend/src/utils/filePreview.test.ts b/frontend/src/utils/filePreview.test.ts
index 723b192dd..2fcfeb484 100644
--- a/frontend/src/utils/filePreview.test.ts
+++ b/frontend/src/utils/filePreview.test.ts
@@ -77,3 +77,38 @@ test('mime and highlight maps cover skill-generated html/csv', () => {
assert.equal(getHighlightLang('html'), 'xml')
assert.equal(getHighlightLang('ts'), 'typescript')
})
+
+function ftyp(major: string, compatible: string[] = []) {
+ const brands = major + String.fromCharCode(0, 0, 0, 0) + compatible.join('')
+ const bytes = new Uint8Array(8 + brands.length)
+ new DataView(bytes.buffer).setUint32(0, bytes.length)
+ bytes.set(new TextEncoder().encode('ftyp'), 4)
+ bytes.set(new TextEncoder().encode(brands), 8)
+ return bytes
+}
+
+test('sniffPreview distinguishes images and audio in ISO base media files', () => {
+ for (const brand of ['avif', 'avis']) {
+ assert.deepEqual(sniffPreview(ftyp(brand)), { kind: 'image', ext: 'avif' })
+ }
+ for (const brand of ['heic', 'heix', 'hevc', 'hevx']) {
+ assert.deepEqual(sniffPreview(ftyp(brand)), { kind: 'image', ext: 'heic' })
+ }
+ for (const brand of ['M4A ', 'M4B ']) {
+ assert.deepEqual(sniffPreview(ftyp(brand)), { kind: 'audio', ext: 'm4a' })
+ }
+})
+
+test('sniffPreview checks compatible image brands when the major brand is generic', () => {
+ assert.deepEqual(sniffPreview(ftyp('mif1', ['miaf', 'avif'])), { kind: 'image', ext: 'avif' })
+ assert.deepEqual(sniffPreview(ftyp('mif1', ['heic'])), { kind: 'image', ext: 'heic' })
+})
+
+test('sniffPreview retains video fallback and confines brands to the ftyp box', () => {
+ assert.deepEqual(sniffPreview(ftyp('isom', ['mp42'])), { kind: 'video', ext: 'mp4' })
+ const video = ftyp('isom')
+ const withTrailingBytes = new Uint8Array(video.length + 4)
+ withTrailingBytes.set(video)
+ withTrailingBytes.set(new TextEncoder().encode('avif'), video.length)
+ assert.deepEqual(sniffPreview(withTrailingBytes), { kind: 'video', ext: 'mp4' })
+})
diff --git a/frontend/src/utils/filePreview.ts b/frontend/src/utils/filePreview.ts
index 065c833ff..4991e0e67 100644
--- a/frontend/src/utils/filePreview.ts
+++ b/frontend/src/utils/filePreview.ts
@@ -349,6 +349,21 @@ function sniffFromMagic(bytes: Uint8Array): FilePreviewSniff | null {
if (tag === 'AVI ') return { kind: 'video', ext: 'avi' }
}
if (bytes.length >= 8 && asciiTag(bytes, 4, 4) === 'ftyp') {
+ // ISO base media containers also carry images and audio. Compatible
+ // brands matter when the major brand is generic (for example, mif1).
+ const brands = new Set([asciiTag(bytes, 8, 4)])
+ if (bytes.length >= 16) {
+ const boxSize = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength).getUint32(0)
+ const end = Math.min(boxSize, bytes.length, SNIFF_BYTES)
+ for (let offset = 16; offset + 4 <= end; offset += 4) {
+ brands.add(asciiTag(bytes, offset, 4))
+ }
+ }
+ if (brands.has('avif') || brands.has('avis')) return { kind: 'image', ext: 'avif' }
+ if (['heic', 'heix', 'hevc', 'hevx'].some(brand => brands.has(brand))) {
+ return { kind: 'image', ext: 'heic' }
+ }
+ if (brands.has('M4A ') || brands.has('M4B ')) return { kind: 'audio', ext: 'm4a' }
return { kind: 'video', ext: 'mp4' }
}
if (bytes.length >= 4 && bytes[0] === 0x1A && bytes[1] === 0x45 && bytes[2] === 0xDF && bytes[3] === 0xA3) {