Skip to content

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

Bug: Added proxy route to handle serverside download

Ashish Viradiyadffe97562fc4
diff --git a/lib/files/get-file.ts b/lib/files/get-file.ts
index 5248e646..3504f2ed 100644
--- a/lib/files/get-file.ts
+++ b/lib/files/get-file.ts
@@ -28,31 +28,56 @@ export const getFile = async ({
};
const getFileFromS3 = async (key: string) => {
- const response = await fetch(
- `${process.env.NEXT_PUBLIC_BASE_URL}/api/file/s3/get-presigned-get-url`,
- {
+ const isServer = typeof window === 'undefined' && process.env.INTERNAL_API_KEY;
+
+ if (isServer) {
+ const response = await fetch(
+ `${process.env.NEXT_PUBLIC_BASE_URL}/api/file/s3/get-presigned-get-url`,
+ {
+ method: "POST",
+ headers: {
+ "Content-Type": "application/json",
+ Authorization: `Bearer ${process.env.INTERNAL_API_KEY}`,
+ },
+ body: JSON.stringify({ key: key }),
+ },
+ );
+
+ if (!response.ok) {
+ try {
+ const error = await response.json();
+ throw new Error(
+ error.message || `Request failed with status ${response.status}`,
+ );
+ } catch (parseError) {
+ // Handle cases where the response isn't valid JSON
+ throw new Error(`Request failed with status ${response.status}`);
+ }
+ }
+
+ const { url } = (await response.json()) as { url: string };
+ return url;
+ } else {
+ const response = await fetch(`/api/file/s3/get-presigned-get-url-proxy`, {
method: "POST",
headers: {
"Content-Type": "application/json",
- Authorization: `Bearer ${process.env.INTERNAL_API_KEY}`,
},
body: JSON.stringify({ key: key }),
- },
- );
-
- if (!response.ok) {
- try {
- const error = await response.json();
- throw new Error(
- error.message || `Request failed with status ${response.status}`,
- );
- } catch (parseError) {
- // Handle cases where the response isn't valid JSON
- throw new Error(`Request failed with status ${response.status}`);
- }
- }
+ });
- const { url } = (await response.json()) as { url: string };
+ if (!response.ok) {
+ try {
+ const error = await response.json();
+ throw new Error(
+ error.message || `Request failed with status ${response.status}`,
+ );
+ } catch (parseError) {
+ throw new Error(`Request failed with status ${response.status}`);
+ }
+ }
- return url;
+ const { url } = (await response.json()) as { url: string };
+ return url;
+ }
};
diff --git a/pages/api/file/s3/get-presigned-get-url-proxy.ts b/pages/api/file/s3/get-presigned-get-url-proxy.ts
new file mode 100644
index 00000000..34daf7f8
--- /dev/null
+++ b/pages/api/file/s3/get-presigned-get-url-proxy.ts
@@ -0,0 +1,49 @@
+import { NextApiRequest, NextApiResponse } from "next";
+
+import { authOptions } from "@/pages/api/auth/[...nextauth]";
+import { getServerSession } from "next-auth/next";
+
+export default async function handler(
+ req: NextApiRequest,
+ res: NextApiResponse,
+) {
+ if (req.method !== "POST") {
+ return res.status(405).end("Method Not Allowed");
+ }
+
+ const session = await getServerSession(req, res, authOptions);
+ if (!session) {
+ return res.status(401).json({ message: "Unauthorized" });
+ }
+
+ const { key } = req.body as { key: string };
+
+ if (!key) {
+ return res.status(400).json({ message: "Key is required" });
+ }
+
+ try {
+ const response = await fetch(
+ `${process.env.NEXTAUTH_URL}/api/file/s3/get-presigned-get-url`,
+ {
+ method: "POST",
+ headers: {
+ "Content-Type": "application/json",
+ Authorization: `Bearer ${process.env.INTERNAL_API_KEY}`,
+ },
+ body: JSON.stringify({ key: key }),
+ },
+ );
+
+ if (!response.ok) {
+ const error = await response.json();
+ return res.status(response.status).json(error);
+ }
+
+ const data = await response.json();
+ return res.status(200).json(data);
+ } catch (error) {
+ console.error("Proxy error:", error);
+ return res.status(500).json({ message: "Internal server error" });
+ }
+}