Skip to content
Open
Show file tree
Hide file tree
Changes from 9 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
18 changes: 16 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 @@ -78,7 +80,8 @@ router.post('/sendPrintRequest', upload.single('chunk'), async (req, res) => {
logger.warn('/sendPrintRequest was requested without a token');
return res.sendStatus(UNAUTHORIZED);
}
if (!await decodeToken(req)) {
const user = await decodeToken(req);
if(!user){
logger.warn('/sendPrintRequest was requested with an invalid token');
return res.sendStatus(UNAUTHORIZED);
}
Expand Down Expand Up @@ -114,7 +117,18 @@ router.post('/sendPrintRequest', upload.single('chunk'), async (req, res) => {
}
}

const stream = await fs.createReadStream(assembledPdfFromChunks);
try{
const stream = await fs.promises.readFile(assembledPdfFromChunks); // buffer
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;
await subtractUserPages(user.id, totalPages); // updates users printcount
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

move this code in the existing try block in axios.post(PRINTER_URL ...

}catch(err){
logger.error('/sendPrintRequest failed', err); // helper increments totalapges, thrown if exceeded
await cleanUpChunks(dir, id);
return res.status(400).json({error:err.message});
}
const data = new FormData();
data.append('file', stream, {filename: id, type: 'application/pdf'});
data.append('copies', copies);
Expand Down
18 changes: 18 additions & 0 deletions api/main_endpoints/util/userHelpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,23 @@ function checkIfPageCountResets(lastLogin) {

return lastLoginWasOverOneWeekAgo || aSundayHasPassedSinceLastLogin;
}
// updates users available pages
async function subtractUserPages(userId, pagesToPrint){
const user = await User.findById(userId);
if(!user){
throw new Error('user not found'); // checks user
}
if(!Number.isInteger(pagesToPrint) || pagesToPrint <= 0){
throw new Error('invalid number');
}
if (user.pagesPrinted < pagesToPrint) {
throw new Error('no pages remaining');
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of throwing an error just do logger.error here and return early, we dont have to return anything


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

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