export default { async fetch(request, env) { const url = new URL(request.url); const path = url.pathname; if (!isPublicRequest(request, path)) { const isAuthorized = await checkAuthorization(request, env); if (!isAuthorized) { return new Response('Unauthorized', { status: 401 }); } } switch (path) { case '/': return await handleDefaultGet(env); case '/all': return await handleAllGet(env); case '/qr': return await handleQrGet(env); case '/manage': return await handleManageGet(env); case '/manage/upload': return await handleUpload(request, env); case '/manage/remove': return await handleRemove(request, env); case '/manage/replace': return await handleReplace(request, env); case '/manage/select': return await handleSelect(request, env); default: return new Response('Not found', { status: 404 }); } }, }; // Helper function to determine if a request is public function isPublicRequest(request, path) { return request.method === 'GET'; } // Helper function to check authorization async function checkAuthorization(request, env) { const authHeader = request.headers.get('X-Auth-Bearer'); const storedAuthKey = env.AUTH_KEY_SECRET; return authHeader === storedAuthKey; } async function handleDefaultGet(env) { const index = await getIndex(env); const selectedFile = index.find((file) => file.selected); return new Response(` Tutorium Resources

${selectedFile ? selectedFile.name : 'No file selected'}

Scan code to download

QR Code Download View All Files Discord
`, { headers: { 'Content-Type': 'text/html' } }); } // GET for "/all" async function handleAllGet(env) { const index = await getIndex(env); index.reverse(); const selectedFile = index.find((file) => file.selected); const listItems = index.map(file => `
  • ${file.name} - Uploaded on ${file.upload_date} Download QR Code
  • `).join(''); return new Response(` All Files - Tutorium Resources
    Back to Current
    `, { headers: { 'Content-Type': 'text/html' } }); } // GET for "/manage" async function handleManageGet(env) { const index = await getIndex(env); const tableRows = index.map((file, i) => ` ${file.name} `).join(''); return new Response(` Manage Files - Tutorium Resources
    ${tableRows}
    NameSelectedRemoveReplace
    `, { headers: { 'Content-Type': 'text/html' } }); } async function handleQrGet(env) { const index = await getIndex(env); const selectedFile = index.find((file) => file.selected); return new Response(` QR Code - Tutorium Resources
    QR Code
    `, { headers: { 'Content-Type': 'text/html' } }); } // POST for "/manage/select" async function handleSelect(request, env) { const { filename } = await request.json(); const index = await getIndex(env); // Mark the selected file and unselect others index.forEach((file) => { file.selected = (file.filename === filename); }); await updateIndex(env, index); return new Response('Selected file updated successfully'); } // POST for "/manage/upload" async function handleUpload(request, env) { const formData = await request.formData(); const file = formData.get('file'); const displayName = formData.get('displayName'); // Validate file and displayName if (!file || !displayName) { return new Response('File and display name are required', { status: 400 }); } const filename = `${Date.now()}_${file.name}`; await env.CDN_BUCKET.put(`uni/ws2425/tutorien/epr/loesungen/${filename}`, file.stream()); // Update index.json with the new file const index = await getIndex(env); index.push({ name: displayName, upload_date: new Date().toISOString(), filename, selected: false }); await updateIndex(env, index); return new Response('File uploaded', { status: 200 }); } // POST for "/manage/remove" async function handleRemove(request, env) { const { filename } = await request.json(); await env.CDN_BUCKET.delete(`uni/ws2425/tutorien/epr/loesungen/${filename}`); const index = await getIndex(env); const updatedIndex = index.filter(file => file.filename !== filename); await updateIndex(env, updatedIndex); return new Response('File removed successfully'); } // POST for "/manage/replace" async function handleReplace(request, env) { const formData = await request.formData(); const newFile = formData.get('newFile'); const filename = formData.get('filename'); await env.CDN_BUCKET.put(`uni/ws2425/tutorien/epr/loesungen/${filename}`, newFile.stream()); return new Response('File replaced successfully'); } // Helper functions async function getIndex(env) { const indexObj = await env.CDN_BUCKET.get('uni/ws2425/tutorien/epr/index.json'); return indexObj ? JSON.parse(await indexObj.text()) : []; } async function updateIndex(env, index) { await env.CDN_BUCKET.put('uni/ws2425/tutorien/epr/index.json', JSON.stringify(index)); }