Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
huangnauh committed Oct 30, 2024
1 parent 9fa7c61 commit 9c643fa
Show file tree
Hide file tree
Showing 11 changed files with 128 additions and 99 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
uses: golangci/golangci-lint-action@v3
with:
# Optional: golangci-lint command line arguments.
version: v1.47.3
version: latest
args:
# Optional: working directory, useful for monorepos
# working-directory: somedir
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module github.com/upyun/go-sdk/v3

go 1.13
go 1.17
4 changes: 2 additions & 2 deletions upyun/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"io"
"net/http"
)

Expand Down Expand Up @@ -35,7 +35,7 @@ func checkResponse(res *http.Response) error {
uerr.StatusCode = res.StatusCode
uerr.Header = res.Header
defer res.Body.Close()
slurp, err := ioutil.ReadAll(res.Body)
slurp, err := io.ReadAll(res.Body)
if err != nil {
return uerr
}
Expand Down
3 changes: 1 addition & 2 deletions upyun/form.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"mime/multipart"
"net/http"
"os"
Expand Down Expand Up @@ -91,7 +90,7 @@ func (up *UpYun) FormUpload(config *FormUploadConfig) (*FormUploadResp, error) {
return nil, err
}

b, err := ioutil.ReadAll(resp.Body)
b, err := io.ReadAll(resp.Body)
resp.Body.Close()

if err != nil {
Expand Down
6 changes: 3 additions & 3 deletions upyun/process.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"
"net/http"
"path"
"strings"
Expand Down Expand Up @@ -131,7 +131,7 @@ func (up *UpYun) doProcessRequest(method, uri string,
return errorOperation("process", err)
}

b, err := ioutil.ReadAll(resp.Body)
b, err := io.ReadAll(resp.Body)
resp.Body.Close()
if err != nil {
return errorOperation("process read body", err)
Expand Down Expand Up @@ -215,7 +215,7 @@ func (up *UpYun) doSyncProcessRequest(method, uri string, payload string) (map[s
return nil, errorOperation("sync process", err)
}

b, err := ioutil.ReadAll(resp.Body)
b, err := io.ReadAll(resp.Body)
resp.Body.Close()
if err != nil {
return nil, err
Expand Down
8 changes: 4 additions & 4 deletions upyun/process_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package upyun

import (
"encoding/json"
"io/ioutil"
"io"
"net/http"
"path"
"testing"
Expand Down Expand Up @@ -69,7 +69,7 @@ func TestNagaResult(t *testing.T) {
Equal(t, len(res), 2)
}

//由于是异步操作,不能确保文件已存在
// 由于是异步操作,不能确保文件已存在
func TestImgaudit(t *testing.T) {
task := map[string]interface{}{
"url": JPG_URL,
Expand Down Expand Up @@ -99,7 +99,7 @@ func TestImgaudit(t *testing.T) {

}

//由于是异步操作,不能确保文件已存在
// 由于是异步操作,不能确保文件已存在
func TestVideoaudit(t *testing.T) {
task := map[string]interface{}{
"url": MP4_URL,
Expand Down Expand Up @@ -156,7 +156,7 @@ func TestFaceDetect(t *testing.T) {
resp, err := http.Get(FACE_URL + "!/face/detection")
Nil(t, err)
defer resp.Body.Close()
b, err := ioutil.ReadAll(resp.Body)
b, err := io.ReadAll(resp.Body)
Nil(t, err)
if err != nil {
var result map[string]interface{}
Expand Down
4 changes: 2 additions & 2 deletions upyun/purge.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package upyun

import (
"encoding/json"
"io/ioutil"
"io"
URL "net/url"
"strings"
"time"
Expand Down Expand Up @@ -33,7 +33,7 @@ func (up *UpYun) Purge(urls []string) (fails []string, err error) {
}
defer resp.Body.Close()

content, err := ioutil.ReadAll(resp.Body)
content, err := io.ReadAll(resp.Body)
if err != nil {
return fails, errorOperation("purge read body", err)
}
Expand Down
55 changes: 39 additions & 16 deletions upyun/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"fmt"
"io"
"io/fs"
"io/ioutil"
"net"
"net/http"
"os"
Expand Down Expand Up @@ -79,7 +78,7 @@ type GetRequestConfig struct {
Headers map[string]string
}

type ProxyReader func(size, offset int64, r io.Reader) io.Reader
type ProxyReader func(offset int64, r io.Reader) io.Reader

// PutObjectConfig provides a configuration to Put method.
type PutObjectConfig struct {
Expand Down Expand Up @@ -281,17 +280,18 @@ func getPartInfo(partSize, fsize int64) (int64, int64, error) {
return partSize, partNum, nil
}

func (up *UpYun) getMultipartUploadProcess(config *PutObjectConfig, fsize int64) (*ResumeProcessResult, error) {
func (up *UpYun) getMultipartUploadProcess(config *PutObjectConfig, fileinfo os.FileInfo) (*ResumeProcessResult, error) {
resumeProcessResult, _ := up.GetResumeProcess(config.Path)
if resumeProcessResult != nil {
if resumeProcessResult.Order {
if resumeProcessResult != nil && resumeProcessResult.Order {
if fileinfo.Size() == resumeProcessResult.Size && fileinfo.ModTime().Unix() <= resumeProcessResult.CreateTime.Unix() {
// fmt.Printf("continue process: %s next: %d\n", config.Path, resumeProcessResult.NextPartID)
return resumeProcessResult, nil
}
}

initMultipartUploadConfig := &InitMultipartUploadConfig{
Path: config.Path,
ContentLength: fsize,
ContentLength: fileinfo.Size(),
PartSize: config.ResumePartSize,
ContentType: config.Headers["Content-Type"],
OrderUpload: true,
Expand Down Expand Up @@ -341,7 +341,7 @@ func (up *UpYun) resumePut(config *PutObjectConfig) error {
maxPartID := int((fsize+config.ResumePartSize-1)/config.ResumePartSize - 1)

if breakpoint == nil || isRecordExpired(fileinfo, breakpoint) {
uploadProcess, err := up.getMultipartUploadProcess(config, fsize)
uploadProcess, err := up.getMultipartUploadProcess(config, fileinfo)
if err != nil {
return err
}
Expand Down Expand Up @@ -532,7 +532,7 @@ func (up *UpYun) ListMultipartUploads(config *ListMultipartConfig) (*ListMultipa
return nil, errorOperation("list multipart", err)
}

body, err := ioutil.ReadAll(res.Body)
body, err := io.ReadAll(res.Body)
if err != nil {
return nil, errorOperation("list multipart read body", err)
}
Expand Down Expand Up @@ -563,7 +563,7 @@ func (up *UpYun) ListMultipartParts(intiResult *InitMultipartUploadResult, confi
return nil, errorOperation("list multipart parts", err)
}

body, err := ioutil.ReadAll(res.Body)
body, err := io.ReadAll(res.Body)
if err != nil {
return nil, errorOperation("list multipart parts read body", err)
}
Expand Down Expand Up @@ -688,7 +688,7 @@ func (up *UpYun) List(config *GetObjectsConfig) error {
return errorOperation("list", err)
}

b, err := ioutil.ReadAll(resp.Body)
b, err := io.ReadAll(resp.Body)
resp.Body.Close()
if err != nil {
return errorOperation("list read body", err)
Expand Down Expand Up @@ -796,7 +796,7 @@ func (up *UpYun) ListObjects(config *ListObjectsConfig) (fileInfos []*FileInfo,
}

// 读取列表
b, err := ioutil.ReadAll(resp.Body)
b, err := io.ReadAll(resp.Body)
resp.Body.Close()
if err != nil {
return nil, "", errorOperation("list read body", err)
Expand Down Expand Up @@ -898,7 +898,7 @@ func (up *UpYun) doRESTRequest(config *restReqConfig) (*http.Response, error) {
}

if config.closeBody {
io.Copy(ioutil.Discard, resp.Body)
io.Copy(io.Discard, resp.Body)
resp.Body.Close()
}

Expand All @@ -914,16 +914,19 @@ type BreakPointConfig struct {
LastTime time.Time
}

func (up *UpYun) resumeUploadPart(config *PutObjectConfig, breakpoint *BreakPointConfig, f io.Reader, fileInfo fs.FileInfo) (*BreakPointConfig, error) {
func (up *UpYun) resumeUploadPart(config *PutObjectConfig, breakpoint *BreakPointConfig, f io.ReadSeeker, fileInfo fs.FileInfo) (*BreakPointConfig, error) {
fsize := fileInfo.Size()
partID := breakpoint.PartID
curSize, partSize := int64(partID)*breakpoint.PartSize, breakpoint.PartSize
bytesLeft := fsize - curSize
ch := make(chan *Chunk, 1)
var err error
if curSize > 0 {
f.Seek(curSize, 0)
}
var reader io.Reader
if config.ProxyReader != nil {
reader = config.ProxyReader(fsize, curSize, f)
reader = config.ProxyReader(curSize, f)
} else {
reader = f
}
Expand Down Expand Up @@ -974,17 +977,19 @@ type ResumeDisorderResult struct {
}

type ResumeProcessResult struct {
CreateTime time.Time
UploadID string
Path string
Order bool
Size int64
NextPartSize int64
NextPartID int64
Parts []*DisorderPart
}

func (up *UpYun) GetResumeProcess(path string) (*ResumeProcessResult, error) {
var partID int64
var partSize int64
var partID, partSize, size int64
var createTime time.Time

headers := make(map[string]string)
headers["X-Upyun-Multi-Info"] = "true"
Expand All @@ -1002,7 +1007,9 @@ func (up *UpYun) GetResumeProcess(path string) (*ResumeProcessResult, error) {
partSizeStr := resp.Header.Get("X-Upyun-Next-Part-Size")
partIDStr := resp.Header.Get("X-Upyun-Next-Part-Id")
uploadID := resp.Header.Get("X-Upyun-Multi-Uuid")
sizeStr := resp.Header.Get("X-Upyun-Multi-Length")
order := resp.Header.Get("X-Upyun-Meta-Order")
createStr := resp.Header.Get("X-Upyun-Created-Date")
o := true
if order == "false" {
o = false
Expand All @@ -1020,6 +1027,20 @@ func (up *UpYun) GetResumeProcess(path string) (*ResumeProcessResult, error) {
return nil, errorOperation(fmt.Sprintf("GetResumeProcess parse partIDStr %s", partIDStr), err)
}
}

if sizeStr != "" {
size, err = strconv.ParseInt(sizeStr, 10, 64)
if err != nil {
return nil, errorOperation(fmt.Sprintf("GetResumeProcess parse size %s", sizeStr), err)
}
}
if createStr != "" {
createTime, err = time.Parse(time.RFC1123, createStr)
if err != nil {
return nil, errorOperation(fmt.Sprintf("GetResumeProcess parse time %s", createStr), err)
}
}

var disorderRes ResumeDisorderResult

b, err := io.ReadAll(resp.Body)
Expand All @@ -1038,6 +1059,8 @@ func (up *UpYun) GetResumeProcess(path string) (*ResumeProcessResult, error) {
NextPartID: partID,
Path: path,
Order: o,
Size: size,
CreateTime: createTime,
Parts: disorderRes.Parts,
}, nil
}
Loading

0 comments on commit 9c643fa

Please sign in to comment.