Skip to content

Papermark is the open-source DocSend alternative and secure data rooms with built-in analytics and custom domains.

fix: hide advanced mode for CSV and non-Excel sheet documents

Ashish Viradiya3c8ff683619a
diff --git a/app/api/links/[id]/upload/route.ts b/app/api/links/[id]/upload/route.ts
index a31a0338..46a97670 100644
--- a/app/api/links/[id]/upload/route.ts
+++ b/app/api/links/[id]/upload/route.ts
@@ -4,6 +4,7 @@ import { processDocument } from "@/lib/api/documents/process-document";
import { verifyDataroomSession } from "@/lib/auth/dataroom-auth";
import { DocumentData } from "@/lib/documents/create-document";
import prisma from "@/lib/prisma";
+import { supportsAdvancedExcelMode } from "@/lib/utils/get-content-type";
export async function POST(
request: NextRequest,
@@ -91,7 +92,9 @@ export async function POST(
const updatedDocumentData = {
...documentData,
- enableExcelAdvancedMode: link.team?.enableExcelAdvancedMode ?? false,
+ enableExcelAdvancedMode: documentData.supportedFileType === "sheet" &&
+ link.team?.enableExcelAdvancedMode &&
+ supportsAdvancedExcelMode(documentData.contentType),
};
// 1. Create the document
diff --git a/components/documents/document-header.tsx b/components/documents/document-header.tsx
index b88ce452..d55c7909 100644
--- a/components/documents/document-header.tsx
+++ b/components/documents/document-header.tsx
@@ -34,6 +34,7 @@ import {
DocumentWithVersion,
} from "@/lib/types";
import { cn } from "@/lib/utils";
+import { supportsAdvancedExcelMode } from "@/lib/utils/get-content-type";
import { fileIcon } from "@/lib/utils/get-file-icon";
import FileUp from "@/components/shared/icons/file-up";
@@ -742,6 +743,7 @@ export default function DocumentHeader({
))} */}
{prismaDocument.type === "sheet" &&
!prismaDocument.advancedExcelEnabled &&
+ supportsAdvancedExcelMode(primaryVersion.contentType) &&
(isBusiness || isDatarooms || isTrial) && (
<DropdownMenuItem
onClick={() => enableAdvancedExcel(prismaDocument)}
@@ -927,32 +929,35 @@ export default function DocumentHeader({
/>
)}
- {prismaDocument.type === "sheet" && (isFree || isPro) && (
- <AlertBanner
- id="advanced-excel-alert"
- variant="default"
- title="Advanced Excel mode"
- description={
- <>
- You can turn on advanced excel mode by{" "}
- <span
- className="cursor-pointer underline underline-offset-4 hover:text-primary/80"
- onClick={() =>
- handleUpgradeClick(PlanEnum.Business, "advanced-excel-mode")
- }
- >
- upgrading
- </span>{" "}
- to Business plan to preserve the file formatting. This uses the
- Microsoft Office viewer.
- </>
- }
- onClose={() => handleCloseAlert("advanced-excel-alert")}
- />
- )}
+ {prismaDocument.type === "sheet" &&
+ supportsAdvancedExcelMode(primaryVersion.contentType) &&
+ (isFree || isPro) && (
+ <AlertBanner
+ id="advanced-excel-alert"
+ variant="default"
+ title="Advanced Excel mode"
+ description={
+ <>
+ You can turn on advanced excel mode by{" "}
+ <span
+ className="hover:text-primary/ 80 cursor-pointer underline underline-offset-4"
+ onClick={() =>
+ handleUpgradeClick(PlanEnum.Business, "advanced-excel-mode")
+ }
+ >
+ upgrading
+ </span>{" "}
+ to Business plan to preserve the file formatting. This uses the
+ Microsoft Office viewer.
+ </>
+ }
+ onClose={() => handleCloseAlert("advanced-excel-alert")}
+ />
+ )}
{prismaDocument.type === "sheet" &&
!prismaDocument.advancedExcelEnabled &&
+ supportsAdvancedExcelMode(primaryVersion.contentType) &&
(isBusiness || isDatarooms || isTrial) && (
<AlertBanner
id="enable-advanced-excel-alert"
diff --git a/lib/utils/get-content-type.ts b/lib/utils/get-content-type.ts
index 073b2400..3d00149a 100644
--- a/lib/utils/get-content-type.ts
+++ b/lib/utils/get-content-type.ts
@@ -100,3 +100,13 @@ export function getExtensionFromContentType(
return null;
}
}
+
+export function supportsAdvancedExcelMode(contentType: string | null | undefined): boolean {
+ if (!contentType) return false;
+
+ return (
+ contentType === "application/vnd.ms-excel" || // .xls
+ contentType === "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" || // .xlsx
+ contentType === "application/vnd.ms-excel.sheet.macroEnabled.12" // .xlsm
+ );
+}
diff --git a/pages/api/teams/[teamId]/documents/[id]/advanced-mode.ts b/pages/api/teams/[teamId]/documents/[id]/advanced-mode.ts
index 971022c0..1821c181 100644
--- a/pages/api/teams/[teamId]/documents/[id]/advanced-mode.ts
+++ b/pages/api/teams/[teamId]/documents/[id]/advanced-mode.ts
@@ -7,6 +7,7 @@ import { errorhandler } from "@/lib/errorHandler";
import { copyFileToBucketServer } from "@/lib/files/copy-file-to-bucket-server";
import prisma from "@/lib/prisma";
import { CustomUser } from "@/lib/types";
+import { supportsAdvancedExcelMode } from "@/lib/utils/get-content-type";
export default async function handle(
req: NextApiRequest,
@@ -48,12 +49,24 @@ export default async function handle(
isPrimary: true,
type: "sheet",
},
+ select: {
+ id: true,
+ file: true,
+ storageType: true,
+ contentType: true,
+ },
});
if (!documentVersion) {
return res.status(404).end("Document not found");
}
+ if (!supportsAdvancedExcelMode(documentVersion.contentType)) {
+ return res.status(400).json({
+ message: "Advanced mode is only available for Excel files (.xls, .xlsx, .xlsm).",
+ });
+ }
+
await copyFileToBucketServer({
filePath: documentVersion.file,
storageType: documentVersion.storageType,
diff --git a/pages/api/teams/[teamId]/documents/index.ts b/pages/api/teams/[teamId]/documents/index.ts
index ca3a60c3..16e2b302 100644
--- a/pages/api/teams/[teamId]/documents/index.ts
+++ b/pages/api/teams/[teamId]/documents/index.ts
@@ -11,6 +11,7 @@ import prisma from "@/lib/prisma";
import { getTeamWithUsersAndDocument } from "@/lib/team/helper";
import { CustomUser } from "@/lib/types";
import { log } from "@/lib/utils";
+import { supportsAdvancedExcelMode } from "@/lib/utils/get-content-type";
export const config = {
// in order to enable `waitUntil` function
@@ -278,7 +279,7 @@ export default async function handle(
contentType,
fileSize,
enableExcelAdvancedMode:
- fileType === "sheet" && team.enableExcelAdvancedMode,
+ fileType === "sheet" && team.enableExcelAdvancedMode && supportsAdvancedExcelMode(contentType),
},
teamId,
userId,
diff --git a/pages/api/teams/[teamId]/enable-advanced-mode.ts b/pages/api/teams/[teamId]/enable-advanced-mode.ts
index 468ca1c6..128cffb5 100644
--- a/pages/api/teams/[teamId]/enable-advanced-mode.ts
+++ b/pages/api/teams/[teamId]/enable-advanced-mode.ts
@@ -6,6 +6,7 @@ import { getServerSession } from "next-auth/next";
import { errorhandler } from "@/lib/errorHandler";
import { copyFileToBucketServer } from "@/lib/files/copy-file-to-bucket-server";
import prisma from "@/lib/prisma";
+import { supportsAdvancedExcelMode } from "@/lib/utils/get-content-type";
import { authOptions } from "../../auth/[...nextauth]";
@@ -71,12 +72,23 @@ export default async function handle(
isPrimary: true,
type: "sheet",
},
+ select: {
+ id: true,
+ file: true,
+ storageType: true,
+ contentType: true,
+ },
},
},
});
+ const eligibleDocuments = documents.filter(doc => {
+ const primaryVersion = doc.versions[0];
+ return primaryVersion && supportsAdvancedExcelMode(primaryVersion.contentType);
+ });
+
// Update all documents and their versions
- const updatePromises = documents.map(async (doc) => {
+ const updatePromises = eligibleDocuments.map(async (doc) => {
const primaryVersion = doc.versions[0];
if (!primaryVersion) return;
@@ -122,7 +134,7 @@ export default async function handle(
});
return {
team: updatedTeam,
- updatedDocumentsCount: documents.length,
+ updatedDocumentsCount: eligibleDocuments.length,
};
});
return res.status(200).json({