Last active
July 7, 2022 03:09
-
-
Save Code-Hex/530458c4b4d54844100d80ae5e831e4a to your computer and use it in GitHub Desktop.
Revisions
-
Code-Hex revised this gist
Jul 6, 2022 . 2 changed files with 4 additions and 7 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,7 +1,7 @@ { "name": "hn-image-workers", "version": "1.0.0", "description": "cloudflare r2 image workers for hacker news", "main": "dist/index.js", "author": "Kei Kamikawa <[email protected]>", "license": "MIT", @@ -15,8 +15,5 @@ "miniflare": "^2.5.1", "wrangler": "^2.0.16" }, "dependencies": {} } This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,4 @@ name = "hn-image-workers" compatibility_date = "2022-07-06" [[r2_buckets]] -
Code-Hex created this gist
Jul 6, 2022 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,55 @@ // Service Worker Syntax で書いてます // addEventListener('fetch', event => { // event.respondWith(handleRequest(event.request)); // }); // Module Worker Syntax で書いてます // async function handleRequest(request) { // return new Response('Hello worker!', { // headers: { 'content-type': 'text/plain' }, // }); // } // ここからは R2 への画像アップロード interface Env { CODEHEX_BUCKET: R2Bucket } export default { async fetch(request: Request, env: Env) { const url = new URL(request.url) const path = url.pathname if (path === "/upload") { const formdata = await request.formData() const imagedata = formdata.get("imagedata") if (imagedata === null) { throw new Error("not found imagedata") } const file = imagedata as File const obj = await env.CODEHEX_BUCKET.put("image", file, { httpMetadata: { contentType: "image/png" } }) return new Response(`key: ${obj.key}!!`, { headers: { 'content-type': 'text/plain' }, }) } if (path === "/image") { const obj = await env.CODEHEX_BUCKET.get("image") if (obj === null) { throw new Error("upload してー") } return new Response(obj.body) } return new Response('Not found', { status: 404, headers: { 'content-type': 'text/plain' }, }); } } This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,22 @@ { "name": "hc-image-workers", "version": "1.0.0", "description": "cloudflare r2 image workers for hacker channel", "main": "dist/index.js", "author": "Kei Kamikawa <[email protected]>", "license": "MIT", "scripts": { "dev": "wrangler dev ./src/index.ts", "deploy": "wrangler publish ./src/index.ts" }, "devDependencies": { "@cloudflare/workers-types": "^3.14.0", "@tsconfig/recommended": "^1.0.1", "miniflare": "^2.5.1", "wrangler": "^2.0.16" }, "dependencies": { "firebase-auth-cloudflare-workers": "^1.0.0", "hono": "^1.5.1" } } This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,12 @@ { "extends": "@tsconfig/recommended/tsconfig.json", "compilerOptions": { "lib": ["es2022"], "target": "es2022", "forceConsistentCasingInFileNames": true, "moduleResolution": "node", "types": [ "@cloudflare/workers-types" ] } } This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,6 @@ name = "hc-image-workers" compatibility_date = "2022-07-06" [[r2_buckets]] binding = "CODEHEX_BUCKET" bucket_name = "codehex-bucket"