From 16ff497adeb86d54ca0e34ed0c3a48a333fa7cdf Mon Sep 17 00:00:00 2001 From: Benny Date: Tue, 7 Feb 2023 06:07:14 +0100 Subject: [PATCH] deprecate ioutil (#171) --- encoder.go | 7 +++---- encoder_test.go | 9 ++++----- helper.go | 5 ++--- helper_test.go | 6 +++--- router_test.go | 4 ++-- update.go | 4 ++-- 6 files changed, 16 insertions(+), 19 deletions(-) diff --git a/encoder.go b/encoder.go index 45122a0..8aa7a95 100644 --- a/encoder.go +++ b/encoder.go @@ -7,7 +7,6 @@ import ( "image" "image/jpeg" "image/png" - "io/ioutil" "os" "path" "path/filepath" @@ -60,7 +59,7 @@ func convertImage(raw, optimized, itype string) { switch itype { case "webp": - webpEncoder(raw, optimized, config.Quality) + _ = webpEncoder(raw, optimized, config.Quality) case "avif": avifEncoder(raw, optimized, config.Quality) } @@ -68,7 +67,7 @@ func convertImage(raw, optimized, itype string) { } func readRawImage(imgPath string, maxPixel int) (img image.Image, err error) { - data, err := ioutil.ReadFile(imgPath) + data, err := os.ReadFile(imgPath) if err != nil { log.Errorln(err) } @@ -138,7 +137,7 @@ func webpEncoder(p1, p2 string, quality float32) error { log.Warnf("Can't encode source image: %v to WebP", err) } - if err := ioutil.WriteFile(p2, buf.Bytes(), 0644); err != nil { + if err := os.WriteFile(p2, buf.Bytes(), 0644); err != nil { log.Error(err) return err } diff --git a/encoder_test.go b/encoder_test.go index cfec0cd..e7c7f0b 100644 --- a/encoder_test.go +++ b/encoder_test.go @@ -2,7 +2,6 @@ package main import ( "github.com/stretchr/testify/assert" - "io/ioutil" "os" "path" "path/filepath" @@ -40,13 +39,13 @@ func TestAvifEncoder(t *testing.T) { func TestNonExistImage(t *testing.T) { var dest = "/tmp/test-result" - webpEncoder("./pics/empty.jpg", dest, 80) + _ = webpEncoder("./pics/empty.jpg", dest, 80) avifEncoder("./pics/empty.jpg", dest, 80) } func TestConvertFail(t *testing.T) { var dest = "/tmp/test-result" - webpEncoder("./pics/webp_server.jpg", dest, -1) + _ = webpEncoder("./pics/webp_server.jpg", dest, -1) avifEncoder("./pics/webp_server.jpg", dest, -1) } @@ -54,13 +53,13 @@ func runEncoder(t *testing.T, file string, dest string) { if file == "pics/empty.jpg" { t.Log("Empty file, that's okay.") } - webpEncoder(file, dest, 80) + _ = webpEncoder(file, dest, 80) assertType(t, dest, "image/webp") } func assertType(t *testing.T, dest, mime string) { - data, _ := ioutil.ReadFile(dest) + data, _ := os.ReadFile(dest) types := getFileContentType(data[:512]) assert.Equalf(t, mime, types, "File %s should be %s", dest, mime) } diff --git a/helper.go b/helper.go index f147ced..6839f70 100644 --- a/helper.go +++ b/helper.go @@ -6,7 +6,6 @@ import ( "github.com/h2non/filetype" "hash/crc32" "io" - "io/ioutil" "net/http" "os" "path" @@ -155,7 +154,7 @@ func genEtag(ImgAbsPath string) string { if proxyMode { ImgAbsPath = path.Join(remoteRaw, strings.Replace(ImgAbsPath, config.ImgPath, "", -1)) } - data, err := ioutil.ReadFile(ImgAbsPath) + data, err := os.ReadFile(ImgAbsPath) if err != nil { log.Warn(err) } @@ -216,7 +215,7 @@ func guessSupportedFormat(header *fasthttp.RequestHeader) []string { func chooseProxy(proxyRawSize string, optimizedAbs string) bool { var proxyRaw, _ = strconv.Atoi(proxyRawSize) - webp, _ := ioutil.ReadFile(optimizedAbs) + webp, _ := os.ReadFile(optimizedAbs) if len(webp) > proxyRaw { return true } else { diff --git a/helper_test.go b/helper_test.go index 73144b1..e410b55 100644 --- a/helper_test.go +++ b/helper_test.go @@ -3,8 +3,8 @@ package main import ( log "github.com/sirupsen/logrus" "github.com/valyala/fasthttp" - "io/ioutil" "net/http" + "os" "path/filepath" "sort" "strings" @@ -153,7 +153,7 @@ func TestFetchRemoteImage(t *testing.T) { url := "http://github.com/favicon.ico" err := fetchRemoteImage(fp, url) assert.Equal(t, err, nil) - data, _ := ioutil.ReadFile(fp) + data, _ := os.ReadFile(fp) assert.Equal(t, "image/vnd.microsoft.icon", getFileContentType(data)) // test can't create file @@ -168,7 +168,7 @@ func TestFetchRemoteImage(t *testing.T) { func TestCleanProxyCache(t *testing.T) { // test normal situation fp := filepath.Join("./exhaust", "sample.png.12345.webp") - _ = ioutil.WriteFile(fp, []byte("1234"), 0755) + _ = os.WriteFile(fp, []byte("1234"), 0755) assert.True(t, imageExists(fp)) cleanProxyCache(fp) assert.False(t, imageExists(fp)) diff --git a/router_test.go b/router_test.go index c3205f6..71791f3 100644 --- a/router_test.go +++ b/router_test.go @@ -5,7 +5,7 @@ package main import ( - "io/ioutil" + "io" "net/http" "net/http/httptest" "os" @@ -40,7 +40,7 @@ func requestToServer(url string, app *fiber.App, ua, accept string) (*http.Respo if err != nil { return nil, nil } - data, _ := ioutil.ReadAll(resp.Body) + data, _ := io.ReadAll(resp.Body) return resp, data } diff --git a/update.go b/update.go index ff2d623..55a0140 100644 --- a/update.go +++ b/update.go @@ -5,7 +5,7 @@ import ( "fmt" log "github.com/sirupsen/logrus" "github.com/staktrace/go-update" - "io/ioutil" + "io" "net/http" "runtime" ) @@ -24,7 +24,7 @@ func autoUpdate() { var res Result log.Debugf("Requesting to %s", api) resp1, _ := http.Get(api) - data1, _ := ioutil.ReadAll(resp1.Body) + data1, _ := io.ReadAll(resp1.Body) _ = json.Unmarshal(data1, &res) var gitVersion = res.TagName