Appended operation names to filenames

This commit is contained in:
Saud Fatayerji 2023-11-17 00:45:37 +03:00
parent 544a080db4
commit fa36d5d296
4 changed files with 30 additions and 27 deletions

View File

@ -6,7 +6,7 @@ export type ExtractPagesParamsType = {
file: PdfFile; file: PdfFile;
pageIndexes: string | number[]; pageIndexes: string | number[];
} }
export async function extractPages(params: ExtractPagesParamsType) { export async function extractPages(params: ExtractPagesParamsType): Promise<PdfFile> {
const { file, pageIndexes } = params; const { file, pageIndexes } = params;
const pdfLibDocument = await file.pdfLibDocument; const pdfLibDocument = await file.pdfLibDocument;
@ -16,5 +16,7 @@ export async function extractPages(params: ExtractPagesParamsType) {
indexes = parsePageIndexSpecification(indexes, pdfLibDocument.getPageCount()); indexes = parsePageIndexSpecification(indexes, pdfLibDocument.getPageCount());
} }
return getPages(file, indexes); const newFile = await getPages(file, indexes);
newFile.filename += "_extractedPages"
return newFile;
} }

View File

@ -14,5 +14,8 @@ export async function removeBlankPages(params: RemoveBlankPagesParamsType) {
const emptyPages = await detectEmptyPages(file, whiteThreashold); const emptyPages = await detectEmptyPages(file, whiteThreashold);
console.debug("Empty Pages: ", emptyPages); console.debug("Empty Pages: ", emptyPages);
const pagesToKeep = invertSelection(emptyPages, pageCount.getPageCount()) const pagesToKeep = invertSelection(emptyPages, pageCount.getPageCount())
return getPages(file, pagesToKeep);
const newFile = await getPages(file, pagesToKeep);
newFile.filename += "_removedBlanks"
return newFile;
} }

View File

@ -18,5 +18,8 @@ export async function sortPagesWithPreset(params: SortPagesWithPresetParamsType)
const sortFunction = sorts[sortPreset]; const sortFunction = sorts[sortPreset];
const pageCount = pdfLibDocument.getPageCount(); const pageCount = pdfLibDocument.getPageCount();
const sortIndexes = sortFunction(pageCount); const sortIndexes = sortFunction(pageCount);
return getPages(file, sortIndexes);
const newFile = await getPages(file, sortIndexes);
newFile.filename += "_sortedPages"
return newFile;
} }

View File

@ -55,7 +55,6 @@ export async function * traverseOperations(operations: Action[], input: PdfFile[
case "extract": case "extract":
yield* nToN(input, action, async (input) => { yield* nToN(input, action, async (input) => {
const newPdf = await Operations.extractPages({file: input, pageIndexes: action.values["pageIndexes"]}); const newPdf = await Operations.extractPages({file: input, pageIndexes: action.values["pageIndexes"]});
newPdf.filename += "_extractedPages";
return newPdf; return newPdf;
}); });
break; break;
@ -71,12 +70,24 @@ export async function * traverseOperations(operations: Action[], input: PdfFile[
return newPdf; return newPdf;
}); });
break; break;
case "removeBlankPages":
yield* nToN(input, action, async (input) => {
const newPdf = await Operations.removeBlankPages({file: input, whiteThreashold: action.values["whiteThreashold"]});
return newPdf;
});
break;
case "rotate": case "rotate":
yield* nToN(input, action, async (input) => { yield* nToN(input, action, async (input) => {
const newPdf = await Operations.rotatePages({file: input, rotation: action.values["rotation"]}); const newPdf = await Operations.rotatePages({file: input, rotation: action.values["rotation"]});
return newPdf; return newPdf;
}); });
break; break;
case "sortPagesWithPreset":
yield* nToN(input, action, async (input) => {
const newPdf = await Operations.sortPagesWithPreset({file: input, sortPreset: action.values["sortPreset"]});
return newPdf;
});
break;
case "split": case "split":
// TODO: A split might break the done condition, it may count multiple times. Needs further testing! // TODO: A split might break the done condition, it may count multiple times. Needs further testing!
yield* oneToN(input, action, async (input) => { yield* oneToN(input, action, async (input) => {
@ -87,27 +98,6 @@ export async function * traverseOperations(operations: Action[], input: PdfFile[
return splitResult; return splitResult;
}); });
break; break;
case "updateMetadata":
yield* nToN(input, action, async (input) => {
const newPdf = await Operations.updateMetadata({file: input, ...action.values["metadata"]});
newPdf.filename += "_metadataEdited";
return newPdf;
});
break;
case "sortPagesWithPreset":
yield* nToN(input, action, async (input) => {
const newPdf = await Operations.sortPagesWithPreset({file: input, sortPreset: action.values["sortPreset"]});
newPdf.filename += "_pagesOrganized";
return newPdf;
});
break;
case "removeBlankPages":
yield* nToN(input, action, async (input) => {
const newPdf = await Operations.removeBlankPages({file: input, whiteThreashold: action.values["whiteThreashold"]});
newPdf.filename += "_removedBlanks";
return newPdf;
});
break;
case "splitOn": case "splitOn":
yield* oneToN(input, action, async (input) => { yield* oneToN(input, action, async (input) => {
const splitResult = await Operations.splitOn({file: input, type: action.values["type"], whiteThreashold: action.values["whiteThreashold"]}); const splitResult = await Operations.splitOn({file: input, type: action.values["type"], whiteThreashold: action.values["whiteThreashold"]});
@ -117,9 +107,14 @@ export async function * traverseOperations(operations: Action[], input: PdfFile[
return splitResult; return splitResult;
}); });
break; break;
case "updateMetadata":
yield* nToN(input, action, async (input) => {
const newPdf = await Operations.updateMetadata({file: input, ...action.values["metadata"]});
return newPdf;
});
break;
default: default:
throw new Error(`${action.type} not implemented yet.`); throw new Error(`${action.type} not implemented yet.`);
break;
} }
} }