Skip to content
Open
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions api/main_endpoints/routes/Printer.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ const fs = require('fs');
const path = require('path');
const { MetricsHandler, register } = require('../../util/metrics.js');
const { cleanUpChunks, cleanUpExpiredChunks, recordPrintingFolderSize } = require('../util/Printer.js');
const { PDFDocument, StandardFonts } = require('pdf-lib');
const {subtractUserPages} = require('../util/userHelpers');

const {
decodeToken,
Expand Down Expand Up @@ -113,16 +115,25 @@ router.post('/sendPrintRequest', upload.single('chunk'), async (req, res) => {
return res.sendStatus(SERVER_ERROR);
}
}

const stream = await fs.createReadStream(assembledPdfFromChunks);
const stream = await fs.promises.readFile(assembledPdfFromChunks); // Buffer
const data = new FormData();
data.append('file', stream, {filename: id, type: 'application/pdf'});
data.append('copies', copies);
data.append('sides', sides);

try {
const pdfDoc = await PDFDocument.load(stream); // load PDF
const numpages = pdfDoc.getPages().length; // get number of pages
const copiesInt = parseInt(copies || 1, 10);
const totalPages = numpages * copiesInt; // updates users printcount
const pagesRemaining = await subtractUserPages(user.id, totalPages);
if (pagesRemaining === null) {
await cleanUpChunks(dir, id);
return res.status(400).json({ error: 'Page limit exceeded or user not found' });
}
// full pdf can be sent to quasar no problem
const printRes = await axios.post(PRINTER_URL + '/print', data, {

headers: {
...data.getHeaders(),
},
Expand Down
21 changes: 21 additions & 0 deletions api/main_endpoints/util/userHelpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,26 @@ function checkIfPageCountResets(lastLogin) {

return lastLoginWasOverOneWeekAgo || aSundayHasPassedSinceLastLogin;
}
// updates users available pages
async function subtractUserPages(userId, pagesToPrint){
const user = await User.findById(userId);
if(!user){
logger.error(`User not found ID: ${userId}`);
return null; // checks user
}
if(!Number.isInteger(pagesToPrint) || pagesToPrint <= 0){
logger.error(`Invalid pagesToPrint value: ${pagesToPrint}.`);
return null;
}
if (user.pagesPrinted < pagesToPrint) {
logger.error(`User has insufficient pages remaining. Requested: ${pagesToPrint}. Available:${user.pagesPrinted}`);
return null;
}

user.pagesPrinted -= pagesToPrint;
await user.save();
return user.pagesPrinted;
}

module.exports = {
registerUser,
Expand All @@ -211,4 +231,5 @@ module.exports = {
userWithEmailExists,
checkIfPageCountResets,
findPasswordReset,
subtractUserPages,
};
Loading