From 2f8efc96b45e2d6bef8164efb37196bc3d26fa4a Mon Sep 17 00:00:00 2001 From: Martin Skec Date: Tue, 5 Sep 2023 16:39:26 +0200 Subject: [PATCH] Fix bulk convertion (#183) --- src/pdf2picCore.ts | 14 ++++++++------ src/utils/converters/convertToBuffer.ts | 17 +++++++++++++++++ test/index.test.ts | 1 - 3 files changed, 25 insertions(+), 7 deletions(-) create mode 100644 src/utils/converters/convertToBuffer.ts diff --git a/src/pdf2picCore.ts b/src/pdf2picCore.ts index 539bbc4..eb203c3 100644 --- a/src/pdf2picCore.ts +++ b/src/pdf2picCore.ts @@ -3,12 +3,14 @@ import { Graphics } from "./graphics"; import type { Convert, ConvertOptions } from "./types/convert"; import type { ConvertResponse } from './types/convertResponse'; import type { Options } from "./types/options"; -import { convertToStream } from "./utils/converters/convertToStream"; +import { bufferToStream } from './utils/converters/bufferToStream'; +import { convertToBuffer } from "./utils/converters/convertToBuffer"; +import { convertToStream } from './utils/converters/convertToStream'; import { defaultOptions } from "./utils/defaultOptions"; import { getPages } from './utils/getPages'; import { resolveResponseType } from './utils/resolveResponseType'; -export function pdf2picCore(source: string, filePath: string | Buffer, options = defaultOptions): Convert { +export function pdf2picCore(source: string, data: string | Buffer, options = defaultOptions): Convert { const gm = new Graphics(); options = { ...defaultOptions, ...options }; @@ -36,20 +38,20 @@ export function pdf2picCore(source: string, filePath: string | Buffer, options = } const convert = (page = 1, convertOptions) => { - const stream = convertToStream(source, filePath); + const stream = convertToStream(source, data); return _convert(stream, page, convertOptions) }; convert.bulk = async (pages, convertOptions) => { + const buffer = await convertToBuffer(source, data); const pagesToConvert = pages === -1 - ? await getPages(gm, convertToStream(source, filePath)) + ? await getPages(gm, bufferToStream(buffer)) : Array.isArray(pages) ? pages : [pages]; const results = [] const batchSize = 10 - const stream = convertToStream(source, filePath); for (let i = 0; i < pagesToConvert.length; i += batchSize) { - results.push(...await _bulk(stream, pagesToConvert.slice(i, i + batchSize), convertOptions)) + results.push(...await _bulk(bufferToStream(buffer), pagesToConvert.slice(i, i + batchSize), convertOptions)) } return results diff --git a/src/utils/converters/convertToBuffer.ts b/src/utils/converters/convertToBuffer.ts new file mode 100644 index 0000000..c31ac83 --- /dev/null +++ b/src/utils/converters/convertToBuffer.ts @@ -0,0 +1,17 @@ +import { promises as fs } from 'fs'; + +export async function convertToBuffer(source: string, data: string | Buffer): Promise { + if (source === 'buffer') { + return data as Buffer; + } + + if (source === 'path') { + return await fs.readFile(data as string); + } + + if (source === 'base64') { + return Buffer.from(data as string, 'base64'); + } + + throw new Error('Cannot recognize specified source'); +} diff --git a/test/index.test.ts b/test/index.test.ts index 991451e..43bd26a 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -3,7 +3,6 @@ import { mkdirSync, readFileSync, writeFileSync } from "fs"; import gm from "gm"; import path from 'path'; import { rimrafSync } from "rimraf"; -import rimraf from "rimraf"; import { fromBase64, fromBuffer, fromPath } from "../src/index"; import { Graphics } from "../src/graphics"; import { BufferResponse, ToBase64Response, WriteImageResponse } from "../src/types/convertResponse";