Skip to content

Commit

Permalink
Fix bulk convertion (#183)
Browse files Browse the repository at this point in the history
  • Loading branch information
mskec committed Sep 5, 2023
1 parent 2b4b5fb commit 2f8efc9
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 7 deletions.
14 changes: 8 additions & 6 deletions src/pdf2picCore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
Expand Down Expand Up @@ -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
Expand Down
17 changes: 17 additions & 0 deletions src/utils/converters/convertToBuffer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { promises as fs } from 'fs';

export async function convertToBuffer(source: string, data: string | Buffer): Promise<Buffer> {
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');
}
1 change: 0 additions & 1 deletion test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down

0 comments on commit 2f8efc9

Please sign in to comment.