mindee
Version:
Mindee Client Library for Node.js
106 lines (105 loc) • 4.02 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ExtractedImage = void 0;
const node_buffer_1 = require("node:buffer");
const errors_1 = require("../../errors");
const node_fs_1 = require("node:fs");
const node_path_1 = __importDefault(require("node:path"));
const logger_1 = require("../../logger");
const input_1 = require("../../input");
const localInputSource_1 = require("../../input/sources/localInputSource");
const node_poppler_1 = require("node-poppler");
const promises_1 = require("fs/promises");
/**
* Generic class for image extraction
*/
class ExtractedImage {
constructor(buffer, fileName) {
this.buffer = node_buffer_1.Buffer.from(buffer);
this.internalFileName = fileName;
}
/**
* Saves the document to a file.
*
* @param outputPath Path to save the file to.
*/
async saveToFileAsync(outputPath) {
const fileExt = node_path_1.default.extname(outputPath).toLowerCase();
if (!localInputSource_1.MIMETYPES.has(fileExt)) {
throw new errors_1.MindeeError(`Unsupported file extension: ${fileExt}`);
}
try {
let outputBuffer = this.buffer;
if (fileExt !== ".pdf") {
const poppler = new node_poppler_1.Poppler();
const options = {
firstPageToConvert: 1,
lastPageToConvert: 1,
singleFile: true,
};
if (fileExt === ".png") {
options.pngFile = true;
}
else if (fileExt === ".jpg" || fileExt === ".jpeg") {
options.jpegFile = true;
}
else if (fileExt === ".tiff" || fileExt === ".tif") {
options.tiffFile = true;
}
const result = await poppler.pdfToCairo(this.buffer, undefined, options);
outputBuffer = node_buffer_1.Buffer.from(result, "latin1");
}
await (0, promises_1.writeFile)(node_path_1.default.resolve(outputPath), outputBuffer);
logger_1.logger.info(`File saved successfully to ${node_path_1.default.resolve(outputPath)}.`);
}
catch (e) {
if (e instanceof TypeError) {
throw new errors_1.MindeeError("Invalid path/filename provided.");
}
else {
throw e;
}
}
}
/**
* Attempts to saves the document to a file synchronously.
* Throws an error if the file extension is not supported or if the file could not be saved to disk for some reason.
*
* @param outputPath Path to save the file to.
*/
saveToFile(outputPath) {
const fileExt = node_path_1.default.extname(outputPath).toLowerCase();
if (fileExt !== ".pdf") {
throw new errors_1.MindeeError(`Unsupported file extension: ${fileExt}. For image formats, use saveToFileAsync() instead.`);
}
else {
try {
(0, node_fs_1.writeFileSync)(node_path_1.default.resolve(outputPath), this.buffer);
logger_1.logger.info(`File saved successfully to ${node_path_1.default.resolve(outputPath)}.`);
}
catch (e) {
if (e instanceof TypeError) {
throw new errors_1.MindeeError("Invalid path/filename provided.");
}
else {
throw e;
}
}
}
}
/**
* Return the file as a Mindee-compatible BufferInput source.
*
* @returns A BufferInput source.
*/
asSource() {
return new input_1.BufferInput({
buffer: this.buffer,
filename: this.internalFileName,
});
}
}
exports.ExtractedImage = ExtractedImage;