added many features lol

This commit is contained in:
J-onasJones 2024-06-18 19:41:20 +02:00
parent 515ad0b7e5
commit af9eccb50d

View file

@ -1,10 +1,24 @@
addEventListener('fetch', event => { addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request)); event.respondWith(handleRequest(event.request, event.env));
}); });
async function handleRequest(request) { const headersCORS = {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET, POST, DELETE, OPTIONS',
'Access-Control-Allow-Headers': 'Content-Type, x-Custom-Auth-Key',
};
async function handleRequest(request, env) {
const url = new URL(request.url); const url = new URL(request.url);
if (request.method === 'OPTIONS') {
// Handle CORS preflight request
return new Response(null, {
status: 204,
headers: headersCORS
});
}
// Check if the path is /blog // Check if the path is /blog
if (url.pathname === '/blog') { if (url.pathname === '/blog') {
// Fetch the index.json file from your CDN // Fetch the index.json file from your CDN
@ -18,11 +32,147 @@ addEventListener('fetch', event => {
return new Response(rssFeed, { return new Response(rssFeed, {
headers: { 'Content-Type': 'application/rss+xml' }, headers: { 'Content-Type': 'application/rss+xml' },
}); });
} else if (url.pathname === '/blog/new_post') {
if (request.method === 'POST') {
if (!hasValidHeader(request)) {
return new Response('Unauthorized', { status: 401, headers: headersCORS});
}
try {
const formData = await request.formData();
const formFields = {};
for (const [key, value] of formData.entries()) {
formFields[key] = value;
}
const fileName = formFields['id'] + '.md';
const posttitle = formFields['title'];
const postauthor = formFields['author'];
const postdate = formFields['date'];
const postdescription = formFields['description'];
const year = postdate.split('-')[0];
const month = postdate.split('-')[1];
const day = postdate.split('-')[2].split(' ')[0];
const fileContent = new TextEncoder().encode(
`[title]: ${posttitle}
[author]: ${postauthor}
[date]: ${postdate}
[description]: ${postdescription}
` + formFields['content']);
const response = await fetch('https://cdn.jonasjones.dev/blog/index.json');
const data = await response.json();
// add new json object to data
const postObj = {
id: formFields['id'],
date: formFields['date'],
title: formFields['title'],
author: formFields['author'],
description: formFields['description']
};
// Check if the object with the given id already exists
const index = data.findIndex(item => item.id === postObj.id);
if (index !== -1) {
// If it exists, update the existing object
data[index] = postObj;
} else {
// If it does not exist, add the new JSON object to the list
data.push(postObj);
}
// Access the R2 bucket using the binding
try {
await CDN_BUCKET.put(`blog/posts/${year}/${month}/${day}/${fileName}`, fileContent, {
httpMetadata: {
contentType: "text/markdown"
},
headers: headersCORS
});
await CDN_BUCKET.put(`blog/index.json`, JSON.stringify(data), {
httpMetadata: {
contentType: "application/json"
}
});
// If the put operation succeeds (no error is thrown), return a success response
return new Response(`Post Successfully uploaded`, { status: 200 });
} catch (error) {
// If an error occurs during the put operation, return a failure response
return new Response(`Failed to upload Post: ${error.message}`, { status: 500 });
}
} catch (e) {
return new Response(e, { status: 400 });
}
} else {
return new Response('Method Not Allowed', { status: 405 });
}
} else if (url.pathname === "/blog/delete_post") {
if (request.method === 'DELETE') {
if (!hasValidHeader(request)) {
return new Response('Unauthorized', { status: 401 });
}
try {
const formData = await request.formData();
const formFields = {};
for (const [key, value] of formData.entries()) {
formFields[key] = value;
}
const fileName = formFields['id'] + '.md';
const response = await fetch('https://cdn.jonasjones.dev/blog/index.json');
const data = await response.json();
// Check if the object with the given id already exists
const index = data.findIndex(item => item.id === formFields['id']);
if (index !== -1) {
// If it exists, delete the existing object
data.splice(index, 1);
}
// delete the file from the bucket
CDN_BUCKET.delete(`blog/posts/${fileName}`);
// update the index.json file
await CDN_BUCKET.put(`blog/index.json`, JSON.stringify(data), {
httpMetadata: {
contentType: "application/json"
}
});
return new Response(`Post Successfully deleted`, { status: 200 });
} catch (e) {
return new Response('Failed to delete Post', { status: 500 });
}
}
return new Response('Method Not Allowed', { status: 405 });
} else { } else {
// Handle other routes or return a 404 response // Handle other routes or return a 404 response
return new Response('Not Found', { status: 404 }); return new Response('Not Found', { status: 404 });
} }
} };
const hasValidHeader = (request) => {
return request.headers.get('X-Custom-Auth-Key') === AUTH_KEY_SECRET;
};
function createRSSFeed(data) { function createRSSFeed(data) {
const DOMAIN = 'https://blog.jonasjones.dev'; const DOMAIN = 'https://blog.jonasjones.dev';