2011-01-02 22:36:03 +00:00
|
|
|
package client
|
2011-01-15 01:22:45 +00:00
|
|
|
|
|
|
|
import (
|
2011-01-26 05:33:19 +00:00
|
|
|
"bytes"
|
2011-01-15 01:22:45 +00:00
|
|
|
"camli/blobref"
|
|
|
|
"camli/http"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"strconv"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (c *Client) Fetch(b *blobref.BlobRef) (blobref.ReadSeekCloser, int64, os.Error) {
|
2011-01-26 05:33:19 +00:00
|
|
|
return c.FetchVia(b, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) FetchVia(b *blobref.BlobRef, v []*blobref.BlobRef) (blobref.ReadSeekCloser, int64, os.Error) {
|
2011-01-15 01:22:45 +00:00
|
|
|
url := fmt.Sprintf("%s/camli/%s", c.server, b)
|
|
|
|
|
2011-01-26 05:33:19 +00:00
|
|
|
if len(v) > 0 {
|
|
|
|
buf := bytes.NewBufferString(url)
|
|
|
|
buf.WriteString("?via=")
|
|
|
|
for i, br := range v {
|
|
|
|
if i != 0 {
|
|
|
|
buf.WriteString(",")
|
|
|
|
}
|
|
|
|
buf.WriteString(br.String())
|
|
|
|
}
|
|
|
|
url = buf.String()
|
|
|
|
}
|
|
|
|
|
2011-01-15 01:22:45 +00:00
|
|
|
req := http.NewGetRequest(url)
|
2011-01-26 05:33:19 +00:00
|
|
|
|
|
|
|
if c.HasAuthCredentials() {
|
|
|
|
req.Header["Authorization"] = c.authHeader()
|
|
|
|
}
|
2011-01-15 01:22:45 +00:00
|
|
|
resp, err := req.Send()
|
|
|
|
if err != nil {
|
|
|
|
return nil, 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var size int64
|
|
|
|
if s := resp.GetHeader("Content-Length"); s != "" {
|
|
|
|
size, _ = strconv.Atoi64(s)
|
|
|
|
}
|
|
|
|
|
2011-01-26 05:33:19 +00:00
|
|
|
return nopSeeker{resp.Body}, size, nil
|
2011-01-15 01:22:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type nopSeeker struct {
|
|
|
|
io.ReadCloser
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n nopSeeker) Seek(offset int64, whence int) (ret int64, err os.Error) {
|
|
|
|
return 0, os.NewError("seek unsupported")
|
|
|
|
}
|
|
|
|
|