Skip to content

Instantly share code, notes, and snippets.

@Zxilly
Created March 28, 2024 12:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Zxilly/9273aa5909f539db3f0dc8736aaab9bd to your computer and use it in GitHub Desktop.
Save Zxilly/9273aa5909f539db3f0dc8736aaab9bd to your computer and use it in GitHub Desktop.
wrangler bug
import { Router } from '@tsndr/cloudflare-worker-router';
import { md5 } from 'js-md5';
export interface Env {
IMG_BUCKET: R2Bucket;
}
const router = new Router<Env>();
router.put('/:name', async (ctx) => {
const name = ctx.req.params.name;
const hash = md5.create().update(name).hex();
const bucket = ctx.env.IMG_BUCKET;
await bucket.put(hash, ctx.req.raw.body);
const url = new URL(ctx.req.url);
url.pathname = `/${hash}`;
return new Response(url.toString(), { status: 201 });
});
export default {
async fetch(request: Request, env: Env, ctx: ExecutionContext) {
return router.handle(request, env, ctx);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment