Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change: func Put return file info #39

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ func (up *UpYun) Mkdir(path string) error
#### 上传

```go
func (up *UpYun) Put(config *PutObjectConfig) (err error)
func (up *UpYun) Put(config *PutObjectConfig) (fInfo *FileInfo, err error)
```

#### 下载
Expand Down
7 changes: 5 additions & 2 deletions upyun/fileinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package upyun

import (
"net/http"
"path"
"strings"
"time"
)
Expand Down Expand Up @@ -32,8 +33,10 @@ type FileInfo struct {
x-upyun-width: 500
x-upyun-frames: 90
*/
func parseHeaderToFileInfo(header http.Header, getinfo bool) *FileInfo {
fInfo := &FileInfo{}
func parseHeaderToFileInfo(name string, header http.Header, getinfo bool) *FileInfo {
fInfo := &FileInfo{
Name: path.Join("/", name),
}
for k, v := range header {
lk := strings.ToLower(k)
if strings.HasPrefix(lk, "x-upyun-meta-") {
Expand Down
39 changes: 21 additions & 18 deletions upyun/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,7 @@ func (up *UpYun) Get(config *GetObjectConfig) (fInfo *FileInfo, err error) {
}
defer resp.Body.Close()

fInfo = parseHeaderToFileInfo(resp.Header, false)
fInfo.Name = config.Path
fInfo = parseHeaderToFileInfo(config.Path, resp.Header, false)

if fInfo.Size, err = io.Copy(config.Writer, resp.Body); err != nil {
return nil, fmt.Errorf("io copy: %v", err)
Expand All @@ -148,7 +147,7 @@ func (up *UpYun) Get(config *GetObjectConfig) (fInfo *FileInfo, err error) {
return
}

func (up *UpYun) put(config *PutObjectConfig) error {
func (up *UpYun) put(config *PutObjectConfig) (*FileInfo, error) {
/* Append Api Deprecated
if config.AppendContent {
if config.Headers == nil {
Expand All @@ -157,7 +156,8 @@ func (up *UpYun) put(config *PutObjectConfig) error {
config.Headers["X-Upyun-Append"] = "true"
}
*/
_, err := up.doRESTRequest(&restReqConfig{

resp, err := up.doRESTRequest(&restReqConfig{
method: "PUT",
uri: config.Path,
headers: config.Headers,
Expand All @@ -166,21 +166,22 @@ func (up *UpYun) put(config *PutObjectConfig) error {
useMD5: config.UseMD5,
})
if err != nil {
return fmt.Errorf("doRESTRequest: %v", err)
return nil, fmt.Errorf("doRESTRequest: %v", err)
}
return nil

return parseHeaderToFileInfo(config.Path, resp.Header, false), nil
}

// TODO: progress
func (up *UpYun) resumePut(config *PutObjectConfig) error {
func (up *UpYun) resumePut(config *PutObjectConfig) (*FileInfo, error) {
f, ok := config.Reader.(*os.File)
if !ok {
return fmt.Errorf("resumePut: type != *os.File")
return nil, fmt.Errorf("resumePut: type != *os.File")
}

fileinfo, err := f.Stat()
if err != nil {
return fmt.Errorf("Stat: %v", err)
return nil, fmt.Errorf("Stat: %v", err)
}

fsize := fileinfo.Size()
Expand Down Expand Up @@ -223,7 +224,7 @@ func (up *UpYun) resumePut(config *PutObjectConfig) error {

fragFile, err := newFragmentFile(f, curSize, partSize)
if err != nil {
return fmt.Errorf("newFragmentFile: %v", err)
return nil, fmt.Errorf("newFragmentFile: %v", err)
}

try := 0
Expand All @@ -241,34 +242,37 @@ func (up *UpYun) resumePut(config *PutObjectConfig) error {
break
}
if _, ok := err.(net.Error); !ok {
return fmt.Errorf("doRESTRequest: %v", err)
return nil, fmt.Errorf("doRESTRequest: %v", err)
}
fragFile.Seek(0, 0)
}

if config.MaxResumePutTries > 0 && try == config.MaxResumePutTries {
return err
return nil, err
}

if id == 0 {
headers["X-Upyun-Multi-UUID"] = resp.Header.Get("X-Upyun-Multi-UUID")
} else {
if id == maxPartID {
return nil
if resp != nil {
return parseHeaderToFileInfo(config.Path, resp.Header, false), nil
}
return nil, nil
}
}

curSize += partSize
}

return nil
return nil, nil
}

func (up *UpYun) Put(config *PutObjectConfig) (err error) {
func (up *UpYun) Put(config *PutObjectConfig) (fInfo *FileInfo, err error) {
if config.LocalPath != "" {
var fd *os.File
if fd, err = os.Open(config.LocalPath); err != nil {
return fmt.Errorf("open file: %v", err)
return nil, fmt.Errorf("open file: %v", err)
}
defer fd.Close()
config.Reader = fd
Expand Down Expand Up @@ -314,8 +318,7 @@ func (up *UpYun) GetInfo(path string) (*FileInfo, error) {
}
return nil, fmt.Errorf("getinfo %s: %v", path, err)
}
fInfo := parseHeaderToFileInfo(resp.Header, true)
fInfo.Name = path
fInfo := parseHeaderToFileInfo(path, resp.Header, true)
return fInfo, nil
}

Expand Down
6 changes: 3 additions & 3 deletions upyun/rest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func TestPutWithFileReader(t *testing.T) {
NotNil(t, fd)
defer fd.Close()

err := up.Put(&PutObjectConfig{
_, err := up.Put(&PutObjectConfig{
Path: REST_FILE_1,
Reader: fd,
Headers: map[string]string{
Expand All @@ -68,7 +68,7 @@ func TestPutWithBuffer(t *testing.T) {
s := BUF_CONTENT
r := strings.NewReader(s)

err := up.Put(&PutObjectConfig{
_, err := up.Put(&PutObjectConfig{
Path: REST_FILE_BUF,
Reader: r,
Headers: map[string]string{
Expand Down Expand Up @@ -114,7 +114,7 @@ func TestResumePut(t *testing.T) {

defer os.RemoveAll(fname)

err := up.Put(&PutObjectConfig{
_, err := up.Put(&PutObjectConfig{
Path: REST_FILE_1M,
LocalPath: fname,
UseMD5: true,
Expand Down