mirror of
https://github.com/Frooodle/Stirling-PDF.git
synced 2025-02-07 00:17:07 +01:00
Fixed up straggling errors
This commit is contained in:
parent
063acc6bbf
commit
ed4c7d9400
@ -1,6 +1,6 @@
|
||||
|
||||
import { readBinaryFile, writeBinaryFile, removeDir, BaseDirectory } from '@tauri-apps/api/fs';
|
||||
import { PdfFile, fromUint8Array } from '@stirling-pdf/shared-operations/src/wrappers/PdfFile'
|
||||
import { PdfFile,RepresentationType } from '@stirling-pdf/shared-operations/src/wrappers/PdfFile'
|
||||
import { runShell } from './tauri-wrapper';
|
||||
|
||||
export async function fileToPdf(byteArray: Uint8Array, filename: string): Promise<PdfFile> {
|
||||
@ -26,7 +26,7 @@ export async function fileToPdf(byteArray: Uint8Array, filename: string): Promis
|
||||
|
||||
await removeDir(tempDir);
|
||||
|
||||
return fromUint8Array(outputBytes, outputFileName);
|
||||
return new PdfFile(outputFileName, outputBytes, RepresentationType.Uint8Array);
|
||||
}
|
||||
|
||||
export async function isLibreOfficeInstalled() {
|
||||
|
@ -1,14 +1,17 @@
|
||||
|
||||
import SharedOperations, { OperationsUseages } from '@stirling-pdf/shared-operations/src'
|
||||
import SharedOperations, { OperationsType } from '@stirling-pdf/shared-operations/src'
|
||||
import { ImposeParamsType } from '@stirling-pdf/shared-operations/src/functions/impose'
|
||||
import { PdfFile } from "@stirling-pdf/shared-operations/src/wrappers/PdfFile"
|
||||
|
||||
// Import injected libraries here!
|
||||
import * as pdfcpuWrapper from "@stirling-pdf/shared-operations/wasm/pdfcpu/pdfcpu-wrapper-browser.js";
|
||||
|
||||
async function impose(snapshot: any, nup: number, format: string) {
|
||||
return SharedOperations.impose(snapshot, nup, format, pdfcpuWrapper)
|
||||
async function impose(params: ImposeParamsType): Promise<PdfFile> {
|
||||
const paramsToUse = { ...params, pdfcpuWrapper: pdfcpuWrapper };
|
||||
return SharedOperations.impose(paramsToUse);
|
||||
}
|
||||
|
||||
const toExport: OperationsUseages = {
|
||||
const toExport: OperationsType = {
|
||||
...SharedOperations,
|
||||
impose,
|
||||
}
|
||||
|
@ -12,8 +12,8 @@ export async function respondWithFile(res: Response, bytes: Uint8Array, name: st
|
||||
}
|
||||
|
||||
export async function respondWithPdfFile(res: Response, file: PdfFile): Promise<void> {
|
||||
const byteFile = await file.convertToByteArrayFile();
|
||||
respondWithFile(res, byteFile.byteArray!, byteFile.filename, "application/pdf");
|
||||
const byteArray = await file.uint8Array;
|
||||
respondWithFile(res, byteArray, file.filename, "application/pdf");
|
||||
}
|
||||
|
||||
export function response_mustHaveExactlyOneFile(res: Response): void {
|
||||
|
@ -1,7 +1,7 @@
|
||||
|
||||
import SharedOperations, { OperationsType } from "@stirling-pdf/shared-operations/src";
|
||||
import SharedOperations, { OperationsType } from "@stirling-pdf/shared-operations/src"
|
||||
import { ImposeParamsType } from '@stirling-pdf/shared-operations/src/functions/impose'
|
||||
import { PdfFile } from "@stirling-pdf/shared-operations/src/wrappers/PdfFile";
|
||||
import { PdfFile } from "@stirling-pdf/shared-operations/src/wrappers/PdfFile"
|
||||
|
||||
// Import injected libraries here!
|
||||
//import * as pdfcpuWrapper from "@stirling-pdf/shared-operations/wasm/pdfcpu/pdfcpu-wrapper-node.js";
|
||||
|
@ -1,16 +1,16 @@
|
||||
|
||||
import { PdfFile, convertAllToPdfLibFile } from '../../wrappers/PdfFile';
|
||||
import { PdfFile } from '../../wrappers/PdfFile';
|
||||
|
||||
export async function sortPdfs(
|
||||
files: PdfFile[],
|
||||
sortType: "orderProvided"|"byFileName"|"byDateModified"|"byDateCreated"|"byPDFTitle" = "orderProvided"
|
||||
): Promise<PdfFile[]> {
|
||||
|
||||
const pdfLibFiles = await convertAllToPdfLibFile(files);
|
||||
|
||||
const docCache = await PdfFile.cacheAsPdfLibDocuments(files);
|
||||
|
||||
switch(sortType) {
|
||||
case "byFileName":
|
||||
pdfLibFiles.sort((a, b) => {
|
||||
files.sort((a, b) => {
|
||||
if (!a || !b) return 0;
|
||||
const ad = a.filename, bd = b.filename;
|
||||
if (!ad || !bd) return 0;
|
||||
@ -18,30 +18,30 @@ export async function sortPdfs(
|
||||
});
|
||||
break;
|
||||
case "byDateModified":
|
||||
pdfLibFiles.sort((a, b) => {
|
||||
const ad = a.pdfLib?.getModificationDate()?.getTime();
|
||||
const bd = b.pdfLib?.getModificationDate()?.getTime();
|
||||
files.sort((a, b) => {
|
||||
const ad = docCache.get(a)?.getModificationDate()?.getTime();
|
||||
const bd = docCache.get(b)?.getModificationDate()?.getTime();
|
||||
if (!ad || !bd) return 0;
|
||||
return ad > bd ? 1 : -1
|
||||
});
|
||||
break;
|
||||
case "byDateCreated":
|
||||
pdfLibFiles.sort((a, b) => {
|
||||
const ad = a.pdfLib?.getCreationDate()?.getTime();
|
||||
const bd = b.pdfLib?.getCreationDate()?.getTime();
|
||||
files.sort((a, b) => {
|
||||
const ad = docCache.get(a)?.getCreationDate()?.getTime();
|
||||
const bd = docCache.get(b)?.getCreationDate()?.getTime();
|
||||
if (!ad || !bd) return 0;
|
||||
return ad > bd ? 1 : -1
|
||||
});
|
||||
break;
|
||||
case "byPDFTitle":
|
||||
pdfLibFiles.sort((a, b) => {
|
||||
const ad = a.pdfLib?.getTitle();
|
||||
const bd = b.pdfLib?.getTitle();
|
||||
files.sort((a, b) => {
|
||||
const ad = docCache.get(a)?.getTitle();
|
||||
const bd = docCache.get(b)?.getTitle();
|
||||
if (!ad || !bd) return 0;
|
||||
return ad.localeCompare(bd);
|
||||
});
|
||||
break;
|
||||
}
|
||||
|
||||
return pdfLibFiles;
|
||||
return files;
|
||||
}
|
@ -10,10 +10,9 @@ export type SplitPdfParamsType = {
|
||||
export async function splitPDF(params: SplitPdfParamsType): Promise<PdfFile[]> {
|
||||
const { file, splitAfterPageArray } = params;
|
||||
|
||||
const byteFile = await file.convertToPdfLibFile();
|
||||
if (!byteFile?.pdfLib) return [];
|
||||
const pdflibDocument = await file.pdflibDocument;
|
||||
|
||||
const numberOfPages = byteFile.pdfLib.getPages().length;
|
||||
const numberOfPages = pdflibDocument.getPages().length;
|
||||
|
||||
let pagesArray: number[] = [];
|
||||
let splitAfter = splitAfterPageArray.shift();
|
||||
@ -21,13 +20,13 @@ export async function splitPDF(params: SplitPdfParamsType): Promise<PdfFile[]> {
|
||||
|
||||
for (let i = 0; i < numberOfPages; i++) {
|
||||
if(splitAfter && i > splitAfter && pagesArray.length > 0) {
|
||||
subDocuments.push(await selectPages({file:byteFile, pagesToExtractArray:pagesArray}));
|
||||
subDocuments.push(await selectPages({file, pagesToExtractArray:pagesArray}));
|
||||
splitAfter = splitAfterPageArray.shift();
|
||||
pagesArray = [];
|
||||
}
|
||||
pagesArray.push(i);
|
||||
}
|
||||
subDocuments.push(await selectPages({file:byteFile, pagesToExtractArray:pagesArray}));
|
||||
subDocuments.push(await selectPages({file, pagesToExtractArray:pagesArray}));
|
||||
pagesArray = [];
|
||||
|
||||
return subDocuments;
|
||||
|
@ -99,6 +99,31 @@ export class PdfFile {
|
||||
static fromMulterFiles(values: Express.Multer.File[]): PdfFile[] {
|
||||
return values.map(v => PdfFile.fromMulterFile(v));
|
||||
}
|
||||
|
||||
static async cacheAsUint8Arrays(files: PdfFile[]): Promise<Map<PdfFile, Uint8Array>> {
|
||||
const docCache = new Map<PdfFile, Uint8Array>();
|
||||
await Promise.all(files.map(async (file) => {
|
||||
const pdfLibDocument = await file.uint8Array;
|
||||
docCache.set(file, pdfLibDocument);
|
||||
}));
|
||||
return docCache;
|
||||
}
|
||||
static async cacheAsPdfLibDocuments(files: PdfFile[]): Promise<Map<PdfFile, PDFLibDocument>> {
|
||||
const docCache = new Map<PdfFile, PDFLibDocument>();
|
||||
await Promise.all(files.map(async (file) => {
|
||||
const pdfLibDocument = await file.pdflibDocument;
|
||||
docCache.set(file, pdfLibDocument);
|
||||
}));
|
||||
return docCache;
|
||||
}
|
||||
static async cacheAsPdfJsDocuments(files: PdfFile[]): Promise<Map<PdfFile, PDFJSDocument>> {
|
||||
const docCache = new Map<PdfFile, PDFJSDocument>();
|
||||
await Promise.all(files.map(async (file) => {
|
||||
const pdfLibDocument = await file.pdfjsDocument;
|
||||
docCache.set(file, pdfLibDocument);
|
||||
}));
|
||||
return docCache;
|
||||
}
|
||||
}
|
||||
|
||||
export const PdfFileSchema = Joi.any().custom((value, helpers) => {
|
||||
|
Loading…
Reference in New Issue
Block a user