Skip to content

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

feat: download document inline

Marc Seitzf0f5b00b228e
diff --git a/components/view/DataroomViewer.tsx b/components/view/DataroomViewer.tsx
index 70a209a7..5304a957 100644
--- a/components/view/DataroomViewer.tsx
+++ b/components/view/DataroomViewer.tsx
@@ -118,7 +118,9 @@ export default function DataroomViewer({
return {
...doc,
itemType: "document",
- canDownload: accessControl?.canDownload ?? true,
+ canDownload:
+ (accessControl?.canDownload ?? true) &&
+ doc.versions[0].type !== "notion",
};
}),
];
diff --git a/components/view/dataroom/document-card.tsx b/components/view/dataroom/document-card.tsx
index a87207e5..4d03bd88 100644
--- a/components/view/dataroom/document-card.tsx
+++ b/components/view/dataroom/document-card.tsx
@@ -2,11 +2,18 @@ import { useRouter } from "next/router";
import React from "react";
-import { ArrowDownToLineIcon } from "lucide-react";
+import { Download, MoreVerticalIcon } from "lucide-react";
import { useTheme } from "next-themes";
import { toast } from "sonner";
import { Button } from "@/components/ui/button";
+import {
+ DropdownMenu,
+ DropdownMenuContent,
+ DropdownMenuItem,
+ DropdownMenuLabel,
+ DropdownMenuTrigger,
+} from "@/components/ui/dropdown-menu";
import { fileIcon } from "@/lib/utils/get-file-icon";
@@ -69,7 +76,7 @@ export default function DocumentCard({
return;
}
try {
- const response = await fetch(`/api/links/download/dataroomDocumet`, {
+ const response = await fetch(`/api/links/download/dataroom-document`, {
method: "POST",
headers: {
"Content-Type": "application/json",
@@ -85,11 +92,61 @@ export default function DocumentCard({
toast.error("Error downloading file");
return;
}
- const { downloadUrl } = await response.json();
- window.open(downloadUrl, "_blank");
+ // Check if the response is JSON (for direct downloads) or binary (for buffered files)
+ const contentType = response.headers.get("content-type");
+
+ // If it's a watermarked PDF, handle it with the buffer method
+ if (contentType?.includes("application/pdf")) {
+ const blob = await response.blob();
+ const url = window.URL.createObjectURL(blob);
+
+ const link = window.document.createElement("a");
+ link.href = url;
+ const disposition = response.headers.get("content-disposition");
+ const filenameMatch =
+ disposition && disposition.match(/filename="(.+)"/);
+ link.download = filenameMatch
+ ? decodeURIComponent(filenameMatch[1])
+ : document.name;
+
+ window.document.body.appendChild(link);
+ link.click();
+ window.document.body.removeChild(link);
+ window.URL.revokeObjectURL(url);
+
+ toast.success("File downloaded successfully");
+ return;
+ }
+
+ // For all other files, use the iframe method
+ if (contentType?.includes("application/json")) {
+ const data = await response.json();
+ const downloadUrl = data.isDirectDownload
+ ? data.downloadUrl
+ : response.url;
+
+ // Create a hidden iframe for download
+ const iframe = window.document.createElement("iframe");
+ iframe.style.display = "none";
+ window.document.body.appendChild(iframe);
+ iframe.src = downloadUrl;
+
+ // Clean up the iframe after a delay
+ setTimeout(() => {
+ if (iframe && iframe.parentNode) {
+ window.document.body.removeChild(iframe);
+ }
+ }, 5000);
+
+ toast.success("Download started");
+ return;
+ }
+
+ toast.error("Unexpected response format");
} catch (error) {
console.error("Error downloading file:", error);
+ toast.error("Error downloading file");
}
};
@@ -120,19 +177,32 @@ export default function DocumentCard({
</div>
</div>
{canDownload && (
- <div className="invisible z-10 group-hover/row:visible">
- <Button
- onClick={(e) => {
- e.preventDefault();
- e.stopPropagation();
- downloadDocument();
- }}
- variant="default"
- className="rounded-md p-2"
- aria-label="Download document"
- >
- <ArrowDownToLineIcon className="h-5 w-5" />
- </Button>
+ <div className="z-10">
+ <DropdownMenu>
+ <DropdownMenuTrigger asChild>
+ <Button
+ variant="ghost"
+ size="icon"
+ className="h-8 w-8 p-0 text-gray-500 ring-1 ring-gray-100 hover:bg-gray-200 group-hover/row:text-foreground group-hover/row:ring-gray-300"
+ aria-label="Open menu"
+ >
+ <MoreVerticalIcon className="h-4 w-4" />
+ </Button>
+ </DropdownMenuTrigger>
+ <DropdownMenuContent align="end">
+ <DropdownMenuLabel>Actions</DropdownMenuLabel>
+ <DropdownMenuItem
+ onClick={(e) => {
+ e.preventDefault();
+ e.stopPropagation();
+ downloadDocument();
+ }}
+ >
+ <Download className="h-4 w-4" />
+ Download
+ </DropdownMenuItem>
+ </DropdownMenuContent>
+ </DropdownMenu>
</div>
)}
</li>
diff --git a/pages/api/links/download/dataroomDocumet.ts b/pages/api/links/download/dataroom-document.ts
similarity index 68%
rename from pages/api/links/download/dataroomDocumet.ts
rename to pages/api/links/download/dataroom-document.ts
index 018cd19f..4cde8259 100644
--- a/pages/api/links/download/dataroomDocumet.ts
+++ b/pages/api/links/download/dataroom-document.ts
@@ -104,10 +104,10 @@ export default async function handle(
return res.status(404).json({ error: "Error downloading" });
}
- // if viewedAt is longer than 30 mins ago, we should not allow the download
+ // if viewedAt is longer than 23 hours ago, we should not allow the download
if (
view.viewedAt &&
- view.viewedAt < new Date(Date.now() - 30 * 60 * 1000)
+ view.viewedAt < new Date(Date.now() - 23 * 60 * 60 * 1000)
) {
return res.status(403).json({ error: "Error downloading" });
}
@@ -150,14 +150,20 @@ export default async function handle(
},
});
+ const file =
+ view.link.enableWatermark &&
+ downloadDocuments[0].document!.versions[0].type === "pdf"
+ ? downloadDocuments[0].document!.versions[0].file
+ : (downloadDocuments[0].document!.versions[0].originalFile ??
+ downloadDocuments[0].document!.versions[0].file);
+
const downloadUrl = await getFile({
type: downloadDocuments[0].document!.versions[0].storageType,
- data:
- downloadDocuments[0].document!.versions[0].originalFile ??
- downloadDocuments[0].document!.versions[0].file,
+ data: file,
isDownload: true,
});
+ // For PDF files with watermark, always buffer and process
if (
downloadDocuments[0].document!.versions[0].type === "pdf" &&
view.link.enableWatermark
@@ -190,20 +196,69 @@ export default async function handle(
const pdfBuffer = await response.arrayBuffer();
- // Set appropriate headers
+ // Set appropriate headers for watermarked PDF
+ res.setHeader("Content-Type", "application/pdf");
+ res.setHeader(
+ "Content-Disposition",
+ `attachment; filename="${encodeURIComponent(downloadDocuments[0].document!.name)}"`,
+ );
+ res.setHeader("Content-Length", Buffer.from(pdfBuffer).length);
+
+ // Send the watermarked buffer directly
+ return res.send(Buffer.from(pdfBuffer));
+ }
+
+ // For non-watermarked PDFs, we need to buffer and set proper headers
+ // - contentType is application/pdf
+ // - contentType is null and type is pdf
+ // - contentType starts with image/
+ if (
+ downloadDocuments[0].document!.versions[0].contentType ===
+ "application/pdf" ||
+ (downloadDocuments[0].document!.versions[0].contentType === null &&
+ downloadDocuments[0].document!.versions[0].type === "pdf") ||
+ downloadDocuments[0].document!.versions[0].contentType?.startsWith(
+ "image/",
+ )
+ ) {
+ const response = await fetch(downloadUrl);
+ if (!response.ok) {
+ return res.status(500).json({ error: "Error downloading file" });
+ }
+
+ const pdfBuffer = await response.arrayBuffer();
+
+ // Set appropriate headers to force download
res.setHeader("Content-Type", "application/pdf");
res.setHeader(
"Content-Disposition",
- 'attachment; filename="watermarked.pdf"',
+ `attachment; filename="${encodeURIComponent(downloadDocuments[0].document!.name)}"`,
);
+ res.setHeader("Content-Length", Buffer.from(pdfBuffer).length);
+ res.setHeader("Cache-Control", "no-cache");
- // Send the buffer directly
+ // Send the PDF buffer directly
return res.send(Buffer.from(pdfBuffer));
}
- return res.status(200).json({ downloadUrl });
- // Add documents in folders
- } catch {}
+ const headResponse = await fetch(downloadUrl, { method: "HEAD" });
+ const contentType =
+ downloadDocuments[0].document!.versions[0].contentType ||
+ headResponse.headers.get("content-type") ||
+ "application/octet-stream";
+ const fileName = downloadDocuments[0].document!.name;
+
+ // For all other files, return direct download URL
+ return res.status(200).json({
+ downloadUrl,
+ fileName,
+ contentType,
+ isDirectDownload: true,
+ });
+ } catch (error) {
+ console.error("Error:", error);
+ return res.status(500).json({ error: "Error downloading file" });
+ }
} else {
// We only allow POST requests
res.setHeader("Allow", ["POST"]);