export default { async fetch(request, env) { const url = new URL(request.url); const path = url.pathname; const method = request.method; // CORS Headers const corsHeaders = { "Access-Control-Allow-Origin": "*", "Access-Control-Allow-Methods": "GET,HEAD,POST,OPTIONS,PUT,DELETE", "Access-Control-Allow-Headers": "Content-Type, X-Admin-Password", }; if (method === "OPTIONS") return new Response(null, { headers: corsHeaders }); // মিডিয়া ফাইল দেখানোর জন্য if (path.startsWith("/media/") && method === "GET") { const key = path.replace("/media/", ""); const object = await env.BUCKET.get(key); if (!object) return new Response("Not found", { status: 404, headers: corsHeaders }); const headers = new Headers(corsHeaders); object.writeHttpMetadata(headers); return new Response(object.body, { headers }); } // পাসওয়ার্ড ভেরিফিকেশন const isAuth = request.headers.get("X-Admin-Password") === env.ADMIN_PASSWORD; if (path === "/api/login" && method === "POST") { if (isAuth) return new Response(JSON.stringify({ success: true }), { headers: corsHeaders }); return new Response(JSON.stringify({ error: "ভুল পাসওয়ার্ড" }), { status: 401, headers: corsHeaders }); } const getPosts = async () => { const obj = await env.BUCKET.get("posts.json"); return obj ? await obj.json() : []; }; const savePosts = async (posts) => await env.BUCKET.put("posts.json", JSON.stringify(posts)); // পোস্ট লোড করা if (path === "/api/posts" && method === "GET") { return new Response(JSON.stringify(await getPosts()), { headers: { ...corsHeaders, "Content-Type": "application/json" } }); } if (!isAuth) return new Response(JSON.stringify({ error: "Unauthorized" }), { status: 401, headers: corsHeaders }); // নতুন পোস্ট আপলোড if (path === "/api/posts" && method === "POST") { try { const fd = await request.formData(); const id = Date.now().toString(36); let key = "", thumbKey = "", type = "image"; const file = fd.get("file"); if (file && file.name) { key = id + "_" + file.name.replace(/\s+/g, "_"); type = file.type.startsWith("video/") ? "video" : "image"; await env.BUCKET.put(key, file.stream(), { httpMetadata: { contentType: file.type } }); } const thumb = fd.get("thumb"); if (thumb && thumb.name) { thumbKey = "thumb_" + id + "_" + thumb.name.replace(/\s+/g, "_"); await env.BUCKET.put(thumbKey, thumb.stream(), { httpMetadata: { contentType: thumb.type } }); } const newPost = { id, caption: fd.get("title") || "", category: fd.get("category") || "", tags: fd.get("tags") ? fd.get("tags").split(",").map(t=>t.trim()) : [], views: 0, type, key, thumbKey, createdAt: Date.now() }; const posts = await getPosts(); posts.unshift(newPost); await savePosts(posts); return new Response(JSON.stringify(newPost), { headers: corsHeaders }); } catch (e) { return new Response(JSON.stringify({ error: e.message }), { status: 500, headers: corsHeaders }); } } // পোস্ট ডিলিট if (path.startsWith("/api/posts/") && method === "DELETE") { const id = path.split("/").pop(); let posts = await getPosts(); const post = posts.find(p => p.id === id); if (post) { if (post.key) await env.BUCKET.delete(post.key); if (post.thumbKey) await env.BUCKET.delete(post.thumbKey); posts = posts.filter(p => p.id !== id); await savePosts(posts); } return new Response(JSON.stringify({ success: true }), { headers: corsHeaders }); } return new Response("Not Found", { status: 404, headers: corsHeaders }); } };