diff options
| author | Chris Lu <chris.lu@gmail.com> | 2015-12-14 21:42:38 -0800 |
|---|---|---|
| committer | Chris Lu <chris.lu@gmail.com> | 2015-12-14 21:42:38 -0800 |
| commit | df5e54e02af60f6a1537cd5853def4dad42932bc (patch) | |
| tree | 1037c5cff81bfda50ca42a3249ebaeeb3774501e /go/util/http_util.go | |
| parent | 020dd480ed8dab0eeb3b6b25b2558084a51b26f2 (diff) | |
| parent | 031d26527f0ebe39bb26c8e8b4503168a849265a (diff) | |
| download | seaweedfs-df5e54e02af60f6a1537cd5853def4dad42932bc.tar.xz seaweedfs-df5e54e02af60f6a1537cd5853def4dad42932bc.zip | |
Merge pull request #224 from tnextday/feature/chunked-file-support
Feature/chunked file support
Diffstat (limited to 'go/util/http_util.go')
| -rw-r--r-- | go/util/http_util.go | 23 |
1 files changed, 18 insertions, 5 deletions
diff --git a/go/util/http_util.go b/go/util/http_util.go index 7854302ab..f80ab0c24 100644 --- a/go/util/http_util.go +++ b/go/util/http_util.go @@ -9,7 +9,10 @@ import ( "net/url" "strings" + "encoding/json" + "github.com/chrislusf/seaweedfs/go/security" + "github.com/syndtr/goleveldb/leveldb/errors" ) var ( @@ -79,10 +82,21 @@ func Delete(url string, jwt security.EncodedJwt) error { return e } defer resp.Body.Close() - if _, err := ioutil.ReadAll(resp.Body); err != nil { + body, err := ioutil.ReadAll(resp.Body) + if err != nil { return err } - return nil + switch resp.StatusCode { + case http.StatusNotFound, http.StatusAccepted, http.StatusOK: + return nil + } + m := make(map[string]interface{}) + if e := json.Unmarshal(body, m); e == nil { + if s, ok := m["error"].(string); ok { + return errors.New(s) + } + } + return errors.New(string(body)) } func GetBufferStream(url string, values url.Values, allocatedBytes []byte, eachBuffer func([]byte)) error { @@ -122,12 +136,11 @@ func GetUrlStream(url string, values url.Values, readFn func(io.Reader) error) e return readFn(r.Body) } -func DownloadUrl(fileUrl string) (filename string, content []byte, e error) { +func DownloadUrl(fileUrl string) (filename string, rc io.ReadCloser, e error) { response, err := client.Get(fileUrl) if err != nil { return "", nil, err } - defer response.Body.Close() contentDisposition := response.Header["Content-Disposition"] if len(contentDisposition) > 0 { if strings.HasPrefix(contentDisposition[0], "filename=") { @@ -135,7 +148,7 @@ func DownloadUrl(fileUrl string) (filename string, content []byte, e error) { filename = strings.Trim(filename, "\"") } } - content, e = ioutil.ReadAll(response.Body) + rc = response.Body return } |
