fix: normalize pdf on the server

This commit is contained in:
Mythie
2024-12-18 08:14:14 +11:00
parent c161553d1d
commit 390a317bd3
5 changed files with 51 additions and 8 deletions

View File

@ -8,7 +8,6 @@ import { DocumentDataType } from '@documenso/prisma/client';
import { AppError } from '../../errors/app-error';
import { createDocumentData } from '../../server-only/document-data/create-document-data';
import { removeOptionalContentGroups } from '../../server-only/pdf/flatten-form';
type File = {
name: string;
@ -25,7 +24,9 @@ export const putPdfFile = async (file: File) => {
() => false,
);
const pdf = await PDFDocument.load(await file.arrayBuffer()).catch((e) => {
const arrayBuffer = await file.arrayBuffer();
const pdf = await PDFDocument.load(arrayBuffer).catch((e) => {
console.error(`PDF upload parse error: ${e.message}`);
throw new AppError('INVALID_DOCUMENT_FILE');
@ -39,11 +40,7 @@ export const putPdfFile = async (file: File) => {
file.name = `${file.name}.pdf`;
}
removeOptionalContentGroups(pdf);
const bytes = await pdf.save();
const { type, data } = await putFile(new File([bytes], file.name, { type: 'application/pdf' }));
const { type, data } = await putFile(file);
return await createDocumentData({ type, data });
};