docxml
Version:
TypeScript (component) library for building and parsing a DOCX file
44 lines (43 loc) • 1.45 kB
JavaScript
;
/**
* @file
* Guesstimating the MIME type of binary file attachments, mostly images.
*
* Adapted from code kindly provided by @pbek:
* https://github.com/Stuk/jszip/issues/626#issuecomment-639272737
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.getMimeTypeForUint8Array = void 0;
const enums_js_1 = require("../enums.js");
function getMimeTypeFromHexSignature(signature) {
switch (signature) {
case 'FFD8FFDB':
case 'FFD8FFE0':
case 'FFD8FFE1':
return enums_js_1.FileMime.jpeg;
case '89504E47':
return enums_js_1.FileMime.png;
case '47494638':
return enums_js_1.FileMime.gif;
// case '25504446':
// return 'application/pdf';
// case '504B0304':
// return 'application/zip';
default:
throw new Error(`Unsupported file type, signature "${signature}" not recognized.`);
}
}
/**
* Guess the mime type associated with a byte stream by looking at the first few signature bytes.
*
* Throws when the mime type is not recognized, or when it is not JPEG/GIF/PNG.
*/
function getMimeTypeForUint8Array(uint) {
const bytes = [];
uint.slice(0, 4).forEach((byte) => {
bytes.push(byte.toString(16));
});
const hex = bytes.join('').toUpperCase();
return getMimeTypeFromHexSignature(hex);
}
exports.getMimeTypeForUint8Array = getMimeTypeForUint8Array;