Skip to content

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

chore: add logging to signed url request

Marc Seitz64de1311dab9
diff --git a/lib/files/get-file.ts b/lib/files/get-file.ts
index 45d4c257..48eb6d49 100644
--- a/lib/files/get-file.ts
+++ b/lib/files/get-file.ts
@@ -34,15 +34,15 @@ const getFileFromS3 = async (key: string) => {
method: "POST",
headers: {
"Content-Type": "application/json",
+ Authorization: `Bearer ${process.env.INTERNAL_API_KEY}`,
},
body: JSON.stringify({ key: key }),
},
);
if (!response.ok) {
- throw new Error(
- `Failed to get presigned get url, failed with status code ${response.status}`,
- );
+ const error = await response.json();
+ throw new Error(error.message);
}
const { url } = (await response.json()) as { url: string };
diff --git a/pages/api/file/s3/get-presigned-get-url.ts b/pages/api/file/s3/get-presigned-get-url.ts
index 296ed0cb..c7f1e8dd 100644
--- a/pages/api/file/s3/get-presigned-get-url.ts
+++ b/pages/api/file/s3/get-presigned-get-url.ts
@@ -6,6 +6,7 @@ import { getSignedUrl as getS3SignedUrl } from "@aws-sdk/s3-request-presigner";
import { ONE_HOUR, ONE_SECOND } from "@/lib/constants";
import { getS3Client } from "@/lib/files/aws-client";
+import { log } from "@/lib/utils";
const client = getS3Client();
@@ -17,6 +18,16 @@ export default async function handler(
return res.status(405).end("Method Not Allowed");
}
+ // Extract the API Key from the Authorization header
+ const authHeader = req.headers.authorization;
+ const token = authHeader?.split(" ")[1]; // Assuming the format is "Bearer [token]"
+
+ // Check if the API Key matches
+ if (token !== process.env.INTERNAL_API_KEY) {
+ res.status(401).json({ message: "Unauthorized" });
+ return;
+ }
+
const { key } = req.body as { key: string };
try {
@@ -47,6 +58,12 @@ export default async function handler(
return res.status(200).json({ url });
} catch (error) {
- return res.status(500).json({ error: "Internal server error" });
+ log({
+ message: `Error getting presigned get url for ${key} \n\n ${error}`,
+ type: "error",
+ });
+ return res
+ .status(500)
+ .json({ error: "AWS Cloudfront Signed URL Error", message: error });
}
}