Papermark is the open-source DocSend alternative and secure data rooms with built-in analytics and custom domains.
feat: add excel support in uploader
Marc Seitzb978b3ebe075
diff --git a/components/document-upload.tsx b/components/document-upload.tsxindex ef55e876..bf4e6d41 100644--- a/components/document-upload.tsx+++ b/components/document-upload.tsx@@ -52,6 +52,8 @@ export default function DocumentUpload({"application/pdf": [], // ".pdf""application/vnd.ms-excel": [], // ".xls""application/vnd.openxmlformats-officedocument.spreadsheetml.sheet": [], // ".xlsx"+ "text/csv": [], // ".csv"+ "application/vnd.oasis.opendocument.spreadsheet": [], // ".ods"},multiple: false,maxSize: maxSize * 1024 * 1024, // 30 MBdiff --git a/components/documents/add-document-modal.tsx b/components/documents/add-document-modal.tsxindex c37a62b5..3027938e 100644--- a/components/documents/add-document-modal.tsx+++ b/components/documents/add-document-modal.tsx@@ -29,6 +29,7 @@ import {} from "@/lib/documents/create-document";import { putFile } from "@/lib/files/put-file";import { copyToClipboard } from "@/lib/utils";+import { getSupportedContentType } from "@/lib/utils/get-content-type";export function AddDocumentModal({newVersion,@@ -69,6 +70,16 @@ export function AddDocumentModal({try {setUploading(true);+ const contentType = getSupportedContentType(currentFile.type);++ if (!contentType) {+ setUploading(false);+ toast.error(+ "Unsupported file format. Please upload a PDF or Excel file.",+ );+ return;+ }+const { type, data, numPages } = await putFile({file: currentFile,teamId,@@ -78,6 +89,7 @@ export function AddDocumentModal({name: currentFile.name,key: data!,storageType: type!,+ contentType: contentType,};let response: Response | undefined;// create a document or new version in the database@@ -115,7 +127,7 @@ export function AddDocumentModal({name: document.name,numPages: document.numPages,path: router.asPath,- type: "pdf",+ type: document.type,teamId: teamId,dataroomId: dataroomId,});@@ -137,7 +149,7 @@ export function AddDocumentModal({name: document.name,numPages: document.numPages,path: router.asPath,- type: "pdf",+ type: document.type,teamId: teamId,});analytics.capture("Link Added", {@@ -157,7 +169,7 @@ export function AddDocumentModal({name: document.name,numPages: document.numPages,path: router.asPath,- type: "pdf",+ type: document.type,newVersion: true,teamId: teamId,});diff --git a/lib/constants.ts b/lib/constants.tsindex eb54c948..68803014 100644--- a/lib/constants.ts+++ b/lib/constants.ts@@ -75,3 +75,11 @@ export const EXCLUDED_PATHS = [export const LIMITS = {views: 20,};++export const SUPPORTED_DOCUMENT_TYPES = [+ "application/pdf",+ "application/vnd.ms-excel",+ "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",+ "text/csv",+ "application/vnd.oasis.opendocument.spreadsheet",+];diff --git a/lib/documents/create-document.ts b/lib/documents/create-document.tsindex 019bec9e..66c6039a 100644--- a/lib/documents/create-document.ts+++ b/lib/documents/create-document.ts@@ -4,6 +4,7 @@ export type DocumentData = {name: string;key: string;storageType: DocumentStorageType;+ contentType: string; // papermark types: "pdf", "sheet"};export const createDocument = async ({@@ -29,6 +30,7 @@ export const createDocument = async ({storageType: documentData.storageType,numPages: numPages,folderPathName: folderPathName,+ type: documentData.contentType,}),});@@ -62,7 +64,7 @@ export const createNewDocumentVersion = async ({url: documentData.key,storageType: documentData.storageType,numPages: numPages,- type: "pdf",+ type: documentData.contentType,}),},);diff --git a/lib/files/put-file.ts b/lib/files/put-file.tsindex e37d410d..6ddc4e08 100644--- a/lib/files/put-file.ts+++ b/lib/files/put-file.ts@@ -5,6 +5,8 @@ import { match } from "ts-pattern";import { newId } from "@/lib/id-helper";import { getPagesCount } from "@/lib/utils/get-page-number-count";+import { SUPPORTED_DOCUMENT_TYPES } from "../constants";+// type File = {// name: string;// type: string;@@ -42,8 +44,11 @@ const putFileInVercel = async (file: File) => {handleUploadUrl: "/api/file/browser-upload",});- const contents = await file.arrayBuffer();- const numPages = await getPagesCount(contents);+ let numPages: number = 1;+ if (file.type === "application/pdf") {+ const contents = await file.arrayBuffer();+ numPages = await getPagesCount(contents);+ }return {type: DocumentStorageType.VERCEL_BLOB,@@ -65,13 +70,7 @@ const putFileInS3 = async ({docId = newId("doc");}- if (- ![- "application/pdf",- "application/vnd.ms-excel",- "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",- ].includes(file.type)- ) {+ if (!SUPPORTED_DOCUMENT_TYPES.includes(file.type)) {throw new Error("Only PDF and Excel files are supported");}diff --git a/lib/utils/get-content-type.ts b/lib/utils/get-content-type.tsnew file mode 100644index 00000000..d5e4d7aa--- /dev/null+++ b/lib/utils/get-content-type.ts@@ -0,0 +1,13 @@+export function getSupportedContentType(contentType: string): string | null {+ switch (contentType) {+ case "application/pdf":+ return "pdf";+ case "application/vnd.ms-excel":+ case "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet":+ case "text/csv":+ case "application/vnd.oasis.opendocument.spreadsheet":+ return "sheet";+ default:+ return null;+ }+}