backup, going for multer

master
Selcuk Sari 4 years ago
parent 3357f89da3
commit 6215aa15a5

Binary file not shown.

Before

Width:  |  Height:  |  Size: 431 KiB

After

Width:  |  Height:  |  Size: 418 KiB

@ -6,11 +6,8 @@ const path = require("path");
const lastFilesDir = "./public/upload/converted/"; const lastFilesDir = "./public/upload/converted/";
const rawDir = "./public/upload/raw/"; const rawDir = "./public/upload/raw/";
const renderPageName = "invoice_sign"; const renderPageName = "invoice_sign";
const signDir = "./public/img/imza.png";
const getMostRecentFile = (dir) => { const stampDir = "./public/img/kase.png";
const files = orderReccentFiles(dir);
return files.length ? files[0] : undefined;
};
const orderReccentFiles = (dir) => { const orderReccentFiles = (dir) => {
return fs return fs
@ -22,34 +19,45 @@ const orderReccentFiles = (dir) => {
const { PDFDocument } = require("pdf-lib"); const { PDFDocument } = require("pdf-lib");
const signDir = "./public/img/imza.png";
const signPDF = async (sourceFile) => { const signPDF = async (sourceFile) => {
// Fetch PNG image // Fetch PNG image
const pngUrl = signDir; const pngUrl = signDir;
const pngImageBytes = fs.readFileSync(signDir); const pngSignBytes = fs.readFileSync(signDir);
const pngStampBytes = fs.readFileSync(stampDir);
const pBytes = fs.readFileSync(rawDir + sourceFile); const pBytes = fs.readFileSync(rawDir + sourceFile);
const pdfDoc = await PDFDocument.load(pBytes); const pdfDoc = await PDFDocument.load(pBytes);
// Embed the JPG image bytes and PNG image bytes // Embed the JPG image bytes and PNG image bytes
const pngImage = await pdfDoc.embedPng(pngImageBytes); const pngSign = await pdfDoc.embedPng(pngSignBytes);
const pngStamp = await pdfDoc.embedPng(pngStampBytes);
// Get the width/height of the PNG image scaled down to 50% of its original size // Get the width/height of the PNG image scaled down to 50% of its original size
const pngDims = pngImage.scale(0.1); const pngSignDims = pngSign.scale(0.1);
const pngStampDims = pngStamp.scale(0.1);
// Add a blank page to the document // Add a blank page to the document
const pages = pdfDoc.getPages(); const pages = pdfDoc.getPages();
// Draw the PNG image near the lower right corner of the JPG image // Draw the PNG image near the lower right corner of the JPG image
page = pages[0]; page = pages[0];
page.drawImage(pngImage, { page.drawImage(pngStamp, {
x: page.getWidth() / 2 - pngDims.width / 2 + 75, x: page.getWidth() / 2 - pngStampDims.width / 2 + 75,
y: page.getHeight() / 2 - pngDims.height, y: page.getHeight() / 2 - pngStampDims.height,
width: pngDims.width, width: pngStampDims.width,
height: pngDims.height, height: pngStampDims.height,
});
page.drawImage(pngSign, {
x: page.getWidth() / 2 - pngSignDims.width / 2 + 75,
y: page.getHeight() / 2 - pngSignDims.height,
width: pngSignDims.width,
height: pngSignDims.height,
}); });
// Serialize the PDFDocument to bytes (a Uint8Array) // Serialize the PDFDocument to bytes (a Uint8Array)
const pdfBytes = await pdfDoc.save(); const pdfBytes = await pdfDoc.save();
if (!fs.existsSync(lastFilesDir)) {
fs.mkdirSync(lastFilesDir, { recursive: true });
}
fs.writeFileSync(lastFilesDir + sourceFile, await pdfDoc.save()); fs.writeFileSync(lastFilesDir + sourceFile, await pdfDoc.save());
return pdfBytes; return pdfBytes;
}; };
@ -58,7 +66,7 @@ const signPDF = async (sourceFile) => {
router.get("/", function (req, res) { router.get("/", function (req, res) {
let lastFiles = []; let lastFiles = [];
if (!fs.existsSync(lastFilesDir)) { if (!fs.existsSync(lastFilesDir)) {
fs.mkdirSync(lastFilesDir); fs.mkdirSync(lastFilesDir, { recursive: true });
} }
// const dir = fs.opendirSync(lastFilesDir); // const dir = fs.opendirSync(lastFilesDir);
let filesOrdered = orderReccentFiles(lastFilesDir); let filesOrdered = orderReccentFiles(lastFilesDir);
@ -90,7 +98,7 @@ router.post("/", function (req, res) {
// Upload path // Upload path
const uploadPath = rawDir; const uploadPath = rawDir;
if (!fs.existsSync(uploadPath)) { if (!fs.existsSync(uploadPath)) {
fs.mkdirSync(uploadPath); fs.mkdirSync(uploadPath, { recursive: true });
} }
// To save the file using mv() function // To save the file using mv() function
uploadedFile.mv(uploadPath + uploadedFile.name, function (err) { uploadedFile.mv(uploadPath + uploadedFile.name, function (err) {
@ -102,6 +110,7 @@ router.post("/", function (req, res) {
else { else {
signPDF(uploadedFile.name); signPDF(uploadedFile.name);
res.redirect(renderPageName); res.redirect(renderPageName);
fs.unlinkSync(uploadPath + uploadedFile.name);
} }
}); });
} else res.send("No file uploaded !!"); } else res.send("No file uploaded !!");
@ -111,6 +120,10 @@ router.post("/", function (req, res) {
router.get("/download", function (req, res) { router.get("/download", function (req, res) {
const downloadFile = lastFilesDir + req.query.downloadFile; const downloadFile = lastFilesDir + req.query.downloadFile;
console.log(downloadFile); console.log(downloadFile);
res.setHeader(
"Content-Disposition",
"attachment; filename=" + req.query.downloadFile
);
res.download(downloadFile, function (err) {}); res.download(downloadFile, function (err) {});
}); });
module.exports = router; module.exports = router;

Loading…
Cancel
Save