chore (refactoring): replace ioutil with io - #847

This commit is contained in:
lemtea8
2025-06-18 08:07:11 +08:00
committed by GitHub
parent af7c4b911c
commit 9f2728c8b2
16 changed files with 40 additions and 49 deletions

View File

@ -17,7 +17,7 @@ import (
"fmt" "fmt"
"github.com/tidwall/gjson" "github.com/tidwall/gjson"
"github.com/tidwall/sjson" "github.com/tidwall/sjson"
"io/ioutil" "io"
"os" "os"
) )
@ -44,7 +44,7 @@ func LoadConfig() ([]byte, error) {
} }
return nil, err return nil, err
} }
cFile, err := ioutil.ReadAll(file) cFile, err := io.ReadAll(file)
file.Close() file.Close()
if err != nil { if err != nil {
return nil, err return nil, err

View File

@ -11,7 +11,6 @@ import (
"encoding/hex" "encoding/hex"
"hash/fnv" "hash/fnv"
"io" "io"
"io/ioutil"
"math/big" "math/big"
mathrand "math/rand" mathrand "math/rand"
"os" "os"
@ -179,7 +178,7 @@ func decompress(something []byte) ([]byte, error) {
return []byte(""), nil return []byte(""), nil
} }
r.Close() r.Close()
return ioutil.ReadAll(r) return io.ReadAll(r)
} }
func sign(something []byte) ([]byte, error) { func sign(something []byte) ([]byte, error) {

View File

@ -5,7 +5,7 @@ import (
"crypto/rsa" "crypto/rsa"
"crypto/x509" "crypto/x509"
"encoding/pem" "encoding/pem"
"io/ioutil" "io"
"os" "os"
) )
@ -45,7 +45,7 @@ func pullCertificateFromFS() (*x509.Certificate, []byte, error) {
return nil, nil, err return nil, nil, err
} }
defer file.Close() defer file.Close()
certPEM, err := ioutil.ReadAll(file) certPEM, err := io.ReadAll(file)
if err != nil { if err != nil {
return nil, nil, err return nil, nil, err
} }

View File

@ -5,7 +5,7 @@ import (
"crypto/rsa" "crypto/rsa"
"crypto/x509" "crypto/x509"
"encoding/pem" "encoding/pem"
"io/ioutil" "io"
"os" "os"
) )
@ -43,7 +43,7 @@ func pullPrivateKeyFromFS() (*rsa.PrivateKey, []byte, error) {
} }
defer file.Close() defer file.Close()
keyPEM, err := ioutil.ReadAll(file) keyPEM, err := io.ReadAll(file)
if err != nil { if err != nil {
return nil, nil, err return nil, nil, err
} }

View File

@ -5,7 +5,6 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"strconv" "strconv"
"strings" "strings"
) )
@ -78,11 +77,11 @@ func NewStringFromInterface(val interface{}) string {
} }
func NewReadCloserFromBytes(t []byte) io.ReadCloser { func NewReadCloserFromBytes(t []byte) io.ReadCloser {
return ioutil.NopCloser(bytes.NewReader(t)) return io.NopCloser(bytes.NewReader(t))
} }
func NewReadCloserFromReader(r io.Reader) io.ReadCloser { func NewReadCloserFromReader(r io.Reader) io.ReadCloser {
return ioutil.NopCloser(r) return io.NopCloser(r)
} }
func PrettyPrint(json_dirty []byte) []byte { func PrettyPrint(json_dirty []byte) []byte {

View File

@ -5,7 +5,6 @@ import (
. "github.com/mickael-kerjean/filestash/server/common" . "github.com/mickael-kerjean/filestash/server/common"
"golang.org/x/crypto/bcrypt" "golang.org/x/crypto/bcrypt"
"io" "io"
"io/ioutil"
"net/http" "net/http"
"os" "os"
"strconv" "strconv"
@ -56,7 +55,7 @@ func AdminSessionAuthenticate(ctx *App, res http.ResponseWriter, req *http.Reque
return return
} }
var params map[string]string var params map[string]string
b, _ := ioutil.ReadAll(req.Body) b, _ := io.ReadAll(req.Body)
json.Unmarshal(b, &params) json.Unmarshal(b, &params)
if err := bcrypt.CompareHashAndPassword([]byte(admin), []byte(params["password"])); err != nil { if err := bcrypt.CompareHashAndPassword([]byte(admin), []byte(params["password"])); err != nil {
SendErrorResult(res, ErrInvalidPassword) SendErrorResult(res, ErrInvalidPassword)

View File

@ -2,7 +2,7 @@ package ctrl
import ( import (
. "github.com/mickael-kerjean/filestash/server/common" . "github.com/mickael-kerjean/filestash/server/common"
"io/ioutil" "io"
"net/http" "net/http"
) )
@ -13,7 +13,7 @@ func PrivateConfigHandler(ctx *App, res http.ResponseWriter, req *http.Request)
} }
func PrivateConfigUpdateHandler(ctx *App, res http.ResponseWriter, req *http.Request) { func PrivateConfigUpdateHandler(ctx *App, res http.ResponseWriter, req *http.Request) {
b, _ := ioutil.ReadAll(req.Body) b, _ := io.ReadAll(req.Body)
if err := SaveConfig(b); err != nil { if err := SaveConfig(b); err != nil {
SendErrorResult(res, err) SendErrorResult(res, err)
return return

View File

@ -2,7 +2,7 @@ package main
import ( import (
"fmt" "fmt"
"io/ioutil" "io"
"os" "os"
) )
@ -15,7 +15,7 @@ func main() {
} }
defer f.Close() defer f.Close()
j, err := ioutil.ReadAll(f) j, err := io.ReadAll(f)
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err) fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1) os.Exit(1)

View File

@ -3,7 +3,7 @@ package main
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"io/ioutil" "io"
"os" "os"
) )
@ -16,7 +16,7 @@ func main() {
} }
defer f.Close() defer f.Close()
j, err := ioutil.ReadAll(f) j, err := io.ReadAll(f)
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err) fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1) os.Exit(1)

