mirror of
https://github.com/woodchen-ink/webp_server_go.git
synced 2025-07-19 14:12:01 +08:00
deprecate ioutil (#171)
This commit is contained in:
parent
9fba09f19c
commit
16ff497ade
@ -7,7 +7,6 @@ import (
|
|||||||
"image"
|
"image"
|
||||||
"image/jpeg"
|
"image/jpeg"
|
||||||
"image/png"
|
"image/png"
|
||||||
"io/ioutil"
|
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
@ -60,7 +59,7 @@ func convertImage(raw, optimized, itype string) {
|
|||||||
|
|
||||||
switch itype {
|
switch itype {
|
||||||
case "webp":
|
case "webp":
|
||||||
webpEncoder(raw, optimized, config.Quality)
|
_ = webpEncoder(raw, optimized, config.Quality)
|
||||||
case "avif":
|
case "avif":
|
||||||
avifEncoder(raw, optimized, config.Quality)
|
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) {
|
func readRawImage(imgPath string, maxPixel int) (img image.Image, err error) {
|
||||||
data, err := ioutil.ReadFile(imgPath)
|
data, err := os.ReadFile(imgPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorln(err)
|
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)
|
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)
|
log.Error(err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -2,7 +2,6 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"io/ioutil"
|
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
@ -40,13 +39,13 @@ func TestAvifEncoder(t *testing.T) {
|
|||||||
|
|
||||||
func TestNonExistImage(t *testing.T) {
|
func TestNonExistImage(t *testing.T) {
|
||||||
var dest = "/tmp/test-result"
|
var dest = "/tmp/test-result"
|
||||||
webpEncoder("./pics/empty.jpg", dest, 80)
|
_ = webpEncoder("./pics/empty.jpg", dest, 80)
|
||||||
avifEncoder("./pics/empty.jpg", dest, 80)
|
avifEncoder("./pics/empty.jpg", dest, 80)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestConvertFail(t *testing.T) {
|
func TestConvertFail(t *testing.T) {
|
||||||
var dest = "/tmp/test-result"
|
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)
|
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" {
|
if file == "pics/empty.jpg" {
|
||||||
t.Log("Empty file, that's okay.")
|
t.Log("Empty file, that's okay.")
|
||||||
}
|
}
|
||||||
webpEncoder(file, dest, 80)
|
_ = webpEncoder(file, dest, 80)
|
||||||
assertType(t, dest, "image/webp")
|
assertType(t, dest, "image/webp")
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func assertType(t *testing.T, dest, mime string) {
|
func assertType(t *testing.T, dest, mime string) {
|
||||||
data, _ := ioutil.ReadFile(dest)
|
data, _ := os.ReadFile(dest)
|
||||||
types := getFileContentType(data[:512])
|
types := getFileContentType(data[:512])
|
||||||
assert.Equalf(t, mime, types, "File %s should be %s", dest, mime)
|
assert.Equalf(t, mime, types, "File %s should be %s", dest, mime)
|
||||||
}
|
}
|
||||||
|
@ -6,7 +6,6 @@ import (
|
|||||||
"github.com/h2non/filetype"
|
"github.com/h2non/filetype"
|
||||||
"hash/crc32"
|
"hash/crc32"
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
@ -155,7 +154,7 @@ func genEtag(ImgAbsPath string) string {
|
|||||||
if proxyMode {
|
if proxyMode {
|
||||||
ImgAbsPath = path.Join(remoteRaw, strings.Replace(ImgAbsPath, config.ImgPath, "", -1))
|
ImgAbsPath = path.Join(remoteRaw, strings.Replace(ImgAbsPath, config.ImgPath, "", -1))
|
||||||
}
|
}
|
||||||
data, err := ioutil.ReadFile(ImgAbsPath)
|
data, err := os.ReadFile(ImgAbsPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Warn(err)
|
log.Warn(err)
|
||||||
}
|
}
|
||||||
@ -216,7 +215,7 @@ func guessSupportedFormat(header *fasthttp.RequestHeader) []string {
|
|||||||
|
|
||||||
func chooseProxy(proxyRawSize string, optimizedAbs string) bool {
|
func chooseProxy(proxyRawSize string, optimizedAbs string) bool {
|
||||||
var proxyRaw, _ = strconv.Atoi(proxyRawSize)
|
var proxyRaw, _ = strconv.Atoi(proxyRawSize)
|
||||||
webp, _ := ioutil.ReadFile(optimizedAbs)
|
webp, _ := os.ReadFile(optimizedAbs)
|
||||||
if len(webp) > proxyRaw {
|
if len(webp) > proxyRaw {
|
||||||
return true
|
return true
|
||||||
} else {
|
} else {
|
||||||
|
@ -3,8 +3,8 @@ package main
|
|||||||
import (
|
import (
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
"github.com/valyala/fasthttp"
|
"github.com/valyala/fasthttp"
|
||||||
"io/ioutil"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"sort"
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
@ -153,7 +153,7 @@ func TestFetchRemoteImage(t *testing.T) {
|
|||||||
url := "http://github.com/favicon.ico"
|
url := "http://github.com/favicon.ico"
|
||||||
err := fetchRemoteImage(fp, url)
|
err := fetchRemoteImage(fp, url)
|
||||||
assert.Equal(t, err, nil)
|
assert.Equal(t, err, nil)
|
||||||
data, _ := ioutil.ReadFile(fp)
|
data, _ := os.ReadFile(fp)
|
||||||
assert.Equal(t, "image/vnd.microsoft.icon", getFileContentType(data))
|
assert.Equal(t, "image/vnd.microsoft.icon", getFileContentType(data))
|
||||||
|
|
||||||
// test can't create file
|
// test can't create file
|
||||||
@ -168,7 +168,7 @@ func TestFetchRemoteImage(t *testing.T) {
|
|||||||
func TestCleanProxyCache(t *testing.T) {
|
func TestCleanProxyCache(t *testing.T) {
|
||||||
// test normal situation
|
// test normal situation
|
||||||
fp := filepath.Join("./exhaust", "sample.png.12345.webp")
|
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))
|
assert.True(t, imageExists(fp))
|
||||||
cleanProxyCache(fp)
|
cleanProxyCache(fp)
|
||||||
assert.False(t, imageExists(fp))
|
assert.False(t, imageExists(fp))
|
||||||
|
@ -5,7 +5,7 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"io/ioutil"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
"os"
|
"os"
|
||||||
@ -40,7 +40,7 @@ func requestToServer(url string, app *fiber.App, ua, accept string) (*http.Respo
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
data, _ := ioutil.ReadAll(resp.Body)
|
data, _ := io.ReadAll(resp.Body)
|
||||||
return resp, data
|
return resp, data
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -5,7 +5,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
"github.com/staktrace/go-update"
|
"github.com/staktrace/go-update"
|
||||||
"io/ioutil"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"runtime"
|
"runtime"
|
||||||
)
|
)
|
||||||
@ -24,7 +24,7 @@ func autoUpdate() {
|
|||||||
var res Result
|
var res Result
|
||||||
log.Debugf("Requesting to %s", api)
|
log.Debugf("Requesting to %s", api)
|
||||||
resp1, _ := http.Get(api)
|
resp1, _ := http.Get(api)
|
||||||
data1, _ := ioutil.ReadAll(resp1.Body)
|
data1, _ := io.ReadAll(resp1.Body)
|
||||||
_ = json.Unmarshal(data1, &res)
|
_ = json.Unmarshal(data1, &res)
|
||||||
var gitVersion = res.TagName
|
var gitVersion = res.TagName
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user