MCPcopy Create free account
hub / github.com/bearlyai/OpenADE / mediaTypeFromMagic

Function mediaTypeFromMagic

projects/runtime-node/src/fileMetadata.ts:78–92  ·  view source on GitHub ↗
(sample: Uint8Array)

Source from the content-addressed store, hash-verified

76}
77
78function mediaTypeFromMagic(sample: Uint8Array): string | null {
79 if (hasPrefix(sample, [0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a])) return "image/png"
80 if (hasPrefix(sample, [0xff, 0xd8, 0xff])) return "image/jpeg"
81 if (asciiAt(sample, 0, 6) === "GIF87a" || asciiAt(sample, 0, 6) === "GIF89a") return "image/gif"
82 if (asciiAt(sample, 0, 4) === "RIFF" && asciiAt(sample, 8, 4) === "WEBP") return "image/webp"
83 if (asciiAt(sample, 0, 2) === "BM") return "image/bmp"
84 if (hasPrefix(sample, [0x00, 0x00, 0x01, 0x00])) return "image/x-icon"
85
86 if (asciiAt(sample, 4, 4) === "ftyp") {
87 const brand = asciiAt(sample, 8, 12)
88 if (brand.includes("avif") || brand.includes("avis")) return "image/avif"
89 }
90
91 return null
92}
93
94function mediaTypeFromExtension(filePath: string): string | null {
95 return IMAGE_EXTENSION_MEDIA_TYPES.get(path.extname(filePath).toLowerCase()) ?? null

Callers 1

classifyFileMetadataFunction · 0.85

Calls 2

hasPrefixFunction · 0.85
asciiAtFunction · 0.85

Tested by

no test coverage detected