Skip to content

Commit

Permalink
调整ioutil为go1.16推荐语法 (flipped-aurora#1209)
Browse files Browse the repository at this point in the history
* perf:根据go1.16后的提议修改包使用

* perf:根据go1.16后的提议修改包使用以及修改中间件的代码冗余
  • Loading branch information
XiaoK29 authored Aug 29, 2022
1 parent cda8f6d commit 23e2b9f
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 34 deletions.
4 changes: 2 additions & 2 deletions server/api/v1/example/exa_breakpoint_continue.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package example

import (
"fmt"
"io/ioutil"
"io"
"mime/multipart"
"strconv"

Expand Down Expand Up @@ -48,7 +48,7 @@ func (b *FileUploadAndDownloadApi) BreakpointContinue(c *gin.Context) {
fmt.Println(err)
}
}(f)
cen, _ := ioutil.ReadAll(f)
cen, _ := io.ReadAll(f)
if !utils.CheckMd5(cen, chunkMd5) {
global.GVA_LOG.Error("检查md5失败!", zap.Error(err))
response.FailWithMessage("检查md5失败", c)
Expand Down
6 changes: 3 additions & 3 deletions server/middleware/email.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package middleware

import (
"bytes"
"io/ioutil"
"io"
"strconv"
"time"

Expand Down Expand Up @@ -32,9 +32,9 @@ func ErrorToEmail() gin.HandlerFunc {
}
username = user.Username
}
body, _ := ioutil.ReadAll(c.Request.Body)
body, _ := io.ReadAll(c.Request.Body)
// 再重新写回请求体body中,ioutil.ReadAll会清空c.Request.Body中的数据
c.Request.Body = ioutil.NopCloser(bytes.NewBuffer(body))
c.Request.Body = io.NopCloser(bytes.NewBuffer(body))
record := system.SysOperationRecord{
Ip: c.ClientIP(),
Method: c.Request.Method,
Expand Down
4 changes: 2 additions & 2 deletions server/middleware/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"io"
"strings"
"time"

Expand Down Expand Up @@ -47,7 +47,7 @@ func (l Logger) SetLoggerMiddleware() gin.HandlerFunc {
if l.Filter != nil && !l.Filter(c) {
body, _ = c.GetRawData()
// 将原body塞回去
c.Request.Body = ioutil.NopCloser(bytes.NewBuffer(body))
c.Request.Body = io.NopCloser(bytes.NewBuffer(body))
}
c.Next()
cost := time.Since(start)
Expand Down
26 changes: 13 additions & 13 deletions server/middleware/operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package middleware
import (
"bytes"
"encoding/json"
"io/ioutil"
"io"
"net/http"
"net/url"
"strconv"
Expand Down Expand Up @@ -36,11 +36,11 @@ func OperationRecord() gin.HandlerFunc {
var userId int
if c.Request.Method != http.MethodGet {
var err error
body, err = ioutil.ReadAll(c.Request.Body)
body, err = io.ReadAll(c.Request.Body)
if err != nil {
global.GVA_LOG.Error("read body from request error:", zap.Error(err))
} else {
c.Request.Body = ioutil.NopCloser(bytes.NewBuffer(body))
c.Request.Body = io.NopCloser(bytes.NewBuffer(body))
}
} else {
query := c.Request.URL.RawQuery
Expand Down Expand Up @@ -75,7 +75,7 @@ func OperationRecord() gin.HandlerFunc {
}

// 上传文件时候 中间件日志进行裁断操作
if strings.Index(c.GetHeader("Content-Type"), "multipart/form-data") > -1 {
if strings.Contains(c.GetHeader("Content-Type"), "multipart/form-data") {
if len(record.Body) > 1024 {
// 截断
newBody := respPool.Get().([]byte)
Expand All @@ -100,15 +100,15 @@ func OperationRecord() gin.HandlerFunc {
record.Latency = latency
record.Resp = writer.body.String()

if strings.Index(c.Writer.Header().Get("Pragma"), "public") > -1 ||
strings.Index(c.Writer.Header().Get("Expires"), "0") > -1 ||
strings.Index(c.Writer.Header().Get("Cache-Control"), "must-revalidate, post-check=0, pre-check=0") > -1 ||
strings.Index(c.Writer.Header().Get("Content-Type"), "application/force-download") > -1 ||
strings.Index(c.Writer.Header().Get("Content-Type"), "application/octet-stream") > -1 ||
strings.Index(c.Writer.Header().Get("Content-Type"), "application/vnd.ms-excel") > -1 ||
strings.Index(c.Writer.Header().Get("Content-Type"), "application/download") > -1 ||
strings.Index(c.Writer.Header().Get("Content-Disposition"), "attachment") > -1 ||
strings.Index(c.Writer.Header().Get("Content-Transfer-Encoding"), "binary") > -1 {
if strings.Contains(c.Writer.Header().Get("Pragma"), "public") ||
strings.Contains(c.Writer.Header().Get("Expires"), "0") ||
strings.Contains(c.Writer.Header().Get("Cache-Control"), "must-revalidate, post-check=0, pre-check=0") ||
strings.Contains(c.Writer.Header().Get("Content-Type"), "application/force-download") ||
strings.Contains(c.Writer.Header().Get("Content-Type"), "application/octet-stream") ||
strings.Contains(c.Writer.Header().Get("Content-Type"), "application/vnd.ms-excel") ||
strings.Contains(c.Writer.Header().Get("Content-Type"), "application/download") ||
strings.Contains(c.Writer.Header().Get("Content-Disposition"), "attachment") ||
strings.Contains(c.Writer.Header().Get("Content-Transfer-Encoding"), "binary") {
if len(record.Resp) > 1024 {
// 截断
newBody := respPool.Get().([]byte)
Expand Down
3 changes: 1 addition & 2 deletions server/packfile/usePackFile.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package packfile

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
Expand All @@ -24,7 +23,7 @@ func writeFile(path string, data []byte) {

// 已存在的文件,不应该覆盖重写,可能在前端更改了配置文件等
if _, err := os.Stat(path); os.IsNotExist(err) {
if err2 := ioutil.WriteFile(path, data, os.ModePerm); err2 != nil {
if err2 := os.WriteFile(path, data, os.ModePerm); err2 != nil {
fmt.Printf("Write file failed: %s\n", path)
}
} else {
Expand Down
7 changes: 3 additions & 4 deletions server/service/system/sys_auto_code.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"go/parser"
"go/token"
"io"
"io/ioutil"
"log"
"mime/multipart"
"os"
Expand Down Expand Up @@ -201,7 +200,7 @@ func (autoCodeService *AutoCodeService) PreviewTemp(autoCode system.AutoCodeStru
builder.WriteString(strings.Replace(ext, ".", "", -1))
}
builder.WriteString("\n\n")
data, err := ioutil.ReadAll(f)
data, err := io.ReadAll(f)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -372,7 +371,7 @@ func (autoCodeService *AutoCodeService) CreateTemp(autoCode system.AutoCodeStruc
//@return: []string, error

func (autoCodeService *AutoCodeService) GetAllTplFile(pathName string, fileList []string) ([]string, error) {
files, err := ioutil.ReadDir(pathName)
files, err := os.ReadDir(pathName)
for _, fi := range files {
if fi.IsDir() {
fileList, err = autoCodeService.GetAllTplFile(pathName+"/"+fi.Name(), fileList)
Expand Down Expand Up @@ -816,7 +815,7 @@ func ImportReference(filepath, importCode, structName, packageName, groupName st
log.Fatal(err)
}
// 写回数据
return ioutil.WriteFile(filepath, buffer.Bytes(), 0o600)
return os.WriteFile(filepath, buffer.Bytes(), 0o600)
}

// CreatePlug 自动创建插件模板
Expand Down
5 changes: 2 additions & 3 deletions server/utils/breakpoint_continue.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package utils

import (
"io/ioutil"
"os"
"strconv"
)
Expand Down Expand Up @@ -75,7 +74,7 @@ func makeFileContent(content []byte, fileName string, FileDir string, contentNum
//@return: error, string

func MakeFile(fileName string, FileMd5 string) (string, error) {
rd, err := ioutil.ReadDir(breakpointDir + FileMd5)
rd, err := os.ReadDir(breakpointDir + FileMd5)
if err != nil {
return finishDir + fileName, err
}
Expand All @@ -86,7 +85,7 @@ func MakeFile(fileName string, FileMd5 string) (string, error) {
}
defer fd.Close()
for k := range rd {
content, _ := ioutil.ReadFile(breakpointDir + FileMd5 + "/" + fileName + "_" + strconv.Itoa(k))
content, _ := os.ReadFile(breakpointDir + FileMd5 + "/" + fileName + "_" + strconv.Itoa(k))
_, err = fd.Write(content)
if err != nil {
_ = os.Remove(finishDir + fileName)
Expand Down
10 changes: 5 additions & 5 deletions server/utils/injection_code.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"go/ast"
"go/parser"
"go/token"
"io/ioutil"
"os"
"strings"
)

Expand All @@ -28,7 +28,7 @@ const (
//@return: error

func AutoInjectionCode(filepath string, funcName string, codeData string) error {
srcData, err := ioutil.ReadFile(filepath)
srcData, err := os.ReadFile(filepath)
if err != nil {
return err
}
Expand Down Expand Up @@ -113,7 +113,7 @@ func AutoInjectionCode(filepath string, funcName string, codeData string) error
srcData = append(append(srcData[:indexPos], insertData...), remainData...)

// 写回数据
return ioutil.WriteFile(filepath, srcData, 0o600)
return os.WriteFile(filepath, srcData, 0o600)
}

func checkExist(srcData *[]byte, startPos int, endPos int, blockStmt *ast.BlockStmt, target string) bool {
Expand Down Expand Up @@ -153,15 +153,15 @@ func checkExist(srcData *[]byte, startPos int, endPos int, blockStmt *ast.BlockS
}

func AutoClearCode(filepath string, codeData string) error {
srcData, err := ioutil.ReadFile(filepath)
srcData, err := os.ReadFile(filepath)
if err != nil {
return err
}
srcData, err = cleanCode(codeData, string(srcData))
if err != nil {
return err
}
return ioutil.WriteFile(filepath, srcData, 0o600)
return os.WriteFile(filepath, srcData, 0o600)
}

func cleanCode(clearCode string, srcData string) ([]byte, error) {
Expand Down
1 change: 1 addition & 0 deletions server/utils/plugin/plugin_uinx.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ func LoadPlugin(path string) error {
return err
}
if fileInfo.IsDir() {
// TODO 返回的参数不一样暂时不修改(https://golang.google.cn/doc/go1.16#ioutil)
fileSlice, err := ioutil.ReadDir(path)
if err != nil {
return err
Expand Down

0 comments on commit 23e2b9f

Please sign in to comment.