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.

1.4 KB
Raw Blame
import { withWorkspace } from "@/lib/auth";
import { createSignedUploadUrl } from "@/lib/storage/create-signed-upload-url";
import { signedUploadInputSchema } from "@/lib/storage/schemas";
import { validateSignedUpload } from "@/lib/storage/validate-signed-upload";
import { assertRateLimit } from "@/lib/upstash/assert-rate-limit";
import { RATELIMIT_POLICIES } from "@/lib/upstash/ratelimit-policies";
import { nanoid } from "@dub/utils";
import { NextResponse } from "next/server";
import * as z from "zod/v4";

const schema = signedUploadInputSchema.extend({
  folder: z.enum(["integrationScreenshots"]),
});

// POST /api/workspaces/[idOrSlug]/upload-url – get a signed URL to upload a file to a workspace
export const POST = withWorkspace(
  async ({ req, workspace, session }) => {
    const {
      folder: policy,
      contentType,
      contentLength,
    } = schema.parse(await req.json());

    const key = `integration-screenshots/${nanoid(10)}`;

    validateSignedUpload({
      contentLength,
      contentType,
      policy,
    });

    await assertRateLimit({
      policy: RATELIMIT_POLICIES.workspaceFileUpload,
      identifier: [workspace.id, session.user.id],
    });

    const { signedUrl, destinationUrl } = await createSignedUploadUrl({
      key,
      contentType,
      contentLength,
    });

    return NextResponse.json({
      key,
      signedUrl,
      destinationUrl,
    });
  },
  {
    requiredRoles: ["owner", "member"],
  },
);