Skip to content

Commit

Permalink
mulitpart resume add GetResumeProcess
Browse files Browse the repository at this point in the history
mulitpart resume add GetResumeProcess
  • Loading branch information
huangnauh authored Oct 31, 2024
1 parent 14076d5 commit de08aa9
Show file tree
Hide file tree
Showing 14 changed files with 248 additions and 192 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
7 changes: 3 additions & 4 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,12 @@ linters:
- gosimple
- govet
- ineffassign
- interfacer
# - interfacer
- misspell
# - nakedret
- nolintlint
- scopelint
# - scopelint
- staticcheck
- structcheck
# - structcheck
- typecheck
- unconvert
- unparam
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
12 changes: 6 additions & 6 deletions upyun/fileinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ type FileInfo struct {
}

/*
Content-Type: image/gif
ETag: "dc9ea7257aa6da18e74505259b04a946"
x-upyun-file-type: GIF
x-upyun-height: 379
x-upyun-width: 500
x-upyun-frames: 90
Content-Type: image/gif
ETag: "dc9ea7257aa6da18e74505259b04a946"
x-upyun-file-type: GIF
x-upyun-height: 379
x-upyun-width: 500
x-upyun-frames: 90
*/
func parseHeaderToFileInfo(header http.Header, getinfo bool) *FileInfo {
fInfo := &FileInfo{}
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
105 changes: 45 additions & 60 deletions upyun/io.go
Original file line number Diff line number Diff line change
@@ -1,78 +1,63 @@
package upyun

import (
"bytes"
"crypto/md5"
"fmt"
"io"
"os"
)

type UpYunPutReader interface {
Len() (n int)
MD5() (ret string)
Read([]byte) (n int, err error)
Copyed() (n int)
}

type fragmentFile struct {
realFile *os.File
offset int64
limit int64
cursor int64
type Chunk struct {
buf io.Reader
buf2 *bytes.Buffer
id int
n int
}

func (f *fragmentFile) Seek(offset int64, whence int) (ret int64, err error) {
switch whence {
case 0:
f.cursor = offset
ret, err = f.realFile.Seek(f.offset+f.cursor, 0)
return ret - f.offset, err
default:
return 0, fmt.Errorf("whence must be 0")
func (c *Chunk) Read(b []byte) (n int, err error) {
if c.buf2 != nil {
return c.buf2.Read(b)
}
}

func (f *fragmentFile) Read(b []byte) (n int, err error) {
if f.cursor >= f.limit {
return 0, io.EOF
}
n, err = f.realFile.Read(b)
if f.cursor+int64(n) > f.limit {
n = int(f.limit - f.cursor)
}
f.cursor += int64(n)
return n, err
}

func (f *fragmentFile) Stat() (fInfo os.FileInfo, err error) {
return fInfo, fmt.Errorf("fragmentFile not implement Stat()")
}

func (f *fragmentFile) Close() error {
return nil
}

func (f *fragmentFile) Copyed() int {
return int(f.cursor - f.offset)
}

func (f *fragmentFile) Len() int {
return int(f.limit - f.offset)
}

func (f *fragmentFile) MD5() string {
s, _ := md5File(f)
return s
}

func newFragmentFile(file *os.File, offset, limit int64) (*fragmentFile, error) {
f := &fragmentFile{
realFile: file,
offset: offset,
limit: limit,
}

if _, err := f.Seek(0, 0); err != nil {
return nil, err
return c.buf.Read(b)
}

func (c *Chunk) Len() int {
return c.n
}
func (c *Chunk) ID() int {
return c.id
}

func (c *Chunk) MD5() string {
c.buf2 = bytes.NewBuffer(nil)
reader := io.TeeReader(c.buf, c.buf2)
hash := md5.New()
_, _ = io.Copy(hash, reader)
return fmt.Sprintf("%x", hash.Sum(nil))
}

func GetReadChunk(input io.Reader, size, partSize int64, ch chan *Chunk) {
id := 0
bytesLeft := size
for bytesLeft > 0 {
n := partSize
if bytesLeft <= partSize {
n = bytesLeft
}
reader := io.LimitReader(input, n)
ch <- &Chunk{
buf: reader,
id: id,
n: int(n),
}
id++
bytesLeft -= n
}
return f, nil
close(ch)
}
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
Loading

0 comments on commit de08aa9

Please sign in to comment.