Created
March 28, 2024 12:09
-
-
Save Zxilly/9273aa5909f539db3f0dc8736aaab9bd to your computer and use it in GitHub Desktop.
wrangler bug
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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