2023-11-11 18:35:33 +03:00
|
|
|
|
2023-11-12 16:57:53 +03:00
|
|
|
import { Response } from 'express';
|
2023-11-13 00:09:12 +01:00
|
|
|
import { PdfFile } from '@stirling-pdf/shared-operations/src/wrappers/PdfFile'
|
2023-11-11 18:35:33 +03:00
|
|
|
|
2023-11-13 02:46:50 +03:00
|
|
|
export async function respondWithFile(res: Response, bytes: Uint8Array, name: string, mimeType: string): Promise<void> {
|
2023-11-11 18:35:33 +03:00
|
|
|
res.writeHead(200, {
|
2023-11-13 02:46:50 +03:00
|
|
|
'Content-Type': mimeType,
|
|
|
|
|
'Content-disposition': 'attachment;filename=' + name,
|
|
|
|
|
'Content-Length': bytes.length
|
2023-11-11 18:35:33 +03:00
|
|
|
});
|
2023-11-13 02:46:50 +03:00
|
|
|
res.end(bytes);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function respondWithPdfFile(res: Response, file: PdfFile): Promise<void> {
|
2023-11-15 02:27:21 +03:00
|
|
|
const byteArray = await file.uint8Array;
|
|
|
|
|
respondWithFile(res, byteArray, file.filename, "application/pdf");
|
2023-11-11 18:35:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function response_mustHaveExactlyOneFile(res: Response): void {
|
|
|
|
|
res.status(400).send([
|
|
|
|
|
{
|
|
|
|
|
"message": "file is required",
|
|
|
|
|
"path": [
|
|
|
|
|
"pdfFile"
|
|
|
|
|
],
|
|
|
|
|
"type": "file",
|
|
|
|
|
"context": {
|
|
|
|
|
"label": "pdfFile",
|
|
|
|
|
"key": "pdfFile"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
]);
|
|
|
|
|
}
|
2023-11-13 02:46:50 +03:00
|
|
|
|
|
|
|
|
export function response_dependencyNotConfigured(res: Response, dependencyName: string): void {
|
|
|
|
|
res.status(400).send([
|
|
|
|
|
{
|
|
|
|
|
"message": `${dependencyName} is not configured correctly on the server.`,
|
|
|
|
|
"type": "dependency_error",
|
|
|
|
|
}
|
|
|
|
|
]);
|
|
|
|
|
}
|