let CACHED_REPORTS = null; export default { async fetch(request, env) { const url = new URL(request.url); if (url.pathname === "/wq-admin") { return new Response(getAdminLoginPage(), { headers: { "Content-Type": "text/html; charset=utf-8" } }); } if (url.pathname === "/api/upload" && request.method === "POST") { const res = await handleUpload(request, env); if (res.ok) CACHED_REPORTS = null; return res; } if (url.pathname === "/api/delete" && request.method === "POST") { const res = await handleDelete(request, env); if (res.ok) CACHED_REPORTS = null; return res; } if (!CACHED_REPORTS) { CACHED_REPORTS = await fetchAllReportsFromGithub(env); } const isAdmin = url.searchParams.has('admin'); return new Response(getMainSiteHTML(isAdmin, CACHED_REPORTS), { headers: { "Content-Type": "text/html; charset=utf-8" } }); } }; // دالة جلب البيانات async function fetchAllReportsFromGithub(env) { const SECTIONS = ['أخبار الرياضيات', 'قسم الذكاء الاصطناعي', 'قسم نشر ابحاث الاعضاء', 'قسم الفيزياء', 'قسم الحوسبة']; let allData = []; const baseUrl = `https://raw.githubusercontent.com/${env.REPO_OWNER}/${env.REPO_NAME}/main`; for(let sec of SECTIONS) { for(let i=1; i<=30; i++) { try { const r = await fetch(`${baseUrl}/${encodeURI(sec)}/write/index${i}.html`); if(r.ok) { const text = await r.text(); const lines = text.split('\n').map(l=>l.trim()).filter(Boolean); allData.push({ id: i, section: sec, title: lines[0], description: lines[1] || '', content: lines.slice(2).join('\n'), image: `${baseUrl}/${encodeURI(sec)}/pic/pic${i}.jpg` }); } else break; } catch(e) { break; } } } return JSON.stringify(allData); } // دالة الرفع async function handleUpload(request, env) { const data = await request.json(); if (data.password !== env.ADMIN_PASSWORD) return new Response("Unauthorized", { status: 401 }); const githubApi = `https://api.github.com/repos/${env.REPO_OWNER}/${env.REPO_NAME}/contents`; const headers = { "Authorization": `token ${env.GITHUB_TOKEN}`, "User-Agent": "MathHub-Worker" }; try { const sectionPath = encodeURI(data.section); const res = await fetch(`${githubApi}/${sectionPath}/write`, { headers }); let nextId = 1; if (res.ok) { const files = await res.json(); nextId = files.length + 1; } // هنا ندمج اسم المؤلف المكتوب يدوياً const reportBody = `${data.title}\n${data.description}\nالمؤلف: ${data.author}\n${data.content}`; await fetch(`${githubApi}/${sectionPath}/write/index${nextId}.html`, { method: "PUT", headers, body: JSON.stringify({ message: "Add Report", content: btoa(unescape(encodeURIComponent(reportBody))) }) }); const imgData = data.imageBase64.split(',')[1]; await fetch(`${githubApi}/${sectionPath}/pic/pic${nextId}.jpg`, { method: "PUT", headers, body: JSON.stringify({ message: "Add Pic", content: imgData }) }); return new Response(JSON.stringify({ success: true })); } catch (e) { return new Response(e.message, { status: 500 }); } } // دالة الحذف async function handleDelete(request, env) { const data = await request.json(); if (data.password !== env.ADMIN_PASSWORD) return new Response("Unauthorized", { status: 401 }); const githubApi = `https://api.github.com/repos/${env.REPO_OWNER}/${env.REPO_NAME}/contents`; const headers = { "Authorization": `token ${env.GITHUB_TOKEN}`, "User-Agent": "MathHub-Worker" }; try { const getSha = async (p) => { const r = await fetch(`${githubApi}/${encodeURI(p)}`, { headers }); return r.ok ? (await r.json()).sha : null; }; const tSha = await getSha(`${data.section}/write/index${data.id}.html`); const pSha = await getSha(`${data.section}/pic/pic${data.id}.jpg`); if (tSha) await fetch(`${githubApi}/${encodeURI(data.section)}/write/index${data.id}.html`, { method: "DELETE", headers, body: JSON.stringify({ message: "Delete", sha: tSha })}); if (pSha) await fetch(`${githubApi}/${encodeURI(data.section)}/pic/pic${data.id}.jpg`, { method: "DELETE", headers, body: JSON.stringify({ message: "Delete", sha: pSha })}); return new Response(JSON.stringify({ success: true })); } catch (e) { return new Response(e.message, { status: 500 }); } } function getMainSiteHTML(isAdmin, preloadedData) { return `
يرجى إدخال رمز التحقق للوصول للوحة الإدارة