Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

upload and display the images

Kiran K4cbb844109a7
diff --git a/apps/web/app/api/oauth/apps/screenshots/upload/route.ts b/apps/web/app/api/oauth/apps/screenshots/upload/route.ts
new file mode 100644
index 0000000000..98a068122a
--- /dev/null
+++ b/apps/web/app/api/oauth/apps/screenshots/upload/route.ts
@@ -0,0 +1,25 @@
+import { withWorkspace } from "@/lib/auth";
+import { storage } from "@/lib/storage";
+import z from "@/lib/zod";
+import { nanoid } from "@dub/utils";
+import { NextResponse } from "next/server";
+
+// TODO:
+// How do we restrict this to only be called by the integration form?
+
+const schema = z.object({
+ name: z.string().min(1).max(100),
+});
+
+// POST /api/oauth/apps/signed-url – get a signed URL to upload an integration screenshot
+export const GET = withWorkspace(async ({ searchParams }) => {
+ const { name } = schema.parse(searchParams);
+
+ const key = `integration-screenshots/${nanoid(7)}/${name}`;
+
+ const signedUrl = await storage.getSignedUrl({
+ key,
+ });
+
+ return NextResponse.json({ key, signedUrl });
+});
diff --git a/apps/web/app/api/oauth/apps/upload-screenshot/route.ts b/apps/web/app/api/oauth/apps/upload-screenshot/route.ts
deleted file mode 100644
index dc7658aca3..0000000000
--- a/apps/web/app/api/oauth/apps/upload-screenshot/route.ts
+++ /dev/null
@@ -1,19 +0,0 @@
-import { withWorkspace } from "@/lib/auth";
-import { storage } from "@/lib/storage";
-import { nanoid } from "@dub/utils";
-import { NextResponse } from "next/server";
-
-// POST /api/oauth/apps/upload-screenshot – get a signed URL to upload an integration screenshot
-export const GET = withWorkspace(async () => {
- // TODO:
- // How do we restrict this to only be called by the integration form?
- // Should we move to more generic endpoint `/api/storage/signed-url`?
-
- const key = `integration-screenshots/${nanoid(7)}`;
-
- const signedUrl = await storage.getSignedUrl({
- key,
- });
-
- return NextResponse.json({ key, signedUrl });
-});
diff --git a/apps/web/lib/storage.ts b/apps/web/lib/storage.ts
index bec3f59f26..9ebe1ca93c 100644
--- a/apps/web/lib/storage.ts
+++ b/apps/web/lib/storage.ts
@@ -71,6 +71,7 @@ class StorageClient {
method: "PUT",
aws: {
signQuery: true,
+ allHeaders: true,
},
});
diff --git a/apps/web/next.config.js b/apps/web/next.config.js
index d59318eb8e..fc667400ee 100644
--- a/apps/web/next.config.js
+++ b/apps/web/next.config.js
@@ -45,6 +45,9 @@ module.exports = withAxiom({
{
hostname: "dubassets.com", // for Dub's user generated images
},
+ {
+ hostname: "dev.dubassets.com", // dev bucket
+ },
{
hostname: "www.google.com",
},
diff --git a/apps/web/ui/integrations/add-edit-integration-form.tsx b/apps/web/ui/integrations/add-edit-integration-form.tsx
index c60dac23a8..c0ab598dde 100644
--- a/apps/web/ui/integrations/add-edit-integration-form.tsx
+++ b/apps/web/ui/integrations/add-edit-integration-form.tsx
@@ -144,49 +144,42 @@ export default function AddEditIntegrationForm({
setScreenshots((prev) => [...prev, { file, uploading: true }]);
const response = await fetch(
- `/api/oauth/apps/upload-screenshot?name=${file.name}&size=${file.size}&workspaceId=${workspaceId}`,
+ `/api/oauth/apps/screenshots/upload?workspaceId=${workspaceId}&name=${file.name}`,
{
method: "GET",
},
);
- const { signedUrl, key } = await response.json();
-
- const form = new FormData();
-
- form.append("file", file);
+ if (!response.ok) {
+ toast.error("Failed to get signed URL for screenshot upload.");
+ return;
+ }
- // Fake the upload
- setScreenshots((prev) =>
- prev.map((screenshot) =>
- screenshot.file === file
- ? { ...screenshot, uploading: false, key: key }
- : screenshot,
- ),
- );
+ const { signedUrl, key } = await response.json();
const uploadResponse = await fetch(signedUrl, {
method: "PUT",
- body: form,
+ body: file,
headers: {
- "Content-Type": "multipart/form-data",
+ "Content-Type": file.type,
+ "Content-Length": file.size.toString(),
},
});
- if (uploadResponse.ok) {
- toast.success("Screenshot uploaded!");
-
- setScreenshots((prev) =>
- prev.map((screenshot) =>
- screenshot.file === file
- ? { ...screenshot, uploading: false, key: key }
- : screenshot,
- ),
- );
- } else {
+ if (!uploadResponse.ok) {
const result = await uploadResponse.json();
toast.error(result.error.message || "Failed to upload screenshot.");
+ return;
}
+
+ toast.success(`${file.name} uploaded!`);
+ setScreenshots((prev) =>
+ prev.map((screenshot) =>
+ screenshot.file === file
+ ? { ...screenshot, uploading: false, key }
+ : screenshot,
+ ),
+ );
};
const {
@@ -204,6 +197,8 @@ export default function AddEditIntegrationForm({
const buttonDisabled =
!name || !slug || !developer || !website || !redirectUris;
+ const uploading = screenshots.some((s) => s.uploading);
+
return (
<>
<form
@@ -477,7 +472,7 @@ export default function AddEditIntegrationForm({
<Button
text={integration ? "Save changes" : "Create"}
- disabled={buttonDisabled}
+ disabled={buttonDisabled || uploading}
loading={saving}
type="submit"
/>