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

Function sniffMimeType

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

Source from the content-addressed store, hash-verified

437};
438
439const sniffMimeType = (bytes: Uint8Array): string | null => {
440 if (startsWithBytes(bytes, [0xff, 0xd8, 0xff])) return "image/jpeg";
441 if (startsWithBytes(bytes, [0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a])) {
442 return "image/png";
443 }
444 if (
445 startsWithBytes(bytes, [0x47, 0x49, 0x46, 0x38, 0x37, 0x61]) ||
446 startsWithBytes(bytes, [0x47, 0x49, 0x46, 0x38, 0x39, 0x61])
447 ) {
448 return "image/gif";
449 }
450 if (
451 startsWithBytes(bytes, [0x52, 0x49, 0x46, 0x46]) &&
452 bytes[8] === 0x57 &&
453 bytes[9] === 0x45 &&
454 bytes[10] === 0x42 &&
455 bytes[11] === 0x50
456 ) {
457 return "image/webp";
458 }
459 if (startsWithBytes(bytes, [0x25, 0x50, 0x44, 0x46, 0x2d])) return "application/pdf";
460 if (
461 startsWithBytes(bytes, [0x50, 0x4b, 0x03, 0x04]) ||
462 startsWithBytes(bytes, [0x50, 0x4b, 0x05, 0x06]) ||
463 startsWithBytes(bytes, [0x50, 0x4b, 0x07, 0x08])
464 ) {
465 return "application/zip";
466 }
467 if (isLikelyUtf8Text(bytes)) return "text/plain";
468 return null;
469};
470
471const bytesFromBase64Prefix = (base64: string): Uint8Array => {
472 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