UNPKG

@adobe/pdftools-extract-node-sdk

Version:

The Document Services PDF Tools Extract Node.js SDK provides APIs for extracting elements and renditions from PDF

100 lines (91 loc) 3.74 kB
/* * Copyright 2019 Adobe * All Rights Reserved. * * NOTICE: Adobe permits you to use, modify, and distribute this file in * accordance with the terms of the Adobe license agreement accompanying * it. If you have received this file from a source other than Adobe, * then your use, modification, or distribution of it requires the prior * written permission of Adobe. */ const inputTypes = require('../internal/io/input-types'), FileInfo = require('../internal/io/file-info'); /** * This class represents a local file. It is typically used by an SDK Operation * which accepts or returns files. * <p> * When a FileRef instance is created by this SDK while referring to a temporary file location, calling any of the methods * to save the fileRef (For example, {@link FileRef#writeToStream}, {@link FileRef#saveAsFile} etc.) will delete the * temporary file. * */ class FileRef { /** * @hideconstructor * @param input */ constructor(input) { Object.defineProperty(this, 'input', { value: input, writable: false }); Object.freeze(this); } /** * Creates a FileRef instance from a local file path. If no media type is provided, it will be inferred from the file * extension. * <p> * @param {!string} filePath - Local file path, either absolute path or relative to the working directory. * @param {string=} mediaType - Media type to identify the local file format. * @returns {FileRef} A FileRef instance. */ static createFromLocalFile(filePath, mediaType) { const fileInfo = new FileInfo(filePath, mediaType, inputTypes.localFile); return new FileRef(fileInfo); } /** * Creates a FileRef instance from a readable stream using the specified media type. * <p> * The stream is not read by this method but by consumers of file content i.e. the execute method of * an operation such as {@link ExtractPDFOperation#execute} . * * @param {!stream.Readable} inputStream - Readable Stream representing the file. * @param {!string} mediaType - Media type to identify the file format. * @returns {FileRef} A FileRef instance. */ static createFromStream(inputStream, mediaType) { const fileInfo = new FileInfo(inputStream, mediaType, inputTypes.stream); return new FileRef(fileInfo); } /** * * Saves this file to the location specified by <code>destinationFilePath</code>. * If this FileRef instance refers to a temporary local file created by the SDK, that temporary file is deleted. * <p> * The directories mentioned in the specified argument are created if they do not exist. * * @param {!string} destinationFilePath - Local file path, either an absolute path or relative to the working directory. * @throws {Error} If the file already exists. * @throws {Error} If this FileRef instance does not represent an operation result. * @returns {Promise<void>} A `Promise` that resolves when the fileRef instance has been moved to the specified location. * */ saveAsFile(destinationFilePath) { return this.input.saveAsFile(destinationFilePath); } /** * Writes the contents of this file to <code>writableStream</code>. * If this FileRef instance refers to a temporary local file created by the SDK, that temporary file is deleted. * <br> * Note: This method does not close the <code>writableStream</code>. * * @param {!stream.Writable} writableStream - The destination writable stream. * @throws {Error} If this FileRef instance does not represent an operation result. * */ writeToStream(writableStream) { this.input.writeToStream(writableStream); } } Object.freeze(FileRef); module.exports = FileRef;