Papermark is the open-source DocSend alternative and secure data rooms with built-in analytics and custom domains.
feat: add contentType and originalFile to document and version
Marc Seitz0f33e7a09f29
diff --git a/components/documents/add-document-modal.tsx b/components/documents/add-document-modal.tsxindex e326007b..8557fbe0 100644--- a/components/documents/add-document-modal.tsx+++ b/components/documents/add-document-modal.tsx@@ -72,12 +72,13 @@ export function AddDocumentModal({try {setUploading(true);- const contentType = getSupportedContentType(currentFile.type);+ const contentType = currentFile.type;+ const supportedFileType = getSupportedContentType(contentType);- if (!contentType) {+ if (!supportedFileType) {setUploading(false);toast.error(- "Unsupported file format. Please upload a PDF or Excel file.",+ "Unsupported file format. Please upload a PDF, Powerpoint, Excel, or Word file.",);return;}@@ -92,6 +93,7 @@ export function AddDocumentModal({key: data!,storageType: type!,contentType: contentType,+ supportedFileType: supportedFileType,};let response: Response | undefined;// create a document or new version in the databasediff --git a/components/upload-zone.tsx b/components/upload-zone.tsxindex 52755f1f..56d84bf1 100644--- a/components/upload-zone.tsx+++ b/components/upload-zone.tsx@@ -9,7 +9,7 @@ import { FileRejection, useDropzone } from "react-dropzone";import { mutate } from "swr";import { useAnalytics } from "@/lib/analytics";-import { createDocument } from "@/lib/documents/create-document";+import { DocumentData, createDocument } from "@/lib/documents/create-document";import { resumableUpload } from "@/lib/files/tus-upload";import { usePlan } from "@/lib/swr/use-billing";import { CustomUser } from "@/lib/types";@@ -135,12 +135,12 @@ export default function UploadZone({const uploadResult = await complete;- const documentData = {+ const documentData: DocumentData = {key: uploadResult.id,- contentType: getSupportedContentType(uploadResult.fileType)!,+ supportedFileType: getSupportedContentType(uploadResult.fileType)!,name: file.name,storageType: DocumentStorageType.S3_PATH,- numPages: uploadResult.numPages,+ contentType: uploadResult.fileType,};const response = await createDocument({documentData,@@ -206,6 +206,7 @@ export default function UploadZone({numPages: document.numPages,path: router.asPath,type: document.type,+ contentType: document.contentType,teamId: teamInfo?.currentTeam?.id,bulkupload: true,dataroomId: dataroomId,diff --git a/components/welcome/upload.tsx b/components/welcome/upload.tsxindex b3edb95e..963cc72b 100644--- a/components/welcome/upload.tsx+++ b/components/welcome/upload.tsx@@ -61,12 +61,13 @@ export default function Upload() {try {setUploading(true);- const contentType = getSupportedContentType(currentFile.type);+ const contentType = currentFile.type;+ const supportedFileType = getSupportedContentType(currentFile.type);- if (!contentType) {+ if (!supportedFileType) {setUploading(false);toast.error(- "Unsupported file format. Please upload a PDF or Excel file.",+ "Unsupported file format. Please upload a PDF, Powerpoint, Excel, or Word file.",);return;}@@ -84,6 +85,7 @@ export default function Upload() {key: data!,storageType: type!,contentType: contentType,+ supportedFileType: supportedFileType,};// create a document in the databaseconst response = await createDocument({ documentData, teamId, numPages });@@ -100,6 +102,7 @@ export default function Upload() {numPages: document.numPages,path: router.asPath,type: document.type,+ contentType: document.contentType,teamId: teamInfo?.currentTeam?.id,});analytics.capture("Link Added", {diff --git a/lib/documents/create-document.ts b/lib/documents/create-document.tsindex 022e45df..1c45753a 100644--- a/lib/documents/create-document.ts+++ b/lib/documents/create-document.ts@@ -4,7 +4,8 @@ export type DocumentData = {name: string;key: string;storageType: DocumentStorageType;- contentType: string; // papermark types: "pdf", "sheet"+ contentType: string; // actual file mime type+ supportedFileType: string; // papermark types: "pdf", "sheet", "docs", "slides"};export const createDocument = async ({@@ -30,7 +31,8 @@ export const createDocument = async ({storageType: documentData.storageType,numPages: numPages,folderPathName: folderPathName,- type: documentData.contentType,+ type: documentData.supportedFileType,+ contentType: documentData.contentType,}),});@@ -64,7 +66,8 @@ export const createAgreementDocument = async ({storageType: documentData.storageType,numPages: numPages,folderPathName: folderPathName,- type: documentData.contentType,+ type: documentData.supportedFileType,+ contentType: documentData.contentType,}),});@@ -98,7 +101,8 @@ export const createNewDocumentVersion = async ({url: documentData.key,storageType: documentData.storageType,numPages: numPages,- type: documentData.contentType,+ type: documentData.supportedFileType,+ contentType: documentData.contentType,}),},);diff --git a/lib/files/put-file.ts b/lib/files/put-file.tsindex cf82238f..8d937dcb 100644--- a/lib/files/put-file.ts+++ b/lib/files/put-file.ts@@ -8,13 +8,7 @@ import {getSheetsCount,} from "@/lib/utils/get-page-number-count";-import { SUPPORTED_DOCUMENT_TYPES } from "../constants";--// type File = {-// name: string;-// type: string;-// arrayBuffer: () => Promise<ArrayBuffer>;-// };+import { SUPPORTED_DOCUMENT_MIME_TYPES } from "../constants";export const putFile = async ({file,@@ -125,13 +119,13 @@ const putFileInS3 = async ({numPages = await getPagesCount(body);}// get sheet count for excel files- else if (- SUPPORTED_DOCUMENT_TYPES.includes(file.type) &&- file.type !== "application/pdf"- ) {- const body = await file.arrayBuffer();- numPages = getSheetsCount(body);- }+ // else if (+ // SUPPORTED_DOCUMENT_MIME_TYPES.includes(file.type) &&+ // file.type !== "application/pdf"+ // ) {+ // const body = await file.arrayBuffer();+ // numPages = getSheetsCount(body);+ // }return {type: DocumentStorageType.S3_PATH,diff --git a/pages/api/teams/[teamId]/documents/[id]/versions/index.ts b/pages/api/teams/[teamId]/documents/[id]/versions/index.tsindex 85835752..ad88ffff 100644--- a/pages/api/teams/[teamId]/documents/[id]/versions/index.ts+++ b/pages/api/teams/[teamId]/documents/[id]/versions/index.ts@@ -8,6 +8,7 @@ import { getServerSession } from "next-auth/next";import { copyFileToBucketServer } from "@/lib/files/copy-file-to-bucket-server";import prisma from "@/lib/prisma";import { getTeamWithUsersAndDocument } from "@/lib/team/helper";+import { convertFilesToPdfTask } from "@/lib/trigger/convert-files";import { CustomUser } from "@/lib/types";import { log } from "@/lib/utils";@@ -27,11 +28,12 @@ export default async function handle(teamId: string;id: string;};- const { url, type, numPages, storageType } = req.body as {+ const { url, type, numPages, storageType, contentType } = req.body as {url: string;type: string;numPages: number;storageType: DocumentStorageType;+ contentType: string;};const userId = (session.user as CustomUser).id;@@ -63,11 +65,24 @@ export default async function handle(data: {documentId: documentId,file: url,+ originalFile: url,type: type,storageType,numPages: document?.advancedExcelEnabled ? 1 : numPages,isPrimary: true,versionNumber: currentVersionNumber + 1,+ contentType,+ },+ });++ // turn off isPrimary flag for all other versions+ await prisma.documentVersion.updateMany({+ where: {+ documentId: documentId,+ id: { not: version.id },+ },+ data: {+ isPrimary: false,},});diff --git a/pages/api/teams/[teamId]/documents/index.ts b/pages/api/teams/[teamId]/documents/index.tsindex b721bebc..da9427af 100644--- a/pages/api/teams/[teamId]/documents/index.ts+++ b/pages/api/teams/[teamId]/documents/index.ts@@ -1,5 +1,6 @@import { NextApiRequest, NextApiResponse } from "next";+import { authOptions } from "@/pages/api/auth/[...nextauth]";import { client } from "@/trigger";import { DocumentStorageType } from "@prisma/client";import { getServerSession } from "next-auth/next";@@ -9,11 +10,10 @@ import { errorhandler } from "@/lib/errorHandler";import notion from "@/lib/notion";import prisma from "@/lib/prisma";import { getTeamWithUsersAndDocument } from "@/lib/team/helper";+import { convertFilesToPdfTask } from "@/lib/trigger/convert-files";import { CustomUser } from "@/lib/types";import { getExtension, log } from "@/lib/utils";-import { authOptions } from "../../../auth/[...nextauth]";-export default async function handle(req: NextApiRequest,res: NextApiResponse,@@ -78,6 +78,7 @@ export default async function handle(numPages,type: fileType,folderPathName,+ contentType,} = req.body as {name: string;url: string;@@ -85,6 +86,7 @@ export default async function handle(numPages?: number;type?: string;folderPathName?: string;+ contentType: string;};try {@@ -127,6 +129,8 @@ export default async function handle(name: name,numPages: numPages,file: fileUrl,+ originalFile: fileUrl,+ contentType: contentType,type: type,storageType,ownerId: (session.user as CustomUser).id,@@ -137,6 +141,8 @@ export default async function handle(versions: {create: {file: fileUrl,+ originalFile: fileUrl,+ contentType: contentType,type: type,storageType,numPages: numPages,