Skip to content

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

improvement

Ashish Viradiya9d40d2e3a474
diff --git a/components/documents/add-document-modal.tsx b/components/documents/add-document-modal.tsx
index 13ef50ac..818de93f 100644
--- a/components/documents/add-document-modal.tsx
+++ b/components/documents/add-document-modal.tsx
@@ -1,14 +1,20 @@
import Link from "next/link";
import { useRouter } from "next/router";
+
+
import { FormEvent, useEffect, useState } from "react";
+
+
import { useTeam } from "@/context/team-context";
import { PlanEnum } from "@/ee/stripe/constants";
import { parsePageId } from "notion-utils";
import { toast } from "sonner";
import { mutate } from "swr";
+
+
import { useAnalytics } from "@/lib/analytics";
import {
DocumentData,
@@ -259,6 +265,7 @@ export function AddDocumentModal({
}
if (!newVersion) {
+ mutate(`/api/teams/${teamId}/documents`);
toast.success("Document uploaded. Redirecting to document page...");
analytics.capture("Document Added", {
@@ -642,4 +649,4 @@ export function AddDocumentModal({
)}
</>
);
-}
+}
\ No newline at end of file
diff --git a/lib/files/get-file.ts b/lib/files/get-file.ts
index 3504f2ed..44fd71aa 100644
--- a/lib/files/get-file.ts
+++ b/lib/files/get-file.ts
@@ -27,57 +27,61 @@ export const getFile = async ({
return url;
};
-const getFileFromS3 = async (key: string) => {
- 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 }),
- },
- );
+const fetchPresignedUrl = async (
+ endpoint: string,
+ headers: Record<string, string>,
+ key: string,
+): Promise<string> => {
+ const response = await fetch(endpoint, {
+ method: "POST",
+ headers,
+ body: JSON.stringify({ key }),
+ });
- if (!response.ok) {
+ if (!response.ok) {
+ const contentType = response.headers.get("content-type");
+ let errorMessage: string;
+
+ if (contentType && contentType.includes("application/json")) {
try {
const error = await response.json();
- throw new Error(
- error.message || `Request failed with status ${response.status}`,
- );
+ errorMessage = 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 textError = await response.text();
+ errorMessage = textError || `Request failed with status ${response.status}`;
}
+ } else {
+ const textError = await response.text();
+ errorMessage = textError || `Request failed with status ${response.status}`;
}
- const { url } = (await response.json()) as { url: string };
- return url;
+ throw new Error(errorMessage);
+ }
+
+ const { url } = (await response.json()) as { url: string };
+ return url;
+};
+
+const getFileFromS3 = async (key: string) => {
+ const isServer = typeof window === 'undefined' && !!process.env.INTERNAL_API_KEY;
+
+ if (isServer) {
+ return fetchPresignedUrl(
+ `${process.env.NEXT_PUBLIC_BASE_URL}/api/file/s3/get-presigned-get-url`,
+ {
+ "Content-Type": "application/json",
+ Authorization: `Bearer ${process.env.INTERNAL_API_KEY}`,
+ },
+ key,
+ );
} else {
- const response = await fetch(`/api/file/s3/get-presigned-get-url-proxy`, {
- method: "POST",
- headers: {
+ return fetchPresignedUrl(
+ `/api/file/s3/get-presigned-get-url-proxy`,
+ {
"Content-Type": "application/json",
},
- 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) {
- throw new Error(`Request failed with status ${response.status}`);
- }
- }
-
- const { url } = (await response.json()) as { url: string };
- return url;
+ key,
+ );
}
};
diff --git a/pages/api/file/s3/get-presigned-get-url-proxy.ts b/pages/api/file/s3/get-presigned-get-url-proxy.ts
index 34daf7f8..e5509483 100644
--- a/pages/api/file/s3/get-presigned-get-url-proxy.ts
+++ b/pages/api/file/s3/get-presigned-get-url-proxy.ts
@@ -36,7 +36,20 @@ export default async function handler(
);
if (!response.ok) {
- const error = await response.json();
+ const contentType = response.headers.get("content-type");
+ let error: any;
+
+ if (contentType && contentType.includes("application/json")) {
+ try {
+ error = await response.json();
+ } catch (parseError) {
+ error = { message: await response.text() || `Request failed with status ${response.status}` };
+ }
+ } else {
+ const textError = await response.text();
+ error = { message: textError || `Request failed with status ${response.status}` };
+ }
+
return res.status(response.status).json(error);
}