Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
- const url = 'https://cf-paste.walshy.dev/';
- addEventListener('fetch', event => {
- try {
- event.respondWith(handleRequest(event.request))
- } catch (e) {
- return new Response(JSON.stringify(e), { 'Content-Type': 'application/json' });
- }
- })
- async function handleRequest(request) {
- const { pathname } = new URL(request.url);
- if (pathname === '' || pathname === '/') {
- return new Response('Send a POST to /documents in order to paste!');
- } else if (pathname === '/documents' && request.method === 'POST') {
- const content = await request.text();
- if (content.trim().length === 0) {
- return new Response('{"error": "Please provide a body!"}', { 'Content-Type': 'application/json' });
- }
- const id = getRandomString();
- await KV.put(id, content, { expirationTtl: 86400 });
- return new Response(JSON.stringify({
- key: id, url: url + id,
- note: 'It may take up to 60 seconds for the paste to become available!'
- }), { 'Content-Type': 'application/json'});
- } else {
- const data = await KV.get(pathname.substring(1));
- if (data === null) {
- return new Response('Not found!', { status: 404 });
- } else {
- return new Response(data);
- }
- }
- function getRandomString() {
- // Pick characers randomly
- let str = '';
- for (let i = 0; i < 12; i++) {
- str += chars.charAt(Math.floor(Math.random() * chars.length));
- }
- return str;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement