@twilio-labs/serverless-api
Version:
API-wrapper for the Twilio Serverless API
38 lines (37 loc) • 1.28 kB
JavaScript
/** @module @twilio-labs/serverless-api/dist/utils */
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getContentType = void 0;
const mime_types_1 = __importDefault(require("mime-types"));
const path_1 = __importDefault(require("path"));
function hasExtension(name) {
if (typeof name === 'undefined') {
return false;
}
return path_1.default.extname(name).length !== 0;
}
/**
* Tries to determine the content type of a string or buffer
*
* @export
* @param {(string | Buffer)} content the content to check
* @returns {Promise<(string | undefined)>} a valid content type or undefined
*/
async function getContentType(content, name) {
let contentType;
if (content instanceof Buffer && !hasExtension(name)) {
const { fileTypeFromBuffer } = await import('file-type');
const type = await fileTypeFromBuffer(content);
if (type) {
contentType = type.mime;
}
}
if (name && !contentType) {
contentType = mime_types_1.default.lookup(name) || undefined;
}
return contentType;
}
exports.getContentType = getContentType;
;