diff options
Diffstat (limited to 'weed/util')
| -rw-r--r-- | weed/util/constants.go | 2 | ||||
| -rw-r--r-- | weed/util/http_util.go | 68 |
2 files changed, 68 insertions, 2 deletions
diff --git a/weed/util/constants.go b/weed/util/constants.go index 96cc83108..2c1e5c830 100644 --- a/weed/util/constants.go +++ b/weed/util/constants.go @@ -1,5 +1,5 @@ package util const ( - VERSION = "0.77" + VERSION = "0.90 beta" ) diff --git a/weed/util/http_util.go b/weed/util/http_util.go index ca9f7c50e..51bedcdfd 100644 --- a/weed/util/http_util.go +++ b/weed/util/http_util.go @@ -141,7 +141,6 @@ func GetBufferStream(url string, values url.Values, allocatedBytes []byte, eachB return err } } - return nil } func GetUrlStream(url string, values url.Values, readFn func(io.Reader) error) error { @@ -183,3 +182,70 @@ func NormalizeUrl(url string) string { } return "http://" + url } + +func ReadUrl(fileUrl string, offset int64, size int, buf []byte) (n int64, e error) { + + req, _ := http.NewRequest("GET", fileUrl, nil) + req.Header.Add("Range", fmt.Sprintf("bytes=%d-%d", offset, offset+int64(size))) + + r, err := client.Do(req) + if err != nil { + return 0, err + } + defer r.Body.Close() + if r.StatusCode >= 400 { + return 0, fmt.Errorf("%s: %s", fileUrl, r.Status) + } + + var i, m int + + for { + m, err = r.Body.Read(buf[i:]) + if m == 0 { + return + } + i += m + n += int64(m) + if err == io.EOF { + return n, nil + } + if e != nil { + return n, e + } + } + +} + +func ReadUrlAsStream(fileUrl string, offset int64, size int, fn func(data []byte)) (n int64, e error) { + + req, _ := http.NewRequest("GET", fileUrl, nil) + req.Header.Add("Range", fmt.Sprintf("bytes=%d-%d", offset, offset+int64(size))) + + r, err := client.Do(req) + if err != nil { + return 0, err + } + defer r.Body.Close() + if r.StatusCode >= 400 { + return 0, fmt.Errorf("%s: %s", fileUrl, r.Status) + } + + var m int + buf := make([]byte, 64*1024) + + for { + m, err = r.Body.Read(buf) + if m == 0 { + return + } + fn(buf[:m]) + n += int64(m) + if err == io.EOF { + return n, nil + } + if e != nil { + return n, e + } + } + +} |
