diff --git a/.gitignore b/.gitignore index 30d5d82..6938dec 100644 --- a/.gitignore +++ b/.gitignore @@ -4,6 +4,7 @@ *.dll *.so *.dylib +tools/bin # Test binary, built with `go test -c` *.test diff --git a/.golangci.yml b/.golangci.yml new file mode 100644 index 0000000..97f5a43 --- /dev/null +++ b/.golangci.yml @@ -0,0 +1,43 @@ +run: + timeout: 10m +linters: + disable-all: true + enable: + - misspell + - ineffassign + - typecheck + - unused + - gosimple + - errcheck + - staticcheck + - stylecheck + - gosec + - asciicheck + - bodyclose + - exportloopref + - rowserrcheck + - unconvert + - makezero + - durationcheck + - prealloc + - predeclared + +linters-settings: + staticcheck: + checks: ["S1002","S1004","S1007","S1009","S1010","S1012","S1019","S1020","S1021","S1024","S1030","SA2*","SA3*","SA4009","SA5*","SA6000","SA6001","SA6005", "-SA2002"] + stylecheck: + checks: ["-ST1003"] + gosec: + severity: "low" + confidence: "low" + excludes: + - G101 + - G107 +issues: + exclude-rules: + - path: _test\.go + linters: + - errcheck + - gosec + - rowserrcheck + - makezero \ No newline at end of file diff --git a/Makefile b/Makefile index 2706e3e..6b37f0b 100644 --- a/Makefile +++ b/Makefile @@ -19,7 +19,19 @@ all: make clean ./scripts/build.sh $(OS) $(ARCH) -test: +tools-dir: + mkdir -p tools/bin + +install-staticcheck: tools-dir + GOBIN=`pwd`/tools/bin go install honnef.co/go/tools/cmd/staticcheck@latest + curl -sfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh| sh -s -- -b ./tools/bin v1.51.2 + +static-check: install-staticcheck + #S1000,SA1015,SA4006,SA4011,S1023,S1034,ST1003,ST1005,ST1016,ST1020,ST1021 + tools/bin/staticcheck -checks all,-ST1000 ./... + GO111MODULE=on tools/bin/golangci-lint run -v $$($(PACKAGE_DIRECTORIES)) --config .golangci.yml + +test: static-check go test -v -coverprofile=coverage.txt -covermode=atomic clean: diff --git a/config.go b/config.go index 83f77b9..bc20eb5 100644 --- a/config.go +++ b/config.go @@ -23,7 +23,7 @@ var ( remoteRaw = "remote-raw" config Config version = "0.5.0" - releaseUrl = "https://github.com/webp-sh/webp_server_go/releases/latest/download/" + releaseURL = "https://github.com/webp-sh/webp_server_go/releases/latest/download/" ) const ( diff --git a/encoder.go b/encoder.go index 8aa7a95..27aed3b 100644 --- a/encoder.go +++ b/encoder.go @@ -55,6 +55,9 @@ func convertImage(raw, optimized, itype string) { //we need to create dir first err = os.MkdirAll(path.Dir(optimized), 0755) + if err != nil { + log.Error(err.Error()) + } //q, _ := strconv.ParseFloat(config.Quality, 32) switch itype { @@ -137,7 +140,7 @@ func webpEncoder(p1, p2 string, quality float32) error { log.Warnf("Can't encode source image: %v to WebP", err) } - if err := os.WriteFile(p2, buf.Bytes(), 0644); err != nil { + if err := os.WriteFile(p2, buf.Bytes(), 0600); err != nil { log.Error(err) return err } diff --git a/helper.go b/helper.go index 32f23ff..ba7ac28 100644 --- a/helper.go +++ b/helper.go @@ -84,12 +84,13 @@ func checkAllowedType(imgFilename string) bool { // Check for remote filepath, e.g: https://test.webp.sh/node.png // return StatusCode, etagValue and length -func getRemoteImageInfo(fileUrl string) (int, string, string) { - res, err := http.Head(fileUrl) +func getRemoteImageInfo(fileURL string) (int, string, string) { + res, err := http.Head(fileURL) if err != nil { log.Errorln("Connection to remote error!") return http.StatusInternalServerError, "", "" } + defer res.Body.Close() if res.StatusCode != 404 { etagValue := res.Header.Get("etag") if etagValue == "" { diff --git a/router.go b/router.go index 7f1805a..20cea72 100644 --- a/router.go +++ b/router.go @@ -69,10 +69,10 @@ func convert(c *fiber.Ctx) error { var availableFiles = []string{rawImageAbs} for _, v := range goodFormat { - if "avif" == v { + if v == "avif" { availableFiles = append(availableFiles, avifAbs) } - if "webp" == v { + if v == "webp" { availableFiles = append(availableFiles, webpAbs) } } diff --git a/router_test.go b/router_test.go index 71791f3..c8829f9 100644 --- a/router_test.go +++ b/router_test.go @@ -52,6 +52,7 @@ func TestServerHeaders(t *testing.T) { // test for chrome response, _ := requestToServer(url, app, chromeUA, acceptWebP) + defer response.Body.Close() ratio := response.Header.Get("X-Compression-Rate") etag := response.Header.Get("Etag") @@ -60,7 +61,8 @@ func TestServerHeaders(t *testing.T) { // test for safari response, _ = requestToServer(url, app, safariUA, acceptLegacy) - ratio = response.Header.Get("X-Compression-Rate") + defer response.Body.Close() + // ratio = response.Header.Get("X-Compression-Rate") etag = response.Header.Get("Etag") assert.NotEqual(t, "", etag) @@ -96,14 +98,16 @@ func TestConvert(t *testing.T) { // test Chrome for url, respType := range testChromeLink { - _, data := requestToServer(url, app, chromeUA, acceptWebP) + resp, data := requestToServer(url, app, chromeUA, acceptWebP) + defer resp.Body.Close() contentType := getFileContentType(data) assert.Equal(t, respType, contentType) } // test Safari for url, respType := range testSafariLink { - _, data := requestToServer(url, app, safariUA, acceptLegacy) + resp, data := requestToServer(url, app, safariUA, acceptLegacy) + defer resp.Body.Close() contentType := getFileContentType(data) assert.Equal(t, respType, contentType) } @@ -119,12 +123,14 @@ func TestConvertNotAllowed(t *testing.T) { // not allowed, but we have the file, this should return File extension not allowed url := "http://127.0.0.1:3333/webp_server.bmp" - _, data := requestToServer(url, app, chromeUA, acceptWebP) + resp, data := requestToServer(url, app, chromeUA, acceptWebP) + defer resp.Body.Close() assert.Contains(t, string(data), "File extension not allowed") // not allowed, random file url = url + "hagdgd" - _, data = requestToServer(url, app, chromeUA, acceptWebP) + resp, data = requestToServer(url, app, chromeUA, acceptWebP) + defer resp.Body.Close() assert.Contains(t, string(data), "File extension not allowed") } @@ -139,6 +145,7 @@ func TestConvertProxyModeBad(t *testing.T) { // this is local random image, should be 500 url := "http://127.0.0.1:3333/webp_8888server.bmp" resp, _ := requestToServer(url, app, chromeUA, acceptWebP) + defer resp.Body.Close() assert.Equal(t, http.StatusInternalServerError, resp.StatusCode) } @@ -154,11 +161,13 @@ func TestConvertProxyModeWork(t *testing.T) { url := "https://webp.sh/images/cover.jpg" resp, data := requestToServer(url, app, chromeUA, acceptWebP) + defer resp.Body.Close() assert.Equal(t, http.StatusOK, resp.StatusCode) assert.Equal(t, "image/webp", getFileContentType(data)) // test proxyMode with Safari resp, data = requestToServer(url, app, safariUA, acceptLegacy) + defer resp.Body.Close() assert.Equal(t, http.StatusOK, resp.StatusCode) assert.Equal(t, "image/jpeg", getFileContentType(data)) } @@ -171,8 +180,9 @@ func TestConvertBigger(t *testing.T) { app.Get("/*", convert) url := "http://127.0.0.1:3333/big.jpg" - response, data := requestToServer(url, app, chromeUA, acceptWebP) - assert.Equal(t, "image/jpeg", response.Header.Get("content-type")) + resp, data := requestToServer(url, app, chromeUA, acceptWebP) + defer resp.Body.Close() + assert.Equal(t, "image/jpeg", resp.Header.Get("content-type")) assert.Equal(t, "image/jpeg", getFileContentType(data)) _ = os.RemoveAll(config.ExhaustPath) } diff --git a/update.go b/update.go index 6327f4d..4226a16 100644 --- a/update.go +++ b/update.go @@ -25,7 +25,11 @@ func autoUpdate() { } var res Result log.Debugf("Requesting to %s", api) - resp1, _ := http.Get(api) + resp1, err := http.Get(api) + if err != nil { + log.Errorf("Error requesting to %s", api) + } + defer resp1.Body.Close() data1, _ := io.ReadAll(resp1.Body) _ = json.Unmarshal(data1, &res) var gitVersion = res.TagName @@ -42,13 +46,13 @@ func autoUpdate() { filename += ".exe" } log.Info("Downloading binary to update...") - resp, _ := http.Get(releaseUrl + filename) + resp, _ := http.Get(releaseURL + filename) if resp.StatusCode != 200 { log.Debugf("%s-%s not found on release.", runtime.GOOS, runtime.GOARCH) return } - err := update.Apply(resp.Body, update.Options{}) + err = update.Apply(resp.Body, update.Options{}) if err != nil { // error handling log.Errorf("Update error. %v", err) diff --git a/update_test.go b/update_test.go index 9a76d98..2f82eb7 100644 --- a/update_test.go +++ b/update_test.go @@ -5,9 +5,10 @@ package main import ( - "github.com/stretchr/testify/assert" "os" "testing" + + "github.com/stretchr/testify/assert" ) func TestNormalAutoUpdate(t *testing.T) { @@ -21,7 +22,7 @@ func TestNormalAutoUpdate(t *testing.T) { func Test404AutoUpdate(t *testing.T) { version = "0.0.1" dir := "./update" - releaseUrl = releaseUrl + "a" + releaseURL = releaseURL + "a" autoUpdate() assert.Equal(t, int64(0), fileCount(dir)) _ = os.RemoveAll(dir)