mirror of
https://github.com/woodchen-ink/webp_server_go.git
synced 2025-07-18 05:32:02 +08:00
deprecate ioutil (#171)
This commit is contained in:
parent
9fba09f19c
commit
16ff497ade
@ -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
|
||||
}
|
||||
|
@ -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)
|
||||
}
|
||||
|
@ -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 {
|
||||
|
@ -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))
|
||||
|
@ -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
|
||||
}
|
||||
|
||||
|
@ -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
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user