Skip to content

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

fix: dataroom individual file download silently failing

Marc Seitz21d5a2ac455b
diff --git a/lib/utils/download-document.ts b/lib/utils/download-document.ts
index 0f1fa5ed..8d02b5bf 100644
--- a/lib/utils/download-document.ts
+++ b/lib/utils/download-document.ts
@@ -1,22 +1,3 @@
-/**
- * Trigger a document download from one of the link-scoped download
- * endpoints (`/api/links/download` or `/api/links/download/dataroom-document`).
- *
- * The server returns one of two response shapes:
- *
- * 1. A buffered binary response (currently only watermarked PDFs that need
- * mupdf annotation) with `Content-Disposition` set to the desired
- * filename. We turn it into a blob + `<a download>` to save it.
- *
- * 2. A JSON response of the form `{ downloadUrl, fileName }`. The URL is
- * a CloudFront / S3 presigned URL with `response-content-disposition`
- * baked in, so we just iframe-redirect to it. The browser streams the
- * bytes straight from origin and saves the file with the right name -
- * no proxying through the JS heap.
- *
- * `fallbackFileName` is used only when the buffered response is missing
- * its `Content-Disposition` header (shouldn't happen in practice).
- */
export async function downloadFromLinkEndpoint({
endpoint,
body,
@@ -40,23 +21,15 @@ export async function downloadFromLinkEndpoint({
const contentType = response.headers.get("content-type");
if (contentType?.includes("application/json")) {
- const { downloadUrl } = (await response.json()) as { downloadUrl: string };
-
- const iframe = window.document.createElement("iframe");
- iframe.style.display = "none";
- window.document.body.appendChild(iframe);
- iframe.src = downloadUrl;
-
- setTimeout(() => {
- if (iframe.parentNode) {
- window.document.body.removeChild(iframe);
- }
- }, 5000);
+ const { downloadUrl, fileName } = (await response.json()) as {
+ downloadUrl: string;
+ fileName?: string;
+ };
+ triggerDownloadFromUrl(downloadUrl, fileName ?? fallbackFileName);
return;
}
- // Buffered binary response (e.g. watermarked PDF).
const blob = await response.blob();
const objectUrl = window.URL.createObjectURL(blob);
@@ -79,11 +52,22 @@ export async function downloadFromLinkEndpoint({
}, 100);
}
-/**
- * Parse a `Content-Disposition` header. Prefers RFC 5987's
- * `filename*=UTF-8''<encoded>` form so unicode filenames survive, and falls
- * back to the legacy quoted-string `filename="..."` form.
- */
+function triggerDownloadFromUrl(url: string, suggestedFileName: string): void {
+ const link = window.document.createElement("a");
+ link.href = url;
+ link.rel = "noopener noreferrer";
+ link.download = suggestedFileName;
+ link.style.display = "none";
+ window.document.body.appendChild(link);
+ link.click();
+
+ setTimeout(() => {
+ if (link.parentNode) {
+ window.document.body.removeChild(link);
+ }
+ }, 100);
+}
+
export function extractFilenameFromContentDisposition(
disposition: string | null,
): string | null {