mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-19 17:01:37 +03:00
fix(ai): detect AVIF and other ISO-BMFF brands instead of labeling them video/mp4 (#58480)
This commit is contained in:
committed by
GitHub
Unverified
parent
240817f664
commit
e3e83be566
@@ -17,7 +17,26 @@ export const detectMediaType = (bytes: Uint8Array): string | undefined => {
|
||||
if (riffType === "WAVE") return "audio/wav"
|
||||
}
|
||||
if (startsWith(bytes, [0x25, 0x50, 0x44, 0x46])) return "application/pdf"
|
||||
if (bytes.length >= 12 && ascii(bytes, 4, 8) === "ftyp") return "video/mp4"
|
||||
if (bytes.length >= 12 && ascii(bytes, 4, 8) === "ftyp") {
|
||||
// AVIF, HEIC, QuickTime and M4A share MP4's container; the major brand tells them apart, and generic HEIF brands
|
||||
// list `avif`/`avis` among the compatible brands that follow the minor version.
|
||||
const brand = ascii(bytes, 8, 12)
|
||||
const boxEnd = Math.min(new DataView(bytes.buffer, bytes.byteOffset).getUint32(0), bytes.length)
|
||||
const compatible = Array.from({ length: Math.floor((boxEnd - 16) / 4) }, (_, index) =>
|
||||
ascii(bytes, 16 + index * 4, 20 + index * 4),
|
||||
)
|
||||
const generic = brand === "mif1" || brand === "msf1"
|
||||
if (
|
||||
brand === "avif" ||
|
||||
brand === "avis" ||
|
||||
(generic && (compatible.includes("avif") || compatible.includes("avis")))
|
||||
)
|
||||
return "image/avif"
|
||||
if (generic || ["heic", "heix", "hevc", "hevx"].includes(brand)) return "image/heic"
|
||||
if (brand === "qt ") return "video/quicktime"
|
||||
if (brand === "M4A ") return "audio/mp4"
|
||||
return "video/mp4"
|
||||
}
|
||||
if (startsWith(bytes, [0x1a, 0x45, 0xdf, 0xa3])) return "video/webm"
|
||||
if (startsWith(bytes, [0x49, 0x44, 0x33])) return "audio/mpeg"
|
||||
// An 11-bit frame sync; layer bits `00` mark AAC ADTS, any other layer is MPEG audio.
|
||||
|
||||
@@ -76,6 +76,31 @@ describe("Media", () => {
|
||||
}),
|
||||
)
|
||||
|
||||
it.effect("tells ISO-BMFF brands apart", () =>
|
||||
Effect.sync(() => {
|
||||
const ftyp = (hex: string) => Uint8Array.from(Buffer.from(hex, "hex"))
|
||||
expect(Media.detectMediaType(ftyp("000000206674797061766966000000004d695072617669666d6961666d696631"))).toBe(
|
||||
"image/avif",
|
||||
)
|
||||
expect(Media.detectMediaType(ftyp("000000186674797061766973000000006d73663161766973"))).toBe("image/avif")
|
||||
expect(Media.detectMediaType(ftyp("000000246674797068656963000000006d6966314d6950726d6961664d694842"))).toBe(
|
||||
"image/heic",
|
||||
)
|
||||
expect(Media.detectMediaType(ftyp("00000018667479706d69663100000000617669666d69663100000000"))).toBe("image/avif")
|
||||
expect(Media.detectMediaType(ftyp("00000018667479706d69663100000000686569636d69663100000000"))).toBe("image/heic")
|
||||
expect(Media.detectMediaType(ftyp("000000206674797069736f6d0000020069736f6d69736f32617663316d703431"))).toBe(
|
||||
"video/mp4",
|
||||
)
|
||||
expect(Media.detectMediaType(ftyp("00000014667479707174202000000200717420200000000877696465"))).toBe(
|
||||
"video/quicktime",
|
||||
)
|
||||
expect(Media.detectMediaType(ftyp("0000001c667479704d344120000002004d34412069736f6d69736f3200000008"))).toBe(
|
||||
"audio/mp4",
|
||||
)
|
||||
expect(Media.bytes(ftyp("000000206674797061766966000000004d695072617669666d6961666d696631")).kind).toBe("image")
|
||||
}),
|
||||
)
|
||||
|
||||
it.effect("materializes url assets through the request executor once", () =>
|
||||
Effect.gen(function* () {
|
||||
const requests = yield* Ref.make<Array<HttpClientRequest.HttpClientRequest>>([])
|
||||
|
||||
Reference in New Issue
Block a user