View File

@ -4,7 +4,7 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
. "github.com/mickael-kerjean/filestash/server/common" . "github.com/mickael-kerjean/filestash/server/common"
"io/ioutil" "io"
"net/http" "net/http"
"strings" "strings"
) )
@ -12,7 +12,7 @@ import (
func BodyParser(fn HandlerFunc) HandlerFunc { func BodyParser(fn HandlerFunc) HandlerFunc {
extractBody := func(req *http.Request) (map[string]interface{}, error) { extractBody := func(req *http.Request) (map[string]interface{}, error) {
body := map[string]interface{}{} body := map[string]interface{}{}
byt, err := ioutil.ReadAll(req.Body) byt, err := io.ReadAll(req.Body)
if err != nil { if err != nil {
return body, err return body, err
} }

View File

@ -6,7 +6,6 @@ import (
"fmt" "fmt"
. "github.com/mickael-kerjean/filestash/server/common" . "github.com/mickael-kerjean/filestash/server/common"
"io" "io"
"io/ioutil"
"net/http" "net/http"
"net/url" "net/url"
"os" "os"
@ -162,7 +161,7 @@ func (this ArtifactoryStorage) Ls(path string) ([]os.FileInfo, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
jsonStr, err := ioutil.ReadAll(res.Body) jsonStr, err := io.ReadAll(res.Body)
res.Body.Close() res.Body.Close()
if err != nil { if err != nil {
Log.Debug("plg_backend_artifactory::ls readall data[%s] status[%d]", string(jsonStr), res.StatusCode) Log.Debug("plg_backend_artifactory::ls readall data[%s] status[%d]", string(jsonStr), res.StatusCode)
@ -190,7 +189,7 @@ func (this ArtifactoryStorage) Cat(path string) (io.ReadCloser, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
jsonStr, err := ioutil.ReadAll(res.Body) jsonStr, err := io.ReadAll(res.Body)
res.Body.Close() res.Body.Close()
if err != nil { if err != nil {
Log.Debug("plg_backend_artifactory::cat readall data[%s] status[%d]", string(jsonStr), res.StatusCode) Log.Debug("plg_backend_artifactory::cat readall data[%s] status[%d]", string(jsonStr), res.StatusCode)
@ -269,7 +268,7 @@ func (this ArtifactoryStorage) Mkdir(path string) error {
if err != nil { if err != nil {
return err return err
} }
jsonStr, err := ioutil.ReadAll(res.Body) jsonStr, err := io.ReadAll(res.Body)
res.Body.Close() res.Body.Close()
if err != nil { if err != nil {
Log.Debug("plg_backend_artifactory::mkdir readall status[%d] data[%s]", res.StatusCode, string(jsonStr)) Log.Debug("plg_backend_artifactory::mkdir readall status[%d] data[%s]", res.StatusCode, string(jsonStr))
@ -307,7 +306,7 @@ func (this ArtifactoryStorage) Rm(path string) error {
if err != nil { if err != nil {
return err return err
} }
jsonStr, err := ioutil.ReadAll(res.Body) jsonStr, err := io.ReadAll(res.Body)
res.Body.Close() res.Body.Close()
if err != nil { if err != nil {
Log.Debug("plg_backend_artifactory::rm readall status[%d] data[%s]", res.StatusCode, string(jsonStr)) Log.Debug("plg_backend_artifactory::rm readall status[%d] data[%s]", res.StatusCode, string(jsonStr))
@ -336,7 +335,7 @@ func (this ArtifactoryStorage) Mv(from, to string) error {
if err != nil { if err != nil {
return err return err
} }
jsonStr, err := ioutil.ReadAll(res.Body) jsonStr, err := io.ReadAll(res.Body)
res.Body.Close() res.Body.Close()
if err != nil { if err != nil {
Log.Debug("plg_backend_artifactory::mv readall status[%d] data[%s]", res.StatusCode, string(jsonStr)) Log.Debug("plg_backend_artifactory::mv readall status[%d] data[%s]", res.StatusCode, string(jsonStr))
@ -367,7 +366,7 @@ func (this ArtifactoryStorage) Save(path string, content io.Reader) error {
if err != nil { if err != nil {
return err return err
} }
jsonStr, err := ioutil.ReadAll(res.Body) jsonStr, err := io.ReadAll(res.Body)
res.Body.Close() res.Body.Close()
if err != nil { if err != nil {
Log.Debug("plg_backend_artifactory::save readall status[%d] data[%s]", res.StatusCode, string(jsonStr)) Log.Debug("plg_backend_artifactory::save readall status[%d] data[%s]", res.StatusCode, string(jsonStr))

View File

@ -7,7 +7,6 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"net/http" "net/http"
"net/url" "net/url"
"os" "os"
@ -56,7 +55,7 @@ func (this Backblaze) Init(params map[string]string, app *App) (IBackend, error)
if err != nil { if err != nil {
return nil, err return nil, err
} }
body, err := ioutil.ReadAll(res.Body) body, err := io.ReadAll(res.Body)
res.Body.Close() res.Body.Close()
if err != nil { if err != nil {
return nil, err return nil, err
@ -79,7 +78,7 @@ func (this Backblaze) Init(params map[string]string, app *App) (IBackend, error)
if err != nil { if err != nil {
return nil, err return nil, err
} }
body, err = ioutil.ReadAll(res.Body) body, err = io.ReadAll(res.Body)
res.Body.Close() res.Body.Close()
if err != nil { if err != nil {
return nil, err return nil, err
@ -153,7 +152,7 @@ func (this Backblaze) Ls(path string) ([]os.FileInfo, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
body, err := ioutil.ReadAll(res.Body) body, err := io.ReadAll(res.Body)
res.Body.Close() res.Body.Close()
if err != nil { if err != nil {
return nil, err return nil, err
@ -222,7 +221,7 @@ func (this Backblaze) Mkdir(path string) error {
if err != nil { if err != nil {
return err return err
} }
body, err := ioutil.ReadAll(res.Body) body, err := io.ReadAll(res.Body)
res.Body.Close() res.Body.Close()
if err != nil { if err != nil {
return err return err
@ -260,7 +259,7 @@ func (this Backblaze) Rm(path string) error {
if err != nil { if err != nil {
return err return err
} }
body, err := ioutil.ReadAll(res.Body) body, err := io.ReadAll(res.Body)
res.Body.Close() res.Body.Close()
if err != nil { if err != nil {
return err return err
@ -289,7 +288,7 @@ func (this Backblaze) Rm(path string) error {
if err != nil { if err != nil {
return err return err
} }
body, err := ioutil.ReadAll(res.Body) body, err := io.ReadAll(res.Body)
res.Body.Close() res.Body.Close()
if err != nil { if err != nil {
return err return err
@ -317,7 +316,7 @@ func (this Backblaze) Rm(path string) error {
if err != nil { if err != nil {
return err return err
} }
if body, err = ioutil.ReadAll(res.Body); err != nil { if body, err = io.ReadAll(res.Body); err != nil {
return err return err
} }
res.Body.Close() res.Body.Close()
@ -349,7 +348,7 @@ func (this Backblaze) Touch(path string) error {
if err != nil { if err != nil {
return err return err
} }
body, err := ioutil.ReadAll(res.Body) body, err := io.ReadAll(res.Body)
res.Body.Close() res.Body.Close()
if err != nil { if err != nil {
return err return err
@ -378,7 +377,7 @@ func (this Backblaze) Touch(path string) error {
if err != nil { if err != nil {
return err return err
} }
body, err = ioutil.ReadAll(res.Body) body, err = io.ReadAll(res.Body)
res.Body.Close() res.Body.Close()
if err != nil { if err != nil {
return err return err
@ -406,7 +405,7 @@ func (this Backblaze) Save(path string, file io.Reader) error {
if err != nil { if err != nil {
return err return err
} }
body, err := ioutil.ReadAll(res.Body) body, err := io.ReadAll(res.Body)
res.Body.Close() res.Body.Close()
if err != nil { if err != nil {
return err return err
@ -468,7 +467,7 @@ func (this Backblaze) Save(path string, file io.Reader) error {
if err != nil { if err != nil {
return err return err
} }
body, err = ioutil.ReadAll(res.Body) body, err = io.ReadAll(res.Body)
res.Body.Close() res.Body.Close()
if err != nil { if err != nil {
return err return err

View File

@ -5,7 +5,6 @@ import (
"fmt" "fmt"
. "github.com/mickael-kerjean/filestash/server/common" . "github.com/mickael-kerjean/filestash/server/common"
"io" "io"
"io/ioutil"
"net/http" "net/http"
"net/url" "net/url"
"os" "os"
@ -201,7 +200,7 @@ func (this Dav) Mv(from string, to string) error {
if err != nil { if err != nil {
return err return err
} }
d, err := ioutil.ReadAll(reader) d, err := io.ReadAll(reader)
if err != nil { if err != nil {
return ErrNotValid return ErrNotValid
} }

View File

@ -4,7 +4,6 @@ import (
"encoding/json" "encoding/json"
. "github.com/mickael-kerjean/filestash/server/common" . "github.com/mickael-kerjean/filestash/server/common"
"io" "io"
"io/ioutil"
"net/http" "net/http"
"os" "os"
"path/filepath" "path/filepath"
@ -123,7 +122,7 @@ func (d Dropbox) Cat(path string) (io.ReadCloser, error) {
arg := struct { arg := struct {
Path string `json:"path"` Path string `json:"path"`
}{d.path(path)} }{d.path(path)}
json, _ := ioutil.ReadAll(d.toReader(arg)) json, _ := io.ReadAll(d.toReader(arg))
req.Header.Set("Dropbox-API-Arg", string(json)) req.Header.Set("Dropbox-API-Arg", string(json))
}) })
if err != nil { if err != nil {
@ -184,7 +183,7 @@ func (d Dropbox) Save(path string, file io.Reader) error {
AutoRename bool `json:"autorename"` AutoRename bool `json:"autorename"`
Mode string `json:"mode"` Mode string `json:"mode"`
}{d.path(path), false, "overwrite"} }{d.path(path), false, "overwrite"}
json, _ := ioutil.ReadAll(d.toReader(arg)) json, _ := io.ReadAll(d.toReader(arg))
req.Header.Set("Dropbox-API-Arg", string(json)) req.Header.Set("Dropbox-API-Arg", string(json))
req.Header.Set("Content-Type", "application/octet-stream") req.Header.Set("Content-Type", "application/octet-stream")
}) })

View File

@ -4,7 +4,6 @@ import (
"github.com/h2non/bimg" "github.com/h2non/bimg"
. "github.com/mickael-kerjean/filestash/server/common" . "github.com/mickael-kerjean/filestash/server/common"
"io" "io"
"io/ioutil"
"net/http" "net/http"
) )
@ -28,7 +27,7 @@ func (this thumbnailer) Generate(reader io.ReadCloser, ctx *App, res *http.Respo
return reader, nil return reader, nil
} }
b, err := ioutil.ReadAll(reader) b, err := io.ReadAll(reader)
if err != nil { if err != nil {
return reader, err return reader, err
} }

View File

@ -3,7 +3,6 @@ package plg_security_svg
import ( import (
. "github.com/mickael-kerjean/filestash/server/common" . "github.com/mickael-kerjean/filestash/server/common"
"io" "io"
"io/ioutil"
"net/http" "net/http"
"regexp" "regexp"
) )
@ -42,7 +41,7 @@ func init() {
(*res).Header().Set("Content-Security-Policy", "script-src 'none'; default-src 'none'; img-src 'self'") (*res).Header().Set("Content-Security-Policy", "script-src 'none'; default-src 'none'; img-src 'self'")
(*res).Header().Set("Content-Type", "text/plain") (*res).Header().Set("Content-Type", "text/plain")
// XML bomb // XML bomb
txt, _ := ioutil.ReadAll(reader) txt, _ := io.ReadAll(reader)
if regexp.MustCompile("(?is)entity").Match(txt) { if regexp.MustCompile("(?is)entity").Match(txt) {
txt = []byte("") txt = []byte("")
} }