Compare commits

...

2 Commits

Author SHA1 Message Date
radex 3ef7a1f1a1
Add copies parameter 2024-02-01 17:59:37 +01:00
radex 8d66083f18
Fix body limit 2024-02-01 17:48:35 +01:00
1 changed files with 11 additions and 7 deletions

View File

@ -61,7 +61,8 @@ app.get('/', (req, res) => {
- Prints IPP jobs
- POST /print?printer=:name (body: application/pdf)
- Prints a PDF
- :name - printer name or alias (case insensitive)
- ?printer= printer name or alias (case insensitive)
- ?copies= (optional) number of copies (1-10 allowed)
- Response: 200 OK if sent to printer successfully (not necessarily printed), 4xx/5xx otherwise
</pre>`)
})
@ -106,12 +107,12 @@ app.get('/printers/:name/jobs', (req, res) => {
// --- Print API ---
app.post('/print', bodyParser.raw({ type: '*/*' }), (req, res) => {
app.post('/print', bodyParser.raw({ type: '*/*', limit: 10_000_000 }), (req, res) => {
const { body, query } = req
console.log("Received print job: ", { body, query })
// validate query params
const { printer: printerName, ...otherParams } = query
const { printer: printerName, copies: copiesParam, ...otherParams } = query
if (!printerName) {
res.status(400).send("No printer specified, pass ?printer=:name in the query params")
return
@ -134,8 +135,12 @@ app.post('/print', bodyParser.raw({ type: '*/*' }), (req, res) => {
} else if (body.subarray(0, 4).toString() !== "%PDF") {
res.status(415).send("Data does not look like a PDF, please send a PDF")
return
} else if (body.length > 10_000_000) {
res.status(413).send("Data too large (limit: 10MB), please send a smaller PDF")
}
// validate job attributes
const copies = Number(copiesParam) || 1
if (!(copies >= 1 && copies <= 10)) {
res.status(400).send("Copies must be between 1 and 10")
return
}
@ -145,8 +150,7 @@ app.post('/print', bodyParser.raw({ type: '*/*' }), (req, res) => {
"document-format": "application/pdf",
},
"job-attributes-tag": {
// FIXME: This should work, but does not
// "copies": 2,
"copies": copies,
},
data: body
};