MCPcopy
hub / github.com/simstudioai/sim / sniffImageMimeType

Function sniffImageMimeType

apps/sim/providers/attachments.ts:212–248  ·  view source on GitHub ↗
(base64: string)

Source from the content-addressed store, hash-verified

210}
211
212function sniffImageMimeType(base64: string): string {
213 let bytes: Buffer
214 try {
215 bytes = Buffer.from(base64, 'base64')
216 } catch {
217 return ''
218 }
219
220 if (
221 bytes.length >= 8 &&
222 bytes.subarray(0, 8).equals(Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]))
223 ) {
224 return 'image/png'
225 }
226
227 if (bytes.length >= 3 && bytes[0] === 0xff && bytes[1] === 0xd8 && bytes[2] === 0xff) {
228 return 'image/jpeg'
229 }
230
231 if (
232 bytes.length >= 6 &&
233 (bytes.subarray(0, 6).equals(Buffer.from('GIF87a')) ||
234 bytes.subarray(0, 6).equals(Buffer.from('GIF89a')))
235 ) {
236 return 'image/gif'
237 }
238
239 if (
240 bytes.length >= 12 &&
241 bytes.subarray(0, 4).equals(Buffer.from('RIFF')) &&
242 bytes.subarray(8, 12).equals(Buffer.from('WEBP'))
243 ) {
244 return 'image/webp'
245 }
246
247 return ''
248}
249
250function getAttachmentExtension(file: UserFile, mimeType: string): string {
251 if (mimeType === 'text/markdown') return 'md'

Callers 1

Calls

no outgoing calls

Tested by

no test coverage detected