feat: document file conversion

This commit is contained in:
Ephraim Atta-Duncan
2025-12-15 11:58:42 +00:00
parent 43486d8448
commit 7a499270be
26 changed files with 252 additions and 63 deletions
@@ -47,9 +47,11 @@ export const createDocumentRoute = authenticatedProcedure
}
const { id: documentDataId } = await putNormalizedPdfFileServerSide({
name: file.name,
type: 'application/pdf',
arrayBuffer: async () => Promise.resolve(pdf),
file: {
name: file.name,
type: 'application/pdf',
arrayBuffer: async () => Promise.resolve(pdf),
},
});
ctx.logger.info({
@@ -18,6 +18,7 @@ export const ZGetMultiSignDocumentResponseSchema = ZDocumentLiteSchema.extend({
id: true,
data: true,
initialData: true,
originalMimeType: true,
}),
documentMeta: DocumentMetaSchema.pick({
signingOrder: true,
@@ -87,7 +87,7 @@ export const createEnvelopeItemsRoute = authenticatedProcedure
// For each file, stream to s3 and create the document data.
const envelopeItems = await Promise.all(
files.map(async (file) => {
const { id: documentDataId } = await putNormalizedPdfFileServerSide(file);
const { id: documentDataId } = await putNormalizedPdfFileServerSide({ file });
return {
title: file.name,
@@ -1,6 +1,7 @@
import { getServerLimits } from '@documenso/ee/server-only/limits/server';
import { AppError, AppErrorCode } from '@documenso/lib/errors/app-error';
import { createEnvelope } from '@documenso/lib/server-only/envelope/create-envelope';
import { convertToPdfIfNeeded } from '@documenso/lib/server-only/file-conversion/convert-to-pdf';
import { putNormalizedPdfFileServerSide } from '@documenso/lib/universal/upload/put-file.server';
import { insertFormValuesInPdf } from '../../../lib/server-only/pdf/insert-form-values-in-pdf';
@@ -59,17 +60,13 @@ export const createEnvelopeRoute = authenticatedProcedure
});
}
if (files.some((file) => !file.type.startsWith('application/pdf'))) {
throw new AppError('INVALID_DOCUMENT_FILE', {
message: 'You cannot upload non-PDF files',
statusCode: 400,
});
}
// For each file, stream to s3 and create the document data.
// For each file, convert to PDF if needed, then store.
const envelopeItems = await Promise.all(
files.map(async (file) => {
let pdf = Buffer.from(await file.arrayBuffer());
// Convert to PDF if needed (DOCX, images, etc)
const { pdfBuffer, originalMimeType } = await convertToPdfIfNeeded(file);
let pdf = pdfBuffer;
if (formValues) {
// eslint-disable-next-line require-atomic-updates
@@ -80,9 +77,12 @@ export const createEnvelopeRoute = authenticatedProcedure
}
const { id: documentDataId } = await putNormalizedPdfFileServerSide({
name: file.name,
type: 'application/pdf',
arrayBuffer: async () => Promise.resolve(pdf),
file: {
name: file.name,
type: 'application/pdf',
arrayBuffer: async () => Promise.resolve(pdf),
},
originalMimeType,
});
return {
@@ -19,6 +19,7 @@ export const ZGetEnvelopeItemsResponseSchema = z.object({
id: true,
data: true,
initialData: true,
originalMimeType: true,
}),
})
.array(),
@@ -79,7 +79,7 @@ export const useEnvelopeRoute = authenticatedProcedure
// Process uploaded files and create document data for them
const uploadedFiles = await Promise.all(
filesToUpload.map(async (file) => {
const { id: documentDataId } = await putNormalizedPdfFileServerSide(file);
const { id: documentDataId } = await putNormalizedPdfFileServerSide({ file });
return {
name: file.name,
@@ -191,7 +191,7 @@ export const templateRouter = router({
attachments,
} = payload;
const { id: templateDocumentDataId } = await putNormalizedPdfFileServerSide(file);
const { id: templateDocumentDataId } = await putNormalizedPdfFileServerSide({ file });
ctx.logger.info({
input: {