Skip to content

Open-source manufacturing ERP, MES and QMS. Quoting, MRP, inventory, shop floor, quality and lot/serial traceability on one Postgres schema, with a REST API and MCP server. Self-host or use Carbon Cloud.

feat: add edge function to get signed url with api key

Brad Barbin92898e836b71
diff --git a/packages/database/supabase/functions/download/index.ts b/packages/database/supabase/functions/download/index.ts
new file mode 100644
index 0000000000..92787264e1
--- /dev/null
+++ b/packages/database/supabase/functions/download/index.ts
@@ -0,0 +1,71 @@
+import { serve } from "https://deno.land/[email protected]/http/server.ts";
+import { z } from "https://deno.land/x/[email protected]/mod.ts";
+
+import { corsHeaders } from "../lib/headers.ts";
+import { getSupabaseServiceRole } from "../lib/supabase.ts";
+
+const downloadValidator = z.object({
+ bucket: z.string(),
+ path: z.string(),
+ companyId: z.string(),
+});
+
+serve(async (req: Request) => {
+ if (req.method === "OPTIONS") {
+ return new Response("ok", { headers: corsHeaders });
+ }
+ const payload = await req.json();
+
+ try {
+ const validatedPayload = downloadValidator.parse(payload);
+ const { bucket, path, companyId } = validatedPayload;
+
+ console.log({
+ function: "download",
+ bucket,
+ path,
+ companyId,
+ });
+
+ // verify that the request is authorized by an API key or service role
+ const serviceRole = await getSupabaseServiceRole(
+ req.headers.get("Authorization"),
+ req.headers.get("carbon-key") ?? "",
+ companyId
+ );
+
+ const signedUrl = await serviceRole.storage
+ .from(bucket)
+ .createSignedUrl(path, 60);
+
+ if (signedUrl.error) {
+ return new Response(
+ JSON.stringify({
+ success: false,
+ error: signedUrl.error.message,
+ }),
+ {
+ headers: { ...corsHeaders, "Content-Type": "application/json" },
+ status: 404,
+ }
+ );
+ }
+
+ return new Response(
+ JSON.stringify({
+ success: true,
+ signedUrl: signedUrl.data?.signedUrl,
+ }),
+ {
+ headers: { ...corsHeaders, "Content-Type": "application/json" },
+ status: 200,
+ }
+ );
+ } catch (err) {
+ console.error(err);
+ return new Response(JSON.stringify(err), {
+ headers: { ...corsHeaders, "Content-Type": "application/json" },
+ status: 500,
+ });
+ }
+});