restructured project (shared-operations)

This commit is contained in:
Felix Kaspar 2023-11-07 01:40:00 +01:00
parent 7186c6c3e0
commit 47e0092378
36 changed files with 36 additions and 23 deletions

View File

@ -2,7 +2,8 @@
// PDFJS as pdfjsLib via index.html script-tag // PDFJS as pdfjsLib via index.html script-tag
// jsQR via index.html script-tag // jsQR via index.html script-tag
import * as pdfcpuWraopper from "./wasm/pdfcpu-wrapper-browser.js"; import * as pdfcpuWraopper from "./wasm/pdfcpu/pdfcpu-wrapper-browser.js";
const OpenCV = { cv: cv } // OPENCV gets importet as cv via index.html script-tag const OpenCV = { cv: cv } // OPENCV gets importet as cv via index.html script-tag
import { extractPages as dependantExtractPages } from "./functions/extractPages.js"; import { extractPages as dependantExtractPages } from "./functions/extractPages.js";
@ -17,6 +18,8 @@ import { organizePages as dependantOrganizePages} from "./functions/organizePage
import { removeBlankPages as dependantRemoveBlankPages} from "./functions/removeBlankPages.js"; import { removeBlankPages as dependantRemoveBlankPages} from "./functions/removeBlankPages.js";
import { splitOn as dependantSplitOn } from "./functions/splitOn.js"; import { splitOn as dependantSplitOn } from "./functions/splitOn.js";
// TODO: Dynamic loading & undloading of libraries.
export async function extractPages(snapshot, pagesToExtractArray) { export async function extractPages(snapshot, pagesToExtractArray) {
return dependantExtractPages(snapshot, pagesToExtractArray, PDFLib); return dependantExtractPages(snapshot, pagesToExtractArray, PDFLib);
} }

View File

@ -12,7 +12,6 @@ import * as Functions from "./functions.js";
console.log("Starting..."); console.log("Starting...");
const files = Array.from(pdfFileInput.files); const files = Array.from(pdfFileInput.files);
console.log(files);
const inputs = await Promise.all(files.map(async file => { const inputs = await Promise.all(files.map(async file => {
return { return {
originalFileName: file.name.replace(/\.[^/.]+$/, ""), originalFileName: file.name.replace(/\.[^/.]+$/, ""),

View File

@ -1,12 +1,13 @@
import api from './routes/api/index.js'; import api from './server-node/routes/api/index.js';
import express from 'express'; import express from 'express';
const app = express(); const app = express();
const PORT = 8080; const PORT = 8080;
// Static Middleware // Static Middleware
app.use(express.static('./public')); app.use(express.static('./client-vanilla'));
app.use(express.static('./shared-operations'));
app.get('/', function (req, res, next) { // TODO: Use EJS? app.get('/', function (req, res, next) { // TODO: Use EJS?
res.render('home.ejs'); res.render('home.ejs');

View File

@ -3,20 +3,20 @@ import PDFJS from "pdfjs-dist";
import jsQR from "jsqr"; import jsQR from "jsqr";
delete global.crypto; // TODO: I hate to do this, but the new node version forces me to, if anyone finds a better solution, please tell me! delete global.crypto; // TODO: I hate to do this, but the new node version forces me to, if anyone finds a better solution, please tell me!
import * as pdfcpuWraopper from "./public/wasm/pdfcpu-wrapper-node.js"; import * as pdfcpuWraopper from "../shared-operations/wasm/pdfcpu/pdfcpu-wrapper-node.js";
import OpenCV from 'opencv-wasm'; import OpenCV from 'opencv-wasm';
import { extractPages as dependantExtractPages } from "./public/functions/extractPages.js"; import { extractPages as dependantExtractPages } from "../shared-operations/functions/extractPages.js";
import { impose as dependantImpose } from './public/functions/impose.js'; import { impose as dependantImpose } from '../shared-operations/functions/impose.js';
import { mergePDFs as dependantMergePDFs } from './public/functions/mergePDFs.js'; import { mergePDFs as dependantMergePDFs } from '../shared-operations/functions/mergePDFs.js';
import { rotatePages as dependantRotatePages } from './public/functions/rotatePages.js'; import { rotatePages as dependantRotatePages } from '../shared-operations/functions/rotatePages.js';
import { scaleContent as dependantScaleContent} from './public/functions/scaleContent.js'; import { scaleContent as dependantScaleContent} from '../shared-operations/functions/scaleContent.js';
import { scalePage as dependantScalePage } from './public/functions/scalePage.js'; import { scalePage as dependantScalePage } from '../shared-operations/functions/scalePage.js';
import { splitPDF as dependantSplitPDF } from './public/functions/splitPDF.js'; import { splitPDF as dependantSplitPDF } from '../shared-operations/functions/splitPDF.js';
import { editMetadata as dependantEditMetadata } from './public/functions/editMetadata.js'; import { editMetadata as dependantEditMetadata } from '../shared-operations/functions/editMetadata.js';
import { organizePages as dependantOrganizePages } from './public/functions/organizePages.js'; import { organizePages as dependantOrganizePages } from '../shared-operations/functions/organizePages.js';
import { removeBlankPages as dependantRemoveBlankPages} from './public/functions/removeBlankPages.js'; import { removeBlankPages as dependantRemoveBlankPages} from '../shared-operations/functions/removeBlankPages.js';
import { splitOn as dependantSplitOn } from "./public/functions/splitOn.js"; import { splitOn as dependantSplitOn } from "../shared-operations/functions/splitOn.js";
export async function extractPages(snapshot, pagesToExtractArray) { export async function extractPages(snapshot, pagesToExtractArray) {
return dependantExtractPages(snapshot, pagesToExtractArray, PDFLib); return dependantExtractPages(snapshot, pagesToExtractArray, PDFLib);

View File

@ -4,7 +4,7 @@ import stream from "stream";
import Archiver from 'archiver'; import Archiver from 'archiver';
import * as Functions from "../../functions.js"; import * as Functions from "../../functions.js";
import { traverseOperations } from "../../public/traverseOperations.js"; import { traverseOperations } from "../../../shared-operations/traverseOperations.js";
const activeWorkflows = {}; const activeWorkflows = {};
@ -17,7 +17,12 @@ router.post("/:workflowUuid?", [
return; return;
} }
req.files = Array.from(req.files.files); // Convert single values to array, keep arrays as is. if(Array.isArray(req.files.files)) {
req.files = req.files.files;
}
else {
req.files = [req.files.files];
}
const workflow = JSON.parse(req.body.workflow); const workflow = JSON.parse(req.body.workflow);
// TODO: Validate input further (json may fail or not be a valid workflow) // TODO: Validate input further (json may fail or not be a valid workflow)

View File

@ -75,10 +75,11 @@ export async function splitOn(snapshot, type, whiteThreashold, PDFJS, OpenCV, PD
const pagesWithQR = []; const pagesWithQR = [];
for (let i = 0; i < pdfDoc.numPages; i++) { for (let i = 0; i < pdfDoc.numPages; i++) {
console.log("Page:", i, "/", pdfDoc.numPages);
const page = await pdfDoc.getPage(i + 1); const page = await pdfDoc.getPage(i + 1);
const images = await getImagesOnPage(page, PDFJS); const images = await getImagesOnPage(page, PDFJS);
console.log("images:", images);
for (const image of images) { for (const image of images) {
const data = await checkForQROnImage(image); const data = await checkForQROnImage(image);
if(data == "https://github.com/Frooodle/Stirling-PDF") { if(data == "https://github.com/Frooodle/Stirling-PDF") {
@ -86,12 +87,16 @@ export async function splitOn(snapshot, type, whiteThreashold, PDFJS, OpenCV, PD
} }
} }
} }
if(pagesWithQR.length == 0) {
console.warn("Could not find any QR Codes in the provided PDF.")
}
return pagesWithQR; return pagesWithQR;
} }
async function checkForQROnImage(image) { async function checkForQROnImage(image) {
// TODO: There is an issue with the jsQR package (The package expects rgba but sometimes we have rgb), and the package seems to be stale, we could create a fork and fix the issue. In the meanwhile we just force rgba: // TODO: There is an issue with the jsQR package (The package expects rgba but sometimes we have rgb), and the package seems to be stale, we could create a fork and fix the issue. In the meanwhile we just force rgba:
// Check for rgb and convert to rgba // Check for rgb and convert to rgba
if(image.data.length == image.width * image.height * 3) { if(image.data.length == image.width * image.height * 3) {
const tmpArray = new Uint8ClampedArray(image.width * image.height * 4); const tmpArray = new Uint8ClampedArray(image.width * image.height * 4);

View File

@ -1,10 +1,11 @@
// TODO: Uses the BrowserFS import, needs to be changed for serverside // imports browserfs via index.html script-tag
let wasmLocation = "/wasm/"; let wasmLocation = "/wasm/pdfcpu/";
let fs; let fs;
let Buffer; let Buffer;
// TODO: This can later be defered to load asynchronously
configureFs(); configureFs();
loadWasm(); loadWasm();

View File

@ -1,5 +1,3 @@
// TODO: Uses the BrowserFS import, needs to be changed for serverside
import { WasmFs } from '@wasmer/wasmfs'; import { WasmFs } from '@wasmer/wasmfs';
import path from "path"; import path from "path";
@ -9,6 +7,7 @@ let nodeWasmLocation = "./public/wasm/";
let fs; let fs;
const wasmfs = new WasmFs(); const wasmfs = new WasmFs();
// TODO: This can later be defered to load asynchronously
(async () => { (async () => {
await loadWasm(); await loadWasm();
await configureFs(); await configureFs();