Skip to content
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
18 changes: 8 additions & 10 deletions src/hooks/custom/LoggerHook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ export class LoggerHook implements AfterSuccessHook, AfterErrorHook {
afterSuccess(hookCtx: AfterSuccessContext, response: Response): Response {
this.retriesCounter.delete(hookCtx.operationID);
// NOTE: In case of split page partition this means - at least one of the splits was partitioned successfully
console.info("Successfully partitioned the document.");
return response;
}

Expand All @@ -67,15 +66,14 @@ export class LoggerHook implements AfterSuccessHook, AfterErrorHook {
this.logRetries(response, error, hookCtx.operationID);

if (response && response.status === 200) {
console.info("Successfully partitioned the document.");
} else {
console.error("Failed to partition the document.");
if (response) {
console.error(`Server responded with ${response.status} - ${response.statusText}`);
}
if (error) {
console.error(`Following error occurred - ${(error as Error).message}`);
}
return { response, error };
}
console.error("Failed to partition the document.");
if (response) {
console.error(`Server responded with ${response.status} - ${response.statusText}`);
}
if (error) {
console.error(`Following error occurred - ${(error as Error).message}`);
}
return { response, error };
}
Expand Down
31 changes: 1 addition & 30 deletions src/hooks/custom/SplitPdfHook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,36 +110,29 @@ export class SplitPdfHook
const file = formData.get(PARTITION_FORM_FILES_KEY) as File | null;

if (!splitPdfPage) {
console.info("Partitioning without split.")
return request;
}

console.info("Preparing to split document for partition.")
if (!this.client) {
console.warn("HTTP client not accessible! Partitioning without split.");
return request;
}

const [error, pdf, totalPages] = await loadPdf(file);
if (file === null || pdf === null || error) {
console.info("Partitioning without split.")
return request;
}

const [pageRangeStart, pageRangeEnd] = getSplitPdfPageRange(formData, totalPages);
const pagesCount = pageRangeEnd - pageRangeStart + 1;

const startingPageNumber = getStartingPageNumber(formData);
console.info("Starting page number set to %d", startingPageNumber);

const concurrencyLevel = getSplitPdfConcurrencyLevel(formData);
console.info("Concurrency level set to %d", concurrencyLevel)

this.allowFailed = getSplitPdfAllowFailed(formData);
console.info("Allow failed set to %s", this.allowFailed)

const splitSize = await getOptimalSplitSize(pagesCount, concurrencyLevel);
console.info("Determined optimal split size of %d pages.", splitSize)

// If user wants a specific page range, we need to call splitPdf,
// even if this page count is too small to be split normally
Expand All @@ -148,26 +141,11 @@ export class SplitPdfHook
// Otherwise, if there are not enough pages, return the original request without splitting
if (!isPageRangeRequested) {
if (splitSize >= pagesCount || pagesCount < MIN_PAGES_PER_THREAD) {
console.info(
"Document has too few pages (%d) to be split efficiently. Partitioning without split.",
pagesCount,
)
return request;
}
}

const splits = await splitPdf(pdf, splitSize, pageRangeStart, pageRangeEnd);
const numberOfSplits = splits.length
console.info(
"Document split into %d, %d-paged sets.",
numberOfSplits,
splitSize,
)
console.info(
"Partitioning %d, %d-paged sets.",
numberOfSplits,
splitSize,
)

const oneSecond = 1000;
const oneMinute = 1000 * 60;
Expand All @@ -181,12 +159,6 @@ export class SplitPdfHook
for (const { content, startPage } of splits) {
// Both startPage and startingPageNumber are 1-based, so we need to subtract 1
const firstPageNumber = startPage + startingPageNumber - 1;
console.info(
"Partitioning set #%d (pages %d-%d).",
setIndex,
firstPageNumber,
Math.min(firstPageNumber + splitSize - 1, pagesCount),
);

const body = await prepareRequestBody(
formData,
Expand Down Expand Up @@ -261,8 +233,7 @@ export class SplitPdfHook
concurrencyLevel
);

const dummyRequest = new Request("https://no-op/");
return dummyRequest;
return new Request("https://no-op/");
}

/**
Expand Down
26 changes: 0 additions & 26 deletions src/hooks/custom/utils/form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,13 +135,6 @@ export function getSplitPdfConcurrencyLevel(formData: FormData): number {
);
splitPdfConcurrencyLevel = DEFAULT_NUMBER_OF_PARALLEL_REQUESTS;
}

console.info(
`Splitting PDF by page on client. Using ${splitPdfConcurrencyLevel} threads when calling API.`
);
console.info(
`Set ${PARTITION_FORM_SPLIT_PDF_CONCURRENCY_LEVEL} parameter if you want to change that.`
);
return splitPdfConcurrencyLevel;
}

Expand All @@ -165,25 +158,6 @@ export function getSplitPdfAllowFailed(formData: FormData): boolean {
PARTITION_FORM_SPLIT_PDF_ALLOW_FAILED_KEY,
DEFAULT_SPLIT_PDF_ALLOW_FAILED_KEY
);


if (splitPdfAllowFailed) {
console.info(
`Running split PDF requests in parallel with no-strict mode -
the failed requests will not stop the process, and the resulting elements might miss
some pages in case of failure.`
);
} else {
console.info(
`Running split PDF requests in parallel with strict mode -
the failed requests will stop the process, and the resulting elements will have all pages
or error out.`
)
}

console.info(
`Set ${PARTITION_FORM_SPLIT_PDF_CONCURRENCY_LEVEL} parameter if you want to change that.`
);
return splitPdfAllowFailed;
}

Expand Down
1 change: 0 additions & 1 deletion src/hooks/custom/utils/pdf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,6 @@ export async function loadPdf(
file: File | null
): Promise<[boolean, PDFDocument | null, number]> {
if (!file) {
console.info("Given file is null, so splitting is not enabled.");
return [true, null, 0];
}

Expand Down
4 changes: 1 addition & 3 deletions src/hooks/custom/utils/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,7 @@ export async function prepareResponseBody(
const allElements: any[] = [];
let index = 1;
for (const res of responses) {
if (res.status == 200) {
console.info("Successfully partitioned set #%d, elements added to the final result.", index);
} else {
if (res.status != 200) {
console.warn("Failed to partition set #%d, its elements will be omitted in the final result.", index);
}

Expand Down
Loading