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.

WIP presigned upload URLs

Tim Wilson258adf34974e
diff --git a/apps/web/app/api/workspaces/[idOrSlug]/import/csv/upload/route.ts b/apps/web/app/api/workspaces/[idOrSlug]/import/csv/upload/route.ts
new file mode 100644
index 0000000000..f22c9b3221
--- /dev/null
+++ b/apps/web/app/api/workspaces/[idOrSlug]/import/csv/upload/route.ts
@@ -0,0 +1,12 @@
+import { withWorkspace } from "@/lib/auth";
+import { storage } from "@/lib/storage";
+import { NextResponse } from "next/server";
+
+// POST /api/workspaces/[idOrSlug]/import/csv/upload – get a signed URL to upload a CSV file
+export const GET = withWorkspace(async ({ req, workspace, session }) => {
+ const id = new Date().getTime();
+ const url = await storage.getSignedUrl({
+ key: `workspaces/${workspace.id}/import/csv/${id}.csv`,
+ });
+ return NextResponse.json({ url, id });
+});
diff --git a/apps/web/lib/storage.ts b/apps/web/lib/storage.ts
index 24fdc3de15..b6af614097 100644
--- a/apps/web/lib/storage.ts
+++ b/apps/web/lib/storage.ts
@@ -53,6 +53,22 @@ class StorageClient {
}
}
+ async getSignedUrl({ key }: { key: string }) {
+ const url = new URL(`${process.env.STORAGE_ENDPOINT}/${key}`);
+
+ // 10 minutes expiration
+ url.searchParams.set("X-Amz-Expires", "600");
+
+ const signed = await this.client.sign(url.toString(), {
+ method: "PUT",
+ aws: {
+ signQuery: true,
+ },
+ });
+
+ return signed.url;
+ }
+
async delete(key: string) {
await this.client.fetch(`${process.env.STORAGE_ENDPOINT}/${key}`, {
method: "DELETE",
diff --git a/apps/web/ui/modals/import-csv-modal/index.tsx b/apps/web/ui/modals/import-csv-modal/index.tsx
index 9e566ef49e..af1d11f11b 100644
--- a/apps/web/ui/modals/import-csv-modal/index.tsx
+++ b/apps/web/ui/modals/import-csv-modal/index.tsx
@@ -164,6 +164,33 @@ function ImportCsvModal({
}
}
+ const res = await fetch(
+ `/api/workspaces/${workspaceId}/import/csv/upload`,
+ );
+
+ if (!res.ok) {
+ toast.error("Error getting signed upload URL");
+ return;
+ }
+
+ const { url } = await res.json();
+
+ const fileFormData = new FormData();
+ fileFormData.append("file", data.file!);
+
+ const uploadRes = await fetch(url, {
+ method: "PUT",
+ body: fileFormData,
+ headers: {
+ "Content-Type": "multipart/form-data",
+ },
+ });
+
+ console.log(uploadRes);
+ console.log(await uploadRes.json());
+
+ return;
+
toast.promise(
fetch(`/api/workspaces/${workspaceId}/import/csv`, {
method: "POST",