Skip to content

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

feat: add download-only button to document

Marc Seitzf262cd826a3e
diff --git a/components/documents/document-header.tsx b/components/documents/document-header.tsx
index 8bc09f18..5a2b664a 100644
--- a/components/documents/document-header.tsx
+++ b/components/documents/document-header.tsx
@@ -13,6 +13,7 @@ import {
SheetIcon,
Sparkles,
TrashIcon,
+ ViewIcon,
} from "lucide-react";
import { usePlausible } from "next-plausible";
import { useTheme } from "next-themes";
@@ -317,6 +318,33 @@ export default function DocumentHeader({
}
};
+ // Make a document download only or viewable
+ const toggleDownloadOnly = async () => {
+ toast.promise(
+ fetch(
+ `/api/teams/${teamId}/documents/${prismaDocument.id}/toggle-download-only`,
+ {
+ method: "PATCH",
+ headers: {
+ "Content-Type": "application/json",
+ },
+ body: JSON.stringify({
+ downloadOnly: !prismaDocument.downloadOnly,
+ }),
+ },
+ ).then(() => {
+ mutate(`/api/teams/${teamId}/documents/${prismaDocument.id}`);
+ }),
+ {
+ loading: "Updating document...",
+ success: `Document is now ${
+ !prismaDocument.downloadOnly ? "download only" : "viewable"
+ }`,
+ error: "Failed to update document",
+ },
+ );
+ };
+
useEffect(() => {
function handleClickOutside(event: { target: any }) {
if (dropdownRef.current && !dropdownRef.current.contains(event.target)) {
@@ -652,6 +680,32 @@ export default function DocumentHeader({
Add to dataroom
</DropdownMenuItem>
)}
+
+ {primaryVersion.type !== "notion" &&
+ primaryVersion.type !== "zip" && (
+ <DropdownMenuItem
+ onClick={() =>
+ isFree
+ ? (setPlanModalTrigger("download-only-document"),
+ setPlanModalOpen(true))
+ : toggleDownloadOnly()
+ }
+ >
+ {prismaDocument.downloadOnly ? (
+ <>
+ <ViewIcon className="mr-2 h-4 w-4" />
+ Set viewable
+ </>
+ ) : (
+ <>
+ <CloudDownloadIcon className="mr-2 h-4 w-4" />
+ Set download only{" "}
+ {isFree && <PlanBadge className="ml-2" plan="pro" />}
+ </>
+ )}
+ </DropdownMenuItem>
+ )}
+
<DropdownMenuSeparator />
{/* Export views in CSV */}
diff --git a/components/view/dataroom/dataroom-view.tsx b/components/view/dataroom/dataroom-view.tsx
index ea2c3aef..c67461d7 100644
--- a/components/view/dataroom/dataroom-view.tsx
+++ b/components/view/dataroom/dataroom-view.tsx
@@ -353,6 +353,20 @@ export default function DataroomView({
screenshotProtectionEnabled={link.enableScreenshotProtection!}
/>
</div>
+ ) : documentData?.downloadOnly ? (
+ <DownloadOnlyViewer
+ file={viewData.file!}
+ linkId={link.id}
+ documentId={documentData.id}
+ viewId={viewData.viewId}
+ allowDownload={true}
+ versionNumber={documentData.documentVersionNumber}
+ brand={brand}
+ documentName={documentData.name}
+ isPreview={viewData.isPreview}
+ dataroomId={dataroom.id}
+ setDocumentData={setDocumentData}
+ />
) : viewData.fileType === "sheet" && viewData.sheetData ? (
<div className="bg-gray-950">
<ExcelViewer
@@ -441,20 +455,6 @@ export default function DataroomView({
linkName={link.name ?? `Link #${link.id.slice(-5)}`}
/>
</div>
- ) : documentData?.downloadOnly ? (
- <DownloadOnlyViewer
- file={viewData.file!}
- linkId={link.id}
- documentId={documentData.id}
- viewId={viewData.viewId}
- allowDownload={true}
- versionNumber={documentData.documentVersionNumber}
- brand={brand}
- documentName={documentData.name}
- isPreview={viewData.isPreview}
- dataroomId={dataroom.id}
- setDocumentData={setDocumentData}
- />
) : null;
}
diff --git a/components/view/view-data.tsx b/components/view/view-data.tsx
index c9ce967c..9dce5d1a 100644
--- a/components/view/view-data.tsx
+++ b/components/view/view-data.tsx
@@ -57,6 +57,18 @@ export default function ViewData({
theme={notionData.theme}
screenshotProtectionEnabled={link.enableScreenshotProtection!}
/>
+ ) : document.downloadOnly ? (
+ <DownloadOnlyViewer
+ file={viewData.file!}
+ linkId={link.id}
+ viewId={viewData.viewId}
+ documentId={document.id}
+ allowDownload={true}
+ versionNumber={document.versions[0].versionNumber}
+ brand={brand}
+ documentName={document.name}
+ isPreview={viewData.isPreview}
+ />
) : viewData.fileType === "sheet" && viewData.sheetData ? (
<ExcelViewer
linkId={link.id}
@@ -134,18 +146,6 @@ export default function ViewData({
ipAddress={viewData.ipAddress}
linkName={link.name ?? `Link #${link.id.slice(-5)}`}
/>
- ) : document.downloadOnly ? (
- <DownloadOnlyViewer
- file={viewData.file!}
- linkId={link.id}
- viewId={viewData.viewId}
- documentId={document.id}
- allowDownload={true}
- versionNumber={document.versions[0].versionNumber}
- brand={brand}
- documentName={document.name}
- isPreview={viewData.isPreview}
- />
) : (
<PDFViewer
file={viewData.file}
diff --git a/pages/api/teams/[teamId]/documents/[id]/toggle-download-only.ts b/pages/api/teams/[teamId]/documents/[id]/toggle-download-only.ts
new file mode 100644
index 00000000..d23ae2b7
--- /dev/null
+++ b/pages/api/teams/[teamId]/documents/[id]/toggle-download-only.ts
@@ -0,0 +1,68 @@
+import { NextApiRequest, NextApiResponse } from "next";
+
+import { authOptions } from "@/pages/api/auth/[...nextauth]";
+import { getServerSession } from "next-auth/next";
+
+import prisma from "@/lib/prisma";
+import { CustomUser } from "@/lib/types";
+
+export default async function handle(
+ req: NextApiRequest,
+ res: NextApiResponse,
+) {
+ if (req.method !== "PATCH") {
+ return res.status(405).json({ message: "Method not allowed" });
+ }
+
+ const session = await getServerSession(req, res, authOptions);
+ if (!session) {
+ return res.status(401).json({ message: "Unauthorized" });
+ }
+
+ const userId = (session.user as CustomUser).id;
+ const { teamId, id: documentId } = req.query as {
+ teamId: string;
+ id: string;
+ };
+ const { downloadOnly } = req.body as { downloadOnly: boolean };
+
+ try {
+ // Check if user has access to the team
+ const team = await prisma.team.findUnique({
+ where: {
+ id: teamId,
+ users: {
+ some: {
+ userId: userId,
+ },
+ },
+ },
+ });
+
+ if (!team) {
+ return res.status(401).json({ message: "Unauthorized" });
+ }
+
+ // Update document
+ await prisma.document.update({
+ where: {
+ id: documentId,
+ teamId: teamId,
+ },
+ data: {
+ downloadOnly: downloadOnly,
+ },
+ });
+
+ await fetch(
+ `${process.env.NEXTAUTH_URL}/api/revalidate?secret=${process.env.REVALIDATE_TOKEN}&documentId=${documentId}`,
+ );
+
+ return res.status(200).json({
+ message: `Document is now ${downloadOnly ? "download only" : "viewable"}`,
+ });
+ } catch (error) {
+ console.error(error);
+ return res.status(500).json({ message: "Error updating document" });
+ }
+}