backup
This commit is contained in:
1 parent
44d0f561f6
commit
3357f89da3
13 files changed
+148
-37
No files matched your search
@@ -1,26 +1,79 @@
|
||||
const express = require("express");
|
||||
const router = express.Router();
|
||||
const fs = require("fs");
|
||||
const path = require("path");
|
||||
|
||||
const lastFilesDir = "./public/upload/converted/";
|
||||
|
||||
const rawDir = "./public/upload/raw/";
|
||||
const renderPageName = "invoice_sign";
|
||||
|
||||
const getMostRecentFile = (dir) => {
|
||||
const files = orderReccentFiles(dir);
|
||||
return files.length ? files[0] : undefined;
|
||||
};
|
||||
|
||||
const orderReccentFiles = (dir) => {
|
||||
return fs
|
||||
.readdirSync(dir)
|
||||
.filter((file) => fs.lstatSync(path.join(dir, file)).isFile())
|
||||
.map((file) => ({ file, mtime: fs.lstatSync(path.join(dir, file)).mtime }))
|
||||
.sort((a, b) => b.mtime.getTime() - a.mtime.getTime());
|
||||
};
|
||||
|
||||
const { PDFDocument } = require("pdf-lib");
|
||||
|
||||
const signDir = "./public/img/imza.png";
|
||||
const signPDF = async (sourceFile) => {
|
||||
// Fetch PNG image
|
||||
const pngUrl = signDir;
|
||||
const pngImageBytes = fs.readFileSync(signDir);
|
||||
const pBytes = fs.readFileSync(rawDir + sourceFile);
|
||||
const pdfDoc = await PDFDocument.load(pBytes);
|
||||
|
||||
// Embed the JPG image bytes and PNG image bytes
|
||||
const pngImage = await pdfDoc.embedPng(pngImageBytes);
|
||||
|
||||
// Get the width/height of the PNG image scaled down to 50% of its original size
|
||||
const pngDims = pngImage.scale(0.1);
|
||||
|
||||
// Add a blank page to the document
|
||||
const pages = pdfDoc.getPages();
|
||||
|
||||
// Draw the PNG image near the lower right corner of the JPG image
|
||||
page = pages[0];
|
||||
page.drawImage(pngImage, {
|
||||
x: page.getWidth() / 2 - pngDims.width / 2 + 75,
|
||||
y: page.getHeight() / 2 - pngDims.height,
|
||||
width: pngDims.width,
|
||||
height: pngDims.height,
|
||||
});
|
||||
|
||||
// Serialize the PDFDocument to bytes (a Uint8Array)
|
||||
const pdfBytes = await pdfDoc.save();
|
||||
fs.writeFileSync(lastFilesDir + sourceFile, await pdfDoc.save());
|
||||
return pdfBytes;
|
||||
};
|
||||
|
||||
// load the page
|
||||
router.get("/", function (req, res) {
|
||||
let lastFiles = [];
|
||||
if (!fs.existsSync(lastFilesDir)) {
|
||||
fs.mkdirSync(lastFilesDir);
|
||||
}
|
||||
const dir = fs.opendirSync(lastFilesDir);
|
||||
let entity;
|
||||
while ((entity = dir.readSync()) !== null) {
|
||||
if (entity.isFile()) {
|
||||
lastFiles.push({ type: "f", name: entity.name });
|
||||
} else if (entity.isDirectory()) {
|
||||
lastFiles.push({ type: "d", name: entity.name });
|
||||
// const dir = fs.opendirSync(lastFilesDir);
|
||||
let filesOrdered = orderReccentFiles(lastFilesDir);
|
||||
|
||||
if (filesOrdered.length > 5) {
|
||||
for (let index = 5; index < filesOrdered.length; index++) {
|
||||
filePath = lastFilesDir + filesOrdered[index].file;
|
||||
fs.unlinkSync(filePath);
|
||||
}
|
||||
}
|
||||
filesOrdered = orderReccentFiles(lastFilesDir);
|
||||
for (file of filesOrdered) {
|
||||
lastFiles.push({ type: "f", name: file.file });
|
||||
}
|
||||
// lastFiles.push({ type: "f", name: getMostRecentFile(lastFilesDir).file });
|
||||
res.render(renderPageName, { lastFiles });
|
||||
});
|
||||
//Post the upload file
|
||||
@@ -35,7 +88,7 @@ router.post("/", function (req, res) {
|
||||
console.log(uploadedFile);
|
||||
|
||||
// Upload path
|
||||
const uploadPath = lastFilesDir;
|
||||
const uploadPath = rawDir;
|
||||
if (!fs.existsSync(uploadPath)) {
|
||||
fs.mkdirSync(uploadPath);
|
||||
}
|
||||
@@ -44,20 +97,18 @@ router.post("/", function (req, res) {
|
||||
if (err) {
|
||||
console.log(err);
|
||||
res.send("Failed !!");
|
||||
} else res.send("Successfully Uploaded !!");
|
||||
}
|
||||
// res.send("Successfully Uploaded !!");
|
||||
else {
|
||||
signPDF(uploadedFile.name);
|
||||
res.redirect(renderPageName);
|
||||
}
|
||||
});
|
||||
} else res.send("No file uploaded !!");
|
||||
});
|
||||
|
||||
// To handle the download file request
|
||||
router.get("/download", function (req, res) {
|
||||
// The res.download() talking file path to be downloaded
|
||||
// console.log(req);
|
||||
// res.download(__dirname + "/uploads/fax.odg", function (err) {
|
||||
// if (err) {
|
||||
// console.log(err);
|
||||
// }
|
||||
// });
|
||||
const downloadFile = lastFilesDir + req.query.downloadFile;
|
||||
console.log(downloadFile);
|
||||
res.download(downloadFile, function (err) {});
|
||||
|
||||
Reference in new issue
Block a user