From cf9fe092d6e282bc32b135c3ad86fafcf3f8e6c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=AB=8F=E8=A8=AA=E5=AD=90?= Date: Wed, 17 May 2023 09:50:17 +0900 Subject: [PATCH] =?UTF-8?q?=E6=9C=80=E5=88=9D=E3=82=B3=E3=83=9F=E3=83=83?= =?UTF-8?q?=E3=83=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 + go.mod | 3 +++ main.go | 42 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 46 insertions(+) create mode 100644 .gitignore create mode 100644 go.mod create mode 100644 main.go diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..aa871a5 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +imgproxy diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..e5de6c3 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module imgproxy + +go 1.20 diff --git a/main.go b/main.go new file mode 100644 index 0000000..82ebcde --- /dev/null +++ b/main.go @@ -0,0 +1,42 @@ +package main + +import ( + "fmt" + "net/http" + "io" + "log" +) + +var client = http.Client{} + +func imgproxy (w http.ResponseWriter, r *http.Request) { + // r.URL.Pathは「/」で始まるから、「https://」じゃなくて、「https:/」となります。 + img, err := client.Get("https:/" + r.URL.Path) + if err != nil { + fmt.Fprintf(w, "Error %d", err) + return + } + + // 画像だけをプロクシーする様に + ctype := img.Header.Get("Content-Type") + if ctype != "image/jpeg" && ctype != "image/png" && ctype != "image/gif" && ctype != "image/tga" && ctype != "image/targa" && ctype != "image/webp" { + return + } + + // ヘッダー + w.Header().Set("Content-Length", fmt.Sprint(img.ContentLength)) + w.Header().Set("Content-Type", img.Header.Get("Content-Type")) + + // 表示 + if _, err = io.Copy(w, img.Body); err != nil { + log.Fatalf("ioエラー:%v", err) + } + + // もう要らない + img.Body.Close() +} + +func main () { + http.HandleFunc("/", imgproxy) + http.ListenAndServe(":9810", nil) +}