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 { 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';
|
import { runShell } from './tauri-wrapper';
|
||||||
|
|
||||||
export async function fileToPdf(byteArray: Uint8Array, filename: string): Promise<PdfFile> {
|
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);
|
await removeDir(tempDir);
|
||||||
|
|
||||||
return fromUint8Array(outputBytes, outputFileName);
|
return new PdfFile(outputFileName, outputBytes, RepresentationType.Uint8Array);
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function isLibreOfficeInstalled() {
|
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 injected libraries here!
|
||||||
import * as pdfcpuWrapper from "@stirling-pdf/shared-operations/wasm/pdfcpu/pdfcpu-wrapper-browser.js";
|
import * as pdfcpuWrapper from "@stirling-pdf/shared-operations/wasm/pdfcpu/pdfcpu-wrapper-browser.js";
|
||||||
|
|
||||||
async function impose(snapshot: any, nup: number, format: string) {
|
async function impose(params: ImposeParamsType): Promise<PdfFile> {
|
||||||
return SharedOperations.impose(snapshot, nup, format, pdfcpuWrapper)
|
const paramsToUse = { ...params, pdfcpuWrapper: pdfcpuWrapper };
|
||||||
|
return SharedOperations.impose(paramsToUse);
|
||||||
}
|
}
|
||||||
|
|
||||||
const toExport: OperationsUseages = {
|
const toExport: OperationsType = {
|
||||||
...SharedOperations,
|
...SharedOperations,
|
||||||
impose,
|
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> {
|
export async function respondWithPdfFile(res: Response, file: PdfFile): Promise<void> {
|
||||||
const byteFile = await file.convertToByteArrayFile();
|
const byteArray = await file.uint8Array;
|
||||||
respondWithFile(res, byteFile.byteArray!, byteFile.filename, "application/pdf");
|
respondWithFile(res, byteArray, file.filename, "application/pdf");
|
||||||
}
|
}
|
||||||
|
|
||||||
export function response_mustHaveExactlyOneFile(res: Response): void {
|
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 { 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 injected libraries here!
|
||||||
//import * as pdfcpuWrapper from "@stirling-pdf/shared-operations/wasm/pdfcpu/pdfcpu-wrapper-node.js";
|
//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(
|
export async function sortPdfs(
|
||||||
files: PdfFile[],
|
files: PdfFile[],
|
||||||
sortType: "orderProvided"|"byFileName"|"byDateModified"|"byDateCreated"|"byPDFTitle" = "orderProvided"
|
sortType: "orderProvided"|"byFileName"|"byDateModified"|"byDateCreated"|"byPDFTitle" = "orderProvided"
|
||||||
): Promise<PdfFile[]> {
|
): Promise<PdfFile[]> {
|
||||||
|
|
||||||
const pdfLibFiles = await convertAllToPdfLibFile(files);
|
const docCache = await PdfFile.cacheAsPdfLibDocuments(files);
|
||||||
|
|
||||||
switch(sortType) {
|
switch(sortType) {
|
||||||
case "byFileName":
|
case "byFileName":
|
||||||
pdfLibFiles.sort((a, b) => {
|
files.sort((a, b) => {
|
||||||
if (!a || !b) return 0;
|
if (!a || !b) return 0;
|
||||||
const ad = a.filename, bd = b.filename;
|
const ad = a.filename, bd = b.filename;
|
||||||
if (!ad || !bd) return 0;
|
if (!ad || !bd) return 0;
|
||||||
@ -18,30 +18,30 @@ export async function sortPdfs(
|
|||||||
});
|
});
|
||||||
break;
|
break;
|
||||||
case "byDateModified":
|
case "byDateModified":
|
||||||
pdfLibFiles.sort((a, b) => {
|
files.sort((a, b) => {
|
||||||
const ad = a.pdfLib?.getModificationDate()?.getTime();
|
const ad = docCache.get(a)?.getModificationDate()?.getTime();
|
||||||
const bd = b.pdfLib?.getModificationDate()?.getTime();
|
const bd = docCache.get(b)?.getModificationDate()?.getTime();
|
||||||
if (!ad || !bd) return 0;
|
if (!ad || !bd) return 0;
|
||||||
return ad > bd ? 1 : -1
|
return ad > bd ? 1 : -1
|
||||||
});
|
});
|
||||||
break;
|
break;
|
||||||
case "byDateCreated":
|
case "byDateCreated":
|
||||||
pdfLibFiles.sort((a, b) => {
|
files.sort((a, b) => {
|
||||||
const ad = a.pdfLib?.getCreationDate()?.getTime();
|
const ad = docCache.get(a)?.getCreationDate()?.getTime();
|
||||||
const bd = b.pdfLib?.getCreationDate()?.getTime();
|
const bd = docCache.get(b)?.getCreationDate()?.getTime();
|
||||||
if (!ad || !bd) return 0;
|
if (!ad || !bd) return 0;
|
||||||
return ad > bd ? 1 : -1
|
return ad > bd ? 1 : -1
|
||||||
});
|
});
|
||||||
break;
|
break;
|
||||||
case "byPDFTitle":
|
case "byPDFTitle":
|
||||||
pdfLibFiles.sort((a, b) => {
|
files.sort((a, b) => {
|
||||||
const ad = a.pdfLib?.getTitle();
|
const ad = docCache.get(a)?.getTitle();
|
||||||
const bd = b.pdfLib?.getTitle();
|
const bd = docCache.get(b)?.getTitle();
|
||||||
if (!ad || !bd) return 0;
|
if (!ad || !bd) return 0;
|
||||||
return ad.localeCompare(bd);
|
return ad.localeCompare(bd);
|
||||||
});
|
});
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
return pdfLibFiles;
|
return files;
|
||||||
}
|
}
|
@ -10,10 +10,9 @@ export type SplitPdfParamsType = {
|
|||||||
export async function splitPDF(params: SplitPdfParamsType): Promise<PdfFile[]> {
|
export async function splitPDF(params: SplitPdfParamsType): Promise<PdfFile[]> {
|
||||||
const { file, splitAfterPageArray } = params;
|
const { file, splitAfterPageArray } = params;
|
||||||
|
|
||||||
const byteFile = await file.convertToPdfLibFile();
|
const pdflibDocument = await file.pdflibDocument;
|
||||||
if (!byteFile?.pdfLib) return [];
|
|
||||||
|
|
||||||
const numberOfPages = byteFile.pdfLib.getPages().length;
|
const numberOfPages = pdflibDocument.getPages().length;
|
||||||
|
|
||||||
let pagesArray: number[] = [];
|
let pagesArray: number[] = [];
|
||||||
let splitAfter = splitAfterPageArray.shift();
|
let splitAfter = splitAfterPageArray.shift();
|
||||||
@ -21,13 +20,13 @@ export async function splitPDF(params: SplitPdfParamsType): Promise<PdfFile[]> {
|
|||||||
|
|
||||||
for (let i = 0; i < numberOfPages; i++) {
|
for (let i = 0; i < numberOfPages; i++) {
|
||||||
if(splitAfter && i > splitAfter && pagesArray.length > 0) {
|
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();
|
splitAfter = splitAfterPageArray.shift();
|
||||||
pagesArray = [];
|
pagesArray = [];
|
||||||
}
|
}
|
||||||
pagesArray.push(i);
|
pagesArray.push(i);
|
||||||
}
|
}
|
||||||
subDocuments.push(await selectPages({file:byteFile, pagesToExtractArray:pagesArray}));
|
subDocuments.push(await selectPages({file, pagesToExtractArray:pagesArray}));
|
||||||
pagesArray = [];
|
pagesArray = [];
|
||||||
|
|
||||||
return subDocuments;
|
return subDocuments;
|
||||||
|
@ -99,6 +99,31 @@ export class PdfFile {
|
|||||||
static fromMulterFiles(values: Express.Multer.File[]): PdfFile[] {
|
static fromMulterFiles(values: Express.Multer.File[]): PdfFile[] {
|
||||||
return values.map(v => PdfFile.fromMulterFile(v));
|
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) => {
|
export const PdfFileSchema = Joi.any().custom((value, helpers) => {
|
||||||
|
Loading…
Reference in New Issue
Block a user