Skip to content

Instantly share code, notes, and snippets.

@dustinrouillard
Last active July 12, 2021 23:43
Show Gist options
  • Save dustinrouillard/dc6331d8466be6904fd413d34144e633 to your computer and use it in GitHub Desktop.
Save dustinrouillard/dc6331d8466be6904fd413d34144e633 to your computer and use it in GitHub Desktop.
Cloudflare worker which runs fax.pics, flax.pics, burmese.pics, siamese.pics, marley.pics, and kush.pics
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request))
});
const start_images = 1;
const end_images = 47;
const image_folder = 'marley';
const default_image = `https://dustin.wtf/${image_folder}/1.jpg`;
async function fetchImage() {
const imageUrl = `https://dustin.wtf/${image_folder}/${Math.floor(Math.random() * (end_images - start_images) + start_images)}.jpg`;
let imageFile = await fetch(imageUrl);
let type = imageFile.headers.get('content-type');
if (type.includes('text/plain')) {
imageUrl = default_image;
imageFile = await fetch(default_image);
type = imageFile.headers.get('content-type');
}
let { readable, writable } = new TransformStream();
imageFile.body.pipeTo(writable);
return { url: imageUrl, file: readable, type };
}
async function handleRequest(request) {
const { url, file, type } = await fetchImage();
return new Response(file, {
headers: {
'x-original-url': url,
'content-type': type
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment