standardised the api response, updated contribute

This commit is contained in:
Saud Fatayerji 2023-10-27 01:33:11 +03:00
parent 99ca6b6a26
commit 29a8107ae0
3 changed files with 18 additions and 15 deletions

View File

@ -12,11 +12,13 @@ StirlingPDF aims to support as many types of operations as possible, including s
### Adding a shared (server + client) operation
1. Add the code for the operation to a new file in the [functions folder](/shared-operations/functions/).
> **NOTE:** all functions in these files use **dependency injection** (see existing functions for examples).
> **NOTE:** many of the functions in these files use **dependency injection** (see impose for an example).
>
> **Explanation:** Because the server and client import libraries in different ways, we import the library as needed in the wrapper module, then pass into the a operation function as a parameter.
> **Explanation:** Because some libraries need to be imported in different ways. We import the library as needed in the ```pdf-operations.js``` files, then pass the required library objects into the operation function as a parameter.
2. Now that we have the function code, we need to tell the other modules that it exists. Edit the [server operations](/server-node/public/pdf-operations.js) and the [client operations](/client-ionic/src/utils/pdf-operations.js) files to add your new operation! (Try to follow existing patterns where possible, keep the added operations in alphabetical order in the files).
2. Now that we have the function code, we need to tell the other modules that it exists. Edit the [server operations](/server-node/src/pdf-operations.js) and the [client operations](/client-ionic/src/utils/pdf-operations.ts) files to add your new operation! (Try to follow existing patterns where possible, keep the added operations in alphabetical order in the files).
3. If you added a wrapper function to the [client operations](/client-ionic/src/utils/pdf-operations.ts) file, you will also need to add the TypeScript declarations to the [declaration](/client-ionic/declarations/shared-operations.d.ts) file. See the other module declarations for examples.
### Adding a server only operation
> WIP

View File

@ -1,5 +1,6 @@
import { rotatePages } from '../../src/pdf-operations.js'
import { rotatePages } from '../../src/pdf-operations.js';
import { respondWithBinaryPdf } from '../../src/utils/endpoint-utils.js';
import express from 'express';
const router = express.Router();
@ -8,18 +9,9 @@ const upload = multer();
router.post('/rotate-pdf', upload.single("pdfFile"), async function(req, res, next) {
console.debug("rotating pdf:", req.file)
const rotated = await rotatePages(req.file.buffer, 90)
// add '_rotated' just before the file extension
const newFilename = req.file.originalname.replace(/(\.[^.]+)$/, '_rotated$1');
res.writeHead(200, {
'Content-Type': "application/pdf",
'Content-disposition': 'attachment;filename=' + newFilename,
'Content-Length': rotated.length
});
res.end(Buffer.from(rotated, 'binary'))
const newFilename = req.file.originalname.replace(/(\.[^.]+)$/, '_rotated$1'); // add '_rotated' just before the file extension
respondWithBinaryPdf(res, rotated, newFilename);
});
export default router;

View File

@ -0,0 +1,9 @@
export function respondWithBinaryPdf(res, buffer, filename) {
res.writeHead(200, {
'Content-Type': "application/pdf",
'Content-disposition': 'attachment;filename=' + filename,
'Content-Length': buffer.length
});
res.end(Buffer.from(buffer, 'binary'))
}