landingpage
This commit is contained in:
@@ -0,0 +1,68 @@
|
||||
import { createAdminClient } from "npm:@insforge/sdk";
|
||||
|
||||
const corsHeaders = {
|
||||
"Access-Control-Allow-Origin": "*",
|
||||
"Access-Control-Allow-Methods": "POST, OPTIONS",
|
||||
"Access-Control-Allow-Headers": "Content-Type, Authorization",
|
||||
};
|
||||
|
||||
export default async function (req: Request): Promise<Response> {
|
||||
if (req.method === "OPTIONS") {
|
||||
return new Response(null, { status: 204, headers: corsHeaders });
|
||||
}
|
||||
|
||||
if (req.method !== "POST") {
|
||||
return new Response(JSON.stringify({ error: "Method not allowed" }), {
|
||||
status: 405,
|
||||
headers: { ...corsHeaders, "Content-Type": "application/json" },
|
||||
});
|
||||
}
|
||||
|
||||
try {
|
||||
const { name, contentType, base64 } = await req.json();
|
||||
|
||||
if (!base64 || typeof base64 !== "string") {
|
||||
return new Response(JSON.stringify({ error: "Missing base64 file" }), {
|
||||
status: 400,
|
||||
headers: { ...corsHeaders, "Content-Type": "application/json" },
|
||||
});
|
||||
}
|
||||
|
||||
const bytes = Uint8Array.from(atob(base64), (c) => c.charCodeAt(0));
|
||||
const file = new File([bytes], name || "gallery.jpg", {
|
||||
type: contentType || "image/jpeg",
|
||||
});
|
||||
|
||||
const admin = createAdminClient({
|
||||
baseUrl: Deno.env.get("INSFORGE_BASE_URL")!,
|
||||
apiKey: Deno.env.get("INSFORGE_API_KEY")!,
|
||||
});
|
||||
|
||||
const { data, error } = await admin.storage
|
||||
.from("gallery")
|
||||
.uploadAuto(file);
|
||||
|
||||
if (error || !data) {
|
||||
return new Response(
|
||||
JSON.stringify({ error: error?.message || "Upload failed" }),
|
||||
{
|
||||
status: 500,
|
||||
headers: { ...corsHeaders, "Content-Type": "application/json" },
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
return new Response(JSON.stringify({ url: data.url, key: data.key }), {
|
||||
status: 200,
|
||||
headers: { ...corsHeaders, "Content-Type": "application/json" },
|
||||
});
|
||||
} catch (err) {
|
||||
return new Response(
|
||||
JSON.stringify({ error: err instanceof Error ? err.message : "Unknown error" }),
|
||||
{
|
||||
status: 500,
|
||||
headers: { ...corsHeaders, "Content-Type": "application/json" },
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user