Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bulk convertion (#183) #184

Merged
merged 1 commit into from
Sep 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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