From 2e6613854b4d49c371a8cccb9433dee24fdac697 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=AB=98=E6=A9=8B=20=E6=8B=93=E4=B9=9F?= Date: Thu, 10 Aug 2023 13:30:03 +0900 Subject: [PATCH 01/15] add webp ext conv --- main.go | 47 ++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 42 insertions(+), 5 deletions(-) diff --git a/main.go b/main.go index db685a5..1c55c56 100644 --- a/main.go +++ b/main.go @@ -9,6 +9,7 @@ import ( "net/http" "net/url" "os" + "path/filepath" "runtime/debug" "strconv" "syscall" @@ -19,6 +20,7 @@ var client http.Client var orgSrvURL string var quality = 90 var version = "" +var convExtCandidates = []string{".jpg", ".png", ".jpeg"} func main() { var ver bool @@ -77,12 +79,47 @@ func proxy(w http.ResponseWriter, r *http.Request) { if len(xff) > 1 { req.Header.Set("X-Forwarded-For", xff) } + var orgRes *http.Response + pathExt := filepath.Ext(req.URL.Path) + if pathExt == ".webp" { + for _, cExt := range convExtCandidates { - orgRes, err := client.Do(req) - if err != nil { - http.Error(w, "Get origin failed", http.StatusBadGateway) - log.Printf("Get origin failed. %v\n", err) - return + newPath := orgURL.Path[:len(orgURL.Path)-len(pathExt)] + cExt + newOrgURL, err := url.Parse(fmt.Sprintf("%s://%s%s?%s", orgURL.Scheme, orgURL.Host, newPath, orgURL.RawQuery)) + if err != nil { + log.Println(err) + continue + } + newReq, err := http.NewRequest("GET", newOrgURL.String(), nil) + newReq.Header = req.Header + if err != nil { + log.Println(err) + continue + } + orgRes, err = client.Do(newReq) + if err != nil { + log.Println(err) + continue + } + if orgRes.StatusCode != 200 { + log.Println(orgRes.Status) + continue + } else { + break + } + } + if orgRes == nil { + http.Error(w, "Get origin failed", http.StatusBadGateway) + log.Printf("Get origin failed. %v\n", err) + return + } + } else { + orgRes, err = client.Do(req) + if err != nil { + http.Error(w, "Get origin failed", http.StatusBadGateway) + log.Printf("Get origin failed. %v\n", err) + return + } } defer orgRes.Body.Close() From c39f10a9125278fcf3b7b3b29fb37b34399a5e03 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=AB=98=E6=A9=8B=20=E6=8B=93=E4=B9=9F?= Date: Thu, 10 Aug 2023 13:32:27 +0900 Subject: [PATCH 02/15] Revert "add webp ext conv" This reverts commit 2e6613854b4d49c371a8cccb9433dee24fdac697. --- main.go | 47 +++++------------------------------------------ 1 file changed, 5 insertions(+), 42 deletions(-) diff --git a/main.go b/main.go index 1c55c56..db685a5 100644 --- a/main.go +++ b/main.go @@ -9,7 +9,6 @@ import ( "net/http" "net/url" "os" - "path/filepath" "runtime/debug" "strconv" "syscall" @@ -20,7 +19,6 @@ var client http.Client var orgSrvURL string var quality = 90 var version = "" -var convExtCandidates = []string{".jpg", ".png", ".jpeg"} func main() { var ver bool @@ -79,47 +77,12 @@ func proxy(w http.ResponseWriter, r *http.Request) { if len(xff) > 1 { req.Header.Set("X-Forwarded-For", xff) } - var orgRes *http.Response - pathExt := filepath.Ext(req.URL.Path) - if pathExt == ".webp" { - for _, cExt := range convExtCandidates { - newPath := orgURL.Path[:len(orgURL.Path)-len(pathExt)] + cExt - newOrgURL, err := url.Parse(fmt.Sprintf("%s://%s%s?%s", orgURL.Scheme, orgURL.Host, newPath, orgURL.RawQuery)) - if err != nil { - log.Println(err) - continue - } - newReq, err := http.NewRequest("GET", newOrgURL.String(), nil) - newReq.Header = req.Header - if err != nil { - log.Println(err) - continue - } - orgRes, err = client.Do(newReq) - if err != nil { - log.Println(err) - continue - } - if orgRes.StatusCode != 200 { - log.Println(orgRes.Status) - continue - } else { - break - } - } - if orgRes == nil { - http.Error(w, "Get origin failed", http.StatusBadGateway) - log.Printf("Get origin failed. %v\n", err) - return - } - } else { - orgRes, err = client.Do(req) - if err != nil { - http.Error(w, "Get origin failed", http.StatusBadGateway) - log.Printf("Get origin failed. %v\n", err) - return - } + orgRes, err := client.Do(req) + if err != nil { + http.Error(w, "Get origin failed", http.StatusBadGateway) + log.Printf("Get origin failed. %v\n", err) + return } defer orgRes.Body.Close() From 341cd14fa24e9f2b13a5b4361e1f13634ce5b523 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=AB=98=E6=A9=8B=20=E6=8B=93=E4=B9=9F?= Date: Thu, 10 Aug 2023 13:34:19 +0900 Subject: [PATCH 03/15] add client do wrapper for webp --- do.go | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ go.sum | 0 main.go | 3 +-- 3 files changed, 56 insertions(+), 2 deletions(-) create mode 100644 do.go create mode 100644 go.sum diff --git a/do.go b/do.go new file mode 100644 index 0000000..7ba2471 --- /dev/null +++ b/do.go @@ -0,0 +1,55 @@ +package main + +import ( + "fmt" + "log" + "net/http" + "net/url" + "path/filepath" +) + +var convExtCandidates = []string{".jpg", ".png", ".jpeg"} + +func Do(req *http.Request) (*http.Response, error) { + orgURL := req.URL + var orgRes *http.Response + pathExt := filepath.Ext(req.URL.Path) + if pathExt == ".webp" { + for _, cExt := range convExtCandidates { + + newPath := orgURL.Path[:len(orgURL.Path)-len(pathExt)] + cExt + newOrgURL, err := url.Parse(fmt.Sprintf("%s://%s%s?%s", orgURL.Scheme, orgURL.Host, newPath, orgURL.RawQuery)) + if err != nil { + log.Println(err) + continue + } + newReq, err := http.NewRequest("GET", newOrgURL.String(), nil) + newReq.Header = req.Header + if err != nil { + log.Println(err) + continue + } + orgRes, err = client.Do(newReq) + if err != nil { + log.Println(err) + continue + } + if orgRes.StatusCode != 200 { + log.Println(orgRes.Status) + continue + } else { + break + } + } + if orgRes == nil { + return nil, fmt.Errorf("get origin failed") + } + return orgRes, nil + } else { + orgRes, err := client.Do(req) + if err != nil { + return nil, fmt.Errorf("get origin failed") + } + return orgRes, nil + } +} diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..e69de29 diff --git a/main.go b/main.go index db685a5..c3ad055 100644 --- a/main.go +++ b/main.go @@ -77,8 +77,7 @@ func proxy(w http.ResponseWriter, r *http.Request) { if len(xff) > 1 { req.Header.Set("X-Forwarded-For", xff) } - - orgRes, err := client.Do(req) + orgRes, err := Do(req) if err != nil { http.Error(w, "Get origin failed", http.StatusBadGateway) log.Printf("Get origin failed. %v\n", err) From a88e3040ec56afb147d2fc042ab84ce2efdad5ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=AB=98=E6=A9=8B=20=E6=8B=93=E4=B9=9F?= Date: Thu, 10 Aug 2023 15:06:18 +0900 Subject: [PATCH 04/15] add webp funcs --- do.go | 55 --------------------------------------------- main.go | 34 +++++++++++++++++++++++----- webp.go | 69 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 97 insertions(+), 61 deletions(-) delete mode 100644 do.go create mode 100644 webp.go diff --git a/do.go b/do.go deleted file mode 100644 index 7ba2471..0000000 --- a/do.go +++ /dev/null @@ -1,55 +0,0 @@ -package main - -import ( - "fmt" - "log" - "net/http" - "net/url" - "path/filepath" -) - -var convExtCandidates = []string{".jpg", ".png", ".jpeg"} - -func Do(req *http.Request) (*http.Response, error) { - orgURL := req.URL - var orgRes *http.Response - pathExt := filepath.Ext(req.URL.Path) - if pathExt == ".webp" { - for _, cExt := range convExtCandidates { - - newPath := orgURL.Path[:len(orgURL.Path)-len(pathExt)] + cExt - newOrgURL, err := url.Parse(fmt.Sprintf("%s://%s%s?%s", orgURL.Scheme, orgURL.Host, newPath, orgURL.RawQuery)) - if err != nil { - log.Println(err) - continue - } - newReq, err := http.NewRequest("GET", newOrgURL.String(), nil) - newReq.Header = req.Header - if err != nil { - log.Println(err) - continue - } - orgRes, err = client.Do(newReq) - if err != nil { - log.Println(err) - continue - } - if orgRes.StatusCode != 200 { - log.Println(orgRes.Status) - continue - } else { - break - } - } - if orgRes == nil { - return nil, fmt.Errorf("get origin failed") - } - return orgRes, nil - } else { - orgRes, err := client.Do(req) - if err != nil { - return nil, fmt.Errorf("get origin failed") - } - return orgRes, nil - } -} diff --git a/main.go b/main.go index c3ad055..b8929ea 100644 --- a/main.go +++ b/main.go @@ -9,6 +9,7 @@ import ( "net/http" "net/url" "os" + "path/filepath" "runtime/debug" "strconv" "syscall" @@ -77,11 +78,22 @@ func proxy(w http.ResponseWriter, r *http.Request) { if len(xff) > 1 { req.Header.Set("X-Forwarded-For", xff) } - orgRes, err := Do(req) - if err != nil { - http.Error(w, "Get origin failed", http.StatusBadGateway) - log.Printf("Get origin failed. %v\n", err) - return + var orgRes *http.Response + pathExt := filepath.Ext(req.URL.Path) + if pathExt == ".webp" { + orgRes, err = doWebp(req) + if err != nil { + http.Error(w, "Get origin failed", http.StatusBadGateway) + log.Printf("Get origin failed. %v\n", err) + return + } + } else { + orgRes, err = client.Do(req) + if err != nil { + http.Error(w, "Get origin failed", http.StatusBadGateway) + log.Printf("Get origin failed. %v\n", err) + return + } } defer orgRes.Body.Close() @@ -129,8 +141,18 @@ func proxy(w http.ResponseWriter, r *http.Request) { return } defer buf.Reset() + if pathExt == ".webp" { + buf, err = convWebp(buf, []string{}) + if err != nil { + http.Error(w, "image convert failed", http.StatusInternalServerError) + log.Printf("Read origin body failed. %v\n", err) + return - w.Header().Set("Content-Type", "image/jpeg") + } + w.Header().Set("Content-Type", "image/webp") + } else { + w.Header().Set("Content-Type", "image/jpeg") + } w.Header().Set("Content-Length", strconv.Itoa(buf.Len())) if _, err := io.Copy(w, buf); err != nil { diff --git a/webp.go b/webp.go new file mode 100644 index 0000000..ca1cb0f --- /dev/null +++ b/webp.go @@ -0,0 +1,69 @@ +package main + +import ( + "bytes" + "fmt" + "io" + "log" + "net/http" + "net/url" + "os" + "os/exec" +) + +var convExtCandidates = []string{".jpg", ".png", ".jpeg"} + +func doWebp(req *http.Request) (*http.Response, error) { + var orgRes *http.Response + for _, cExt := range convExtCandidates { + orgURL := req.URL + newPath := orgURL.Path[:len(orgURL.Path)-len(".webp")] + cExt + newOrgURL, err := url.Parse(fmt.Sprintf("%s://%s%s?%s", orgURL.Scheme, orgURL.Host, newPath, orgURL.RawQuery)) + if err != nil { + log.Println(err) + continue + } + newReq, err := http.NewRequest("GET", newOrgURL.String(), nil) + newReq.Header = req.Header + if err != nil { + log.Println(err) + continue + } + orgRes, err = client.Do(newReq) + if err != nil { + log.Println(err) + continue + } + if orgRes.StatusCode != 200 { + log.Println(orgRes.Status) + continue + } else { + break + } + } + if orgRes == nil { + return nil, fmt.Errorf("get origin failed") + } + return orgRes, nil +} + +func convWebp(src io.Reader, params []string) (*bytes.Buffer, error) { + f, err := os.CreateTemp("/tmp", "") + if err != nil { + return nil, err + } + defer f.Close() + defer os.Remove(f.Name()) + + _, err = io.Copy(f, src) + if err != nil { + return nil, err + } + params = append(params, "-quiet", f.Name(), "-o", "-") + out, err := exec.Command("cwebp", params...).Output() + if err != nil { + log.Println(err) + return nil, err + } + return bytes.NewBuffer(out), nil +} From d778c8ac91abd8ab58e3590021f9f6e6128831b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=AB=98=E6=A9=8B=20=E6=8B=93=E4=B9=9F?= Date: Thu, 10 Aug 2023 15:42:48 +0900 Subject: [PATCH 05/15] add cwebp --- Dockerfile | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Dockerfile b/Dockerfile index e3d98fb..ac17290 100644 --- a/Dockerfile +++ b/Dockerfile @@ -6,10 +6,15 @@ WORKDIR /go/src/oyaki COPY . /go/src/oyaki RUN CGO_ENABLED=0 go build -ldflags "-s -w -X main.version=${OYAKI_VERSION}" -o /go/bin/oyaki +RUN apt update && apt install -y curl \ + && curl https://storage.googleapis.com/downloads.webmproject.org/releases/webp/libwebp-1.3.1-linux-x86-64.tar.gz --output libwebp.tar.gz \ + && tar vzxf libwebp.tar.gz \ + && mv libwebp-1.3.1-linux-x86-64/bin/cwebp /go/bin/ FROM gcr.io/distroless/static-debian11 COPY --from=build /go/bin/oyaki / +COPY --from=build /go/bin/cwebp /bin/ EXPOSE 8080 From 0052ab29cdf6700e76dfcbc07f0d468dec274299 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=AB=98=E6=A9=8B=20=E6=8B=93=E4=B9=9F?= Date: Fri, 8 Sep 2023 18:18:48 +0900 Subject: [PATCH 06/15] add build_webp.yaml --- .github/workflows/build_webp.yaml | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 .github/workflows/build_webp.yaml diff --git a/.github/workflows/build_webp.yaml b/.github/workflows/build_webp.yaml new file mode 100644 index 0000000..2214f1f --- /dev/null +++ b/.github/workflows/build_webp.yaml @@ -0,0 +1,12 @@ +name: "build" + +on: + workflow_dispatch: + + +jobs: + build: + uses: takutakahashi/github-actions/.github/workflows/ghcr.yaml@main + with: + path: . + image: takutakahashi/oyaki-webp:${{ github.sha }} From 8a096abc73f9eee1b7291649306d00d3b22191ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=AB=98=E6=A9=8B=20=E6=8B=93=E4=B9=9F?= Date: Tue, 19 Sep 2023 15:52:11 +0900 Subject: [PATCH 07/15] add params --- webp.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/webp.go b/webp.go index ca1cb0f..d7992ed 100644 --- a/webp.go +++ b/webp.go @@ -59,7 +59,7 @@ func convWebp(src io.Reader, params []string) (*bytes.Buffer, error) { if err != nil { return nil, err } - params = append(params, "-quiet", f.Name(), "-o", "-") + params = append(params, "-quiet", "-mt", "-jpeg_like", "-q", "80", f.Name(), "-o", "-") out, err := exec.Command("cwebp", params...).Output() if err != nil { log.Println(err) From 0522645cb3b630bc344bbbe94ab7d2a28f5a4bd1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=AB=98=E6=A9=8B=20=E6=8B=93=E4=B9=9F?= Date: Tue, 19 Sep 2023 16:30:47 +0900 Subject: [PATCH 08/15] Revert "add params" This reverts commit 8a096abc73f9eee1b7291649306d00d3b22191ce. --- webp.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/webp.go b/webp.go index d7992ed..ca1cb0f 100644 --- a/webp.go +++ b/webp.go @@ -59,7 +59,7 @@ func convWebp(src io.Reader, params []string) (*bytes.Buffer, error) { if err != nil { return nil, err } - params = append(params, "-quiet", "-mt", "-jpeg_like", "-q", "80", f.Name(), "-o", "-") + params = append(params, "-quiet", f.Name(), "-o", "-") out, err := exec.Command("cwebp", params...).Output() if err != nil { log.Println(err) From 1aea0d868824779c978104e1430a336728f2c2fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=AB=98=E6=A9=8B=20=E6=8B=93=E4=B9=9F?= Date: Tue, 19 Sep 2023 16:31:24 +0900 Subject: [PATCH 09/15] -mt --- webp.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/webp.go b/webp.go index ca1cb0f..f8602ea 100644 --- a/webp.go +++ b/webp.go @@ -59,7 +59,7 @@ func convWebp(src io.Reader, params []string) (*bytes.Buffer, error) { if err != nil { return nil, err } - params = append(params, "-quiet", f.Name(), "-o", "-") + params = append(params, "-quiet", "-mt", f.Name(), "-o", "-") out, err := exec.Command("cwebp", params...).Output() if err != nil { log.Println(err) From d8840732875de1a2537ecb172157b0e336bf5e38 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=AB=98=E6=A9=8B=20=E6=8B=93=E4=B9=9F?= Date: Tue, 19 Sep 2023 16:31:48 +0900 Subject: [PATCH 10/15] add -jpeg_like --- webp.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/webp.go b/webp.go index f8602ea..55798a8 100644 --- a/webp.go +++ b/webp.go @@ -59,7 +59,7 @@ func convWebp(src io.Reader, params []string) (*bytes.Buffer, error) { if err != nil { return nil, err } - params = append(params, "-quiet", "-mt", f.Name(), "-o", "-") + params = append(params, "-quiet", "-mt", "-jpeg_like", f.Name(), "-o", "-") out, err := exec.Command("cwebp", params...).Output() if err != nil { log.Println(err) From c81ee2271459a4ad51cf5d927a37f3ea430064f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=AB=98=E6=A9=8B=20=E6=8B=93=E4=B9=9F?= Date: Thu, 21 Sep 2023 11:36:00 +0900 Subject: [PATCH 11/15] layerd ext will be handled first --- webp.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/webp.go b/webp.go index 55798a8..1842a9f 100644 --- a/webp.go +++ b/webp.go @@ -11,12 +11,13 @@ import ( "os/exec" ) -var convExtCandidates = []string{".jpg", ".png", ".jpeg"} +// handle .jpg.webp ext first +var convExtCandidates = []string{"", ".jpg", ".png", ".jpeg"} func doWebp(req *http.Request) (*http.Response, error) { var orgRes *http.Response + orgURL := req.URL for _, cExt := range convExtCandidates { - orgURL := req.URL newPath := orgURL.Path[:len(orgURL.Path)-len(".webp")] + cExt newOrgURL, err := url.Parse(fmt.Sprintf("%s://%s%s?%s", orgURL.Scheme, orgURL.Host, newPath, orgURL.RawQuery)) if err != nil { From bee64f8cc0e1d035b0b8f4a98d16af8ec6b571f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=AB=98=E6=A9=8B=20=E6=8B=93=E4=B9=9F?= Date: Tue, 10 Oct 2023 11:55:50 +0900 Subject: [PATCH 12/15] add test for webp --- webp_test.go | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 webp_test.go diff --git a/webp_test.go b/webp_test.go new file mode 100644 index 0000000..c67dd62 --- /dev/null +++ b/webp_test.go @@ -0,0 +1,56 @@ +package main + +import ( + "io" + "net/http" + "net/http/httptest" + "testing" +) + +func TestProxyWebP(t *testing.T) { + ts := httptest.NewServer(http.HandlerFunc(proxy)) + + origin := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + http.ServeFile(w, r, "./testdata/oyaki.jpg") + })) + + orgSrvURL = origin.URL + url := ts.URL + "/oyaki.jpg.webp" + + req, _ := http.NewRequest("GET", url, nil) + resp, err := doWebp(req) + if err != nil { + t.Fatal(err) + } else { + io.ReadAll(resp.Body) + resp.Body.Close() + } + // match with origin file info + if resp.Header.Get("Content-Type") != "image/jpeg" { + t.Error("wrong header Content-Type") + t.Error(resp.Header) + } +} + +func TestConvJPG2WebP(t *testing.T) { + ts := httptest.NewServer(http.HandlerFunc(proxy)) + + origin := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + http.ServeFile(w, r, "./testdata/oyaki.jpg") + })) + + orgSrvURL = origin.URL + url := ts.URL + "/oyaki.jpg.webp" + + req, _ := http.NewRequest("GET", url, nil) + resp, err := doWebp(req) + if err != nil { + t.Fatal(err) + } + defer resp.Body.Close() + _, err = convWebp(resp.Body, []string{}) + if err != nil { + t.Fatal(err) + } + +} From ac8a05fc7f8dd4c4a79039bc0e89db3a195a67b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=AB=98=E6=A9=8B=20=E6=8B=93=E4=B9=9F?= Date: Tue, 10 Oct 2023 14:20:55 +0900 Subject: [PATCH 13/15] rm test build --- .github/workflows/build_webp.yaml | 12 ------------ 1 file changed, 12 deletions(-) delete mode 100644 .github/workflows/build_webp.yaml diff --git a/.github/workflows/build_webp.yaml b/.github/workflows/build_webp.yaml deleted file mode 100644 index 2214f1f..0000000 --- a/.github/workflows/build_webp.yaml +++ /dev/null @@ -1,12 +0,0 @@ -name: "build" - -on: - workflow_dispatch: - - -jobs: - build: - uses: takutakahashi/github-actions/.github/workflows/ghcr.yaml@main - with: - path: . - image: takutakahashi/oyaki-webp:${{ github.sha }} From 95512c99b9dd468b160c39a1ec1c73ecebfae68f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=AB=98=E6=A9=8B=20=E6=8B=93=E4=B9=9F?= Date: Tue, 10 Oct 2023 14:25:21 +0900 Subject: [PATCH 14/15] add cwebp to runner --- .github/workflows/ci.yml | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index f3df13a..0c86dc9 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -11,10 +11,14 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - go_version: [1.18, 1.19] + go_version: [ 1.18, 1.19 ] steps: - name: Checkout uses: actions/checkout@v3 + - name: Install Dependencies + run: | + sudo apt update + sudo apt install -y webp - name: Setup Go uses: actions/setup-go@v3 with: From 83e15ceb696319cfa5403ac549f9a6693792bbf0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=AB=98=E6=A9=8B=20=E6=8B=93=E4=B9=9F?= Date: Thu, 12 Oct 2023 11:53:46 +0900 Subject: [PATCH 15/15] rm impl for single ext --- webp.go | 48 +++++++++++++++++++----------------------------- 1 file changed, 19 insertions(+), 29 deletions(-) diff --git a/webp.go b/webp.go index 1842a9f..9ae0a7c 100644 --- a/webp.go +++ b/webp.go @@ -11,39 +11,29 @@ import ( "os/exec" ) -// handle .jpg.webp ext first -var convExtCandidates = []string{"", ".jpg", ".png", ".jpeg"} - func doWebp(req *http.Request) (*http.Response, error) { var orgRes *http.Response orgURL := req.URL - for _, cExt := range convExtCandidates { - newPath := orgURL.Path[:len(orgURL.Path)-len(".webp")] + cExt - newOrgURL, err := url.Parse(fmt.Sprintf("%s://%s%s?%s", orgURL.Scheme, orgURL.Host, newPath, orgURL.RawQuery)) - if err != nil { - log.Println(err) - continue - } - newReq, err := http.NewRequest("GET", newOrgURL.String(), nil) - newReq.Header = req.Header - if err != nil { - log.Println(err) - continue - } - orgRes, err = client.Do(newReq) - if err != nil { - log.Println(err) - continue - } - if orgRes.StatusCode != 200 { - log.Println(orgRes.Status) - continue - } else { - break - } + newPath := orgURL.Path[:len(orgURL.Path)-len(".webp")] + newOrgURL, err := url.Parse(fmt.Sprintf("%s://%s%s?%s", orgURL.Scheme, orgURL.Host, newPath, orgURL.RawQuery)) + if err != nil { + log.Println(err) + return nil, err } - if orgRes == nil { - return nil, fmt.Errorf("get origin failed") + newReq, err := http.NewRequest("GET", newOrgURL.String(), nil) + newReq.Header = req.Header + if err != nil { + log.Println(err) + return nil, err + } + orgRes, err = client.Do(newReq) + if err != nil { + log.Println(err) + return nil, err + } + if orgRes.StatusCode != 200 { + log.Println(orgRes.Status) + return nil, err } return orgRes, nil }