Skip to content

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

chore: prettier

Marc Seitz4135090d72f7
diff --git a/pages/api/teams/[teamId]/agreements/[agreementId]/download.ts b/pages/api/teams/[teamId]/agreements/[agreementId]/download.ts
index 677f24e4..4c072e21 100644
--- a/pages/api/teams/[teamId]/agreements/[agreementId]/download.ts
+++ b/pages/api/teams/[teamId]/agreements/[agreementId]/download.ts
@@ -21,7 +21,10 @@ export default async function handle(
}
const userId = (session.user as CustomUser).id;
- const { teamId, agreementId } = req.query as { teamId: string; agreementId: string };
+ const { teamId, agreementId } = req.query as {
+ teamId: string;
+ agreementId: string;
+ };
if (!teamId || !agreementId) {
return res.status(400).json("Missing required parameters");
@@ -71,8 +74,9 @@ export default async function handle(
const agreement = team.agreements[0];
// Check if the content is a Papermark URL
- const isPapermarkUrl = agreement.content.includes('papermark.com/view/') ||
- agreement.content.includes('www.papermark.com/view/');
+ const isPapermarkUrl =
+ agreement.content.includes("papermark.com/view/") ||
+ agreement.content.includes("www.papermark.com/view/");
let fileContent: string;
let filename: string;
@@ -80,13 +84,13 @@ export default async function handle(
if (isPapermarkUrl) {
// Extract linkId from Papermark URL
- const urlParts = agreement.content.split('/view/');
+ const urlParts = agreement.content.split("/view/");
if (urlParts.length < 2) {
return res.status(400).json("Invalid Papermark URL format");
}
-
+
const linkId = urlParts[1].split(/[/?#]/)[0]; // Get linkId, remove any query params or fragments
-
+
// Fetch the link and its document
link = await prisma.link.findUnique({
where: { id: linkId },
@@ -116,16 +120,22 @@ export default async function handle(
});
if (!link || !link.document) {
- return res.status(404).json("Document not found for the provided Papermark URL");
+ return res
+ .status(404)
+ .json("Document not found for the provided Papermark URL");
}
// Use the primary version if available, otherwise use the document file
const documentVersion = link.document.versions[0];
- const fileKey = documentVersion ?
- (documentVersion.originalFile || documentVersion.file) :
- (link.document.originalFile || link.document.file);
- const storageType = documentVersion ? documentVersion.storageType : link.document.storageType;
- const fileType = documentVersion ? documentVersion.type : link.document.type;
+ const fileKey = documentVersion
+ ? documentVersion.originalFile || documentVersion.file
+ : link.document.originalFile || link.document.file;
+ const storageType = documentVersion
+ ? documentVersion.storageType
+ : link.document.storageType;
+ const fileType = documentVersion
+ ? documentVersion.type
+ : link.document.type;
// Get the file URL from storage
const fileUrl = await getFile({
@@ -134,54 +144,63 @@ export default async function handle(
isDownload: true,
});
-// Fetch the actual file content with safety measures
- const controller = new AbortController();
- const timeoutId = setTimeout(() => controller.abort(), 30000); // 30 second timeout
-
- const fileResponse = await fetch(fileUrl, {
- signal: controller.signal,
- // Add size limit check in the response handling
- });
-
- clearTimeout(timeoutId);
-
-
+ // Fetch the actual file content with safety measures
+ const controller = new AbortController();
+ const timeoutId = setTimeout(() => controller.abort(), 30000); // 30 second timeout
+
+ const fileResponse = await fetch(fileUrl, {
+ signal: controller.signal,
+ // Add size limit check in the response handling
+ });
+
+ clearTimeout(timeoutId);
+
if (!fileResponse.ok) {
throw new Error("Failed to fetch document content");
}
// Use the document name for filename
const docName = link.document.name.replace(/\.[^/.]+$/, ""); // Remove extension
- let extension = 'txt';
- if (fileType === 'pdf') extension = 'pdf';
- else if (fileType === 'docs') extension = 'docx';
- else if (fileType === 'slides') extension = 'pptx';
- else if (fileType === 'sheet') extension = 'xlsx';
-
- filename = `${docName.replace(/[^a-z0-9\-_]/gi, '_').toLowerCase().substring(0, 50)}_agreement.${extension}`;
+ let extension = "txt";
+ if (fileType === "pdf") extension = "pdf";
+ else if (fileType === "docs") extension = "docx";
+ else if (fileType === "slides") extension = "pptx";
+ else if (fileType === "sheet") extension = "xlsx";
+
+ filename = `${docName
+ .replace(/[^a-z0-9\-_]/gi, "_")
+ .toLowerCase()
+ .substring(0, 50)}_agreement.${extension}`;
// Handle different file types appropriately
- const isPdf = fileType === 'pdf' || link.document.contentType?.includes('pdf');
- const isDocx = fileType === 'docs' || link.document.contentType?.includes('wordprocessingml');
-
+ const isPdf =
+ fileType === "pdf" || link.document.contentType?.includes("pdf");
+ const isDocx =
+ fileType === "docs" ||
+ link.document.contentType?.includes("wordprocessingml");
+
if (isPdf || isDocx) {
// Handle binary files (PDFs, Word docs)
const buffer = await fileResponse.arrayBuffer();
- let contentType = link.document.contentType || 'application/octet-stream';
-
- if (isPdf) contentType = 'application/pdf';
- else if (isDocx) contentType = 'application/vnd.openxmlformats-officedocument.wordprocessingml.document';
-
-
+ let contentType =
+ link.document.contentType || "application/octet-stream";
+
+ if (isPdf) contentType = "application/pdf";
+ else if (isDocx)
+ contentType =
+ "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
+
res.setHeader("Content-Type", contentType);
- res.setHeader("Content-Disposition", `attachment; filename="${filename}"`);
+ res.setHeader(
+ "Content-Disposition",
+ `attachment; filename="${filename}"`,
+ );
res.setHeader("Content-Length", buffer.byteLength.toString());
return res.send(Buffer.from(buffer));
} else {
// Handle text-based files
fileContent = await fileResponse.text();
}
-
} else {
// Regular URL - return formatted metadata as before
fileContent = `
@@ -211,14 +230,20 @@ Downloaded on: ${new Date().toLocaleDateString()} at ${new Date().toLocaleTimeSt
Agreement ID: ${agreement.id}
`.trim();
- filename = `${agreement.name.replace(/[^a-z0-9\-_]/gi, '_').toLowerCase().substring(0, 50)}_agreement.txt`;
+ filename = `${agreement.name
+ .replace(/[^a-z0-9\-_]/gi, "_")
+ .toLowerCase()
+ .substring(0, 50)}_agreement.txt`;
}
// Set headers for file download (only for text files - PDFs are handled above)
if (fileContent) {
res.setHeader("Content-Type", "text/plain; charset=utf-8");
- res.setHeader("Content-Disposition", `attachment; filename="${filename}"`);
- res.setHeader("Content-Length", Buffer.byteLength(fileContent, 'utf8'));
+ res.setHeader(
+ "Content-Disposition",
+ `attachment; filename="${filename}"`,
+ );
+ res.setHeader("Content-Length", Buffer.byteLength(fileContent, "utf8"));
return res.send(fileContent);
}
} catch (error) {
@@ -229,4 +254,4 @@ Agreement ID: ${agreement.id}
res.setHeader("Allow", ["POST"]);
return res.status(405).end(`Method ${req.method} Not Allowed`);
}
-}
\ No newline at end of file
+}