UNPKG

@maximai/maxim-js

Version:

Maxim AI JS SDK. Visit https://getmaxim.ai for more info.

157 lines 6.25 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.populateAttachmentFields = populateAttachmentFields; const fs = __importStar(require("fs")); const mimeTypes = __importStar(require("mime-types")); const path = __importStar(require("path")); const utils_1 = require("../utils"); function populateAttachmentFields(attachment) { const result = { ...attachment }; if (!result.id) { result.id = (0, utils_1.uniqueId)(); } switch (result.type) { case "file": { const filePath = result.path; if (!result.name) { result.name = path.basename(filePath); } if (!result.mimeType) { result.mimeType = mimeTypes.lookup(filePath) || "application/octet-stream"; } if (!result.size) { try { const stats = fs.statSync(filePath); result.size = stats.size; } catch (e) { } } break; } case "fileData": { if (!result.size && result.data) { result.size = result.data.length; } if (!result.mimeType && result.data && result.data.length > 4) { const header = result.data.subarray(0, 4).toString("hex"); if (header.startsWith("89504e47")) { result.mimeType = "image/png"; } else if (header.startsWith("ffd8ff")) { result.mimeType = "image/jpeg"; } else if (header.startsWith("47494638")) { result.mimeType = "image/gif"; } else if (header.startsWith("25504446")) { result.mimeType = "application/pdf"; } else if (result.data.slice(0, 5).toString() === "%PDF-") { result.mimeType = "application/pdf"; } else if (header.startsWith("504b0304")) { result.mimeType = "application/zip"; } else { let isText = true; const sampleSize = Math.min(result.data.length, 512); for (let i = 0; i < sampleSize; i++) { const byte = result.data[i]; if ((byte < 32 || byte > 126) && ![9, 10, 13].includes(byte)) { isText = false; break; } } if (isText) { try { JSON.parse(result.data.toString("utf8").trim()); result.mimeType = "application/json"; } catch { if (result.data.toString("utf8").match(/<html|<!doctype html/i)) { result.mimeType = "text/html"; } else { result.mimeType = "text/plain"; } } } else { result.mimeType = "application/octet-stream"; } } } break; } case "url": { try { const urlObj = new URL(result.url); if (!result.name) { const urlPath = urlObj.pathname; result.name = path.basename(urlPath) || urlObj.hostname; } if (!result.mimeType) { const urlPath = urlObj.pathname; const extension = path.extname(urlPath); if (extension) { const detectedMimeType = mimeTypes.lookup(extension); if (detectedMimeType) { result.mimeType = detectedMimeType; } } if (!result.mimeType && (urlObj.search || urlObj.hash)) { const cleanPath = urlPath.split(/[?#]/)[0]; const extension = path.extname(cleanPath); if (extension) { const detectedMimeType = mimeTypes.lookup(extension); if (detectedMimeType) { result.mimeType = detectedMimeType; } } } } } catch (e) { } break; } default: const exhaustiveCheck = result; } return result; } //# sourceMappingURL=attachment.js.map