UNPKG

mindee

Version:

Mindee Client Library for Node.js

181 lines (180 loc) 6.87 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; }; })(); var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.LocalInputSource = void 0; const handler_1 = require("../../errors/handler"); const logger_1 = require("../../logger"); const imageOperations_1 = require("../../imageOperations"); const pdf_1 = require("../../pdf"); const path_1 = __importDefault(require("path")); const fileType = __importStar(require("file-type")); const pdf_2 = require("../../pdf"); const inputSource_1 = require("./inputSource"); const MIMETYPES = new Map([ [".pdf", "application/pdf"], [".heic", "image/heic"], [".jpg", "image/jpeg"], [".jpeg", "image/jpeg"], [".png", "image/png"], [".tif", "image/tiff"], [".tiff", "image/tiff"], [".webp", "image/webp"], ]); const ALLOWED_INPUT_TYPES = [ inputSource_1.INPUT_TYPE_STREAM, inputSource_1.INPUT_TYPE_BASE64, inputSource_1.INPUT_TYPE_BYTES, inputSource_1.INPUT_TYPE_PATH, inputSource_1.INPUT_TYPE_BUFFER, ]; class LocalInputSource extends inputSource_1.InputSource { /** * @param {InputConstructor} constructor Constructor parameters. */ constructor({ inputType }) { super(); this.filename = ""; this.mimeType = ""; // Check if inputType is valid if (!ALLOWED_INPUT_TYPES.includes(inputType)) { const allowed = Array.from(ALLOWED_INPUT_TYPES.keys()).join(", "); handler_1.errorHandler.throw(new Error(`Invalid input type, must be one of ${allowed}.`)); } this.inputType = inputType; logger_1.logger.debug(`Loading file from: ${inputType}`); } async checkMimetype() { if (!(this.fileObject instanceof Buffer)) { throw new Error(`MIME type cannot be verified on input source of type ${this.inputType}.`); } let mimeType; const fileExt = path_1.default.extname(this.filename); if (fileExt) { mimeType = MIMETYPES.get(fileExt.toLowerCase()) || ""; } else { const guess = await fileType.fromBuffer(this.fileObject); if (guess !== undefined) { mimeType = guess.mime; } else { throw "Could not determine the MIME type of the file"; } } if (!mimeType) { const allowed = Array.from(MIMETYPES.keys()).join(", "); const err = new Error(`Invalid file type, must be one of ${allowed}.`); handler_1.errorHandler.throw(err); } logger_1.logger.debug(`File is of type: ${mimeType}`); return mimeType; } isPdf() { if (!this.initialized) { throw new Error("The `init()` method must be called before calling `isPdf()`."); } return this.mimeType === "application/pdf"; } /** * Cut PDF pages. * @param pageOptions */ async applyPageOptions(pageOptions) { if (!this.initialized) { await this.init(); } if (!(this.fileObject instanceof Buffer)) { throw new Error(`Cannot modify an input source of type ${this.inputType}.`); } const processedPdf = await (0, pdf_2.extractPages)(this.fileObject, pageOptions); this.fileObject = processedPdf.file; } /** * Cut PDF pages. * @param pageOptions * @deprecated Deprecated in favor of {@link LocalInputSource.applyPageOptions}. */ async cutPdf(pageOptions) { return this.applyPageOptions(pageOptions); } /** * Compresses the file object, either as a PDF or an image. * * @param quality Quality of the compression. For images, this is the JPEG quality. * For PDFs, this affects image quality within the PDF. * @param maxWidth Maximum width for image resizing. Ignored for PDFs. * @param maxHeight Maximum height for image resizing. Ignored for PDFs. * @param forceSourceText For PDFs, whether to force compression even if source text is present. * @param disableSourceText For PDFs, whether to disable source text during compression. * * @returns A Promise that resolves when the compression is complete. */ async compress(quality = 85, maxWidth = null, maxHeight = null, forceSourceText = false, disableSourceText = true) { if (!this.initialized) { await this.init(); } let buffer; if (typeof this.fileObject === "string") { buffer = Buffer.from(this.fileObject); } else { buffer = this.fileObject; } if (this.isPdf()) { this.fileObject = await (0, pdf_1.compressPdf)(buffer, quality, forceSourceText, disableSourceText); } else { this.fileObject = await (0, imageOperations_1.compressImage)(buffer, quality, maxWidth, maxHeight); } } /** * Returns true if the object is a PDF and has source text. False otherwise. * @return boolean */ async hasSourceText() { if (!this.initialized) { await this.init(); } if (!this.isPdf()) { return false; } const buffer = typeof this.fileObject === "string" ? Buffer.from(this.fileObject) : this.fileObject; return (0, pdf_2.hasSourceText)(buffer); } } exports.LocalInputSource = LocalInputSource;