mirror of https://github.com/perkeep/perkeep.git
added Fetch to Client
This commit is contained in:
parent
0de3dc28e3
commit
8933895fd1
|
@ -45,3 +45,7 @@ func (c *Client) Stats() Stats {
|
|||
defer c.statsMutex.Unlock()
|
||||
return c.stats // copy
|
||||
}
|
||||
|
||||
func (c *Client) authHeader() string {
|
||||
return "Basic " + encodeBase64("username:" + c.password)
|
||||
}
|
||||
|
|
|
@ -1 +1,37 @@
|
|||
package client
|
||||
|
||||
import (
|
||||
"camli/blobref"
|
||||
"camli/http"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func (c *Client) Fetch(b *blobref.BlobRef) (blobref.ReadSeekCloser, int64, os.Error) {
|
||||
url := fmt.Sprintf("%s/camli/%s", c.server, b)
|
||||
|
||||
req := http.NewGetRequest(url)
|
||||
req.Header["Authorization"] = c.authHeader()
|
||||
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)
|
||||
}
|
||||
|
||||
return nopSeeker{resp.Body}, size, nil
|
||||
}
|
||||
|
||||
type nopSeeker struct {
|
||||
io.ReadCloser
|
||||
}
|
||||
|
||||
func (n nopSeeker) Seek(offset int64, whence int) (ret int64, err os.Error) {
|
||||
return 0, os.NewError("seek unsupported")
|
||||
}
|
||||
|
||||
|
|
|
@ -61,7 +61,6 @@ func (c *Client) Upload(h *UploadHandle) (*PutResult, os.Error) {
|
|||
c.stats.UploadRequests.Bytes += h.Size
|
||||
c.statsMutex.Unlock()
|
||||
|
||||
authHeader := "Basic " + encodeBase64("username:" + c.password)
|
||||
blobRefString := h.BlobRef.String()
|
||||
|
||||
// Pre-upload. Check whether the blob already exists on the
|
||||
|
@ -72,7 +71,7 @@ func (c *Client) Upload(h *UploadHandle) (*PutResult, os.Error) {
|
|||
url,
|
||||
"application/x-www-form-urlencoded",
|
||||
strings.NewReader(requestBody))
|
||||
req.Header["Authorization"] = authHeader
|
||||
req.Header["Authorization"] = c.authHeader()
|
||||
req.ContentLength = int64(len(requestBody))
|
||||
req.TransferEncoding = nil
|
||||
|
||||
|
@ -123,7 +122,7 @@ func (c *Client) Upload(h *UploadHandle) (*PutResult, os.Error) {
|
|||
strings.NewReader(multiPartHeader),
|
||||
h.Contents,
|
||||
strings.NewReader(multiPartFooter)))
|
||||
req.Header["Authorization"] = authHeader
|
||||
req.Header["Authorization"] = c.authHeader()
|
||||
req.ContentLength = int64(len(multiPartHeader)) + h.Size + int64(len(multiPartFooter))
|
||||
req.TransferEncoding = nil
|
||||
resp, err = req.Send()
|
||||
|
|
Loading…
Reference in New Issue