MCPcopy Create free account
hub / github.com/UsefulSoftwareCo/executor / sniffMimeType

Function sniffMimeType

packages/plugins/openapi/src/sdk/invoke.ts:405–435  ·  view source on GitHub ↗
(bytes: Uint8Array)

Source from the content-addressed store, hash-verified

403};
404
405const sniffMimeType = (bytes: Uint8Array): string | null => {
406 if (startsWithBytes(bytes, [0xff, 0xd8, 0xff])) return "image/jpeg";
407 if (startsWithBytes(bytes, [0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a])) {
408 return "image/png";
409 }
410 if (
411 startsWithBytes(bytes, [0x47, 0x49, 0x46, 0x38, 0x37, 0x61]) ||
412 startsWithBytes(bytes, [0x47, 0x49, 0x46, 0x38, 0x39, 0x61])
413 ) {
414 return "image/gif";
415 }
416 if (
417 startsWithBytes(bytes, [0x52, 0x49, 0x46, 0x46]) &&
418 bytes[8] === 0x57 &&
419 bytes[9] === 0x45 &&
420 bytes[10] === 0x42 &&
421 bytes[11] === 0x50
422 ) {
423 return "image/webp";
424 }
425 if (startsWithBytes(bytes, [0x25, 0x50, 0x44, 0x46, 0x2d])) return "application/pdf";
426 if (
427 startsWithBytes(bytes, [0x50, 0x4b, 0x03, 0x04]) ||
428 startsWithBytes(bytes, [0x50, 0x4b, 0x05, 0x06]) ||
429 startsWithBytes(bytes, [0x50, 0x4b, 0x07, 0x08])
430 ) {
431 return "application/zip";
432 }
433 if (isLikelyUtf8Text(bytes)) return "text/plain";
434 return null;
435};
436
437const bytesFromBase64Prefix = (base64: string): Uint8Array => {
438 const prefix = base64.slice(0, Math.min(base64.length, 64));

Callers 2

sniffMimeTypeFromBase64Function · 0.85
fileFromBinaryBytesFunction · 0.85

Calls 2

startsWithBytesFunction · 0.85
isLikelyUtf8TextFunction · 0.85

Tested by

no test coverage detected