2011-01-28 07:07:18 +00:00
|
|
|
/*
|
|
|
|
Copyright 2011 Google Inc.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
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"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2011-04-09 06:20:24 +00:00
|
|
|
"log"
|
2011-01-15 01:22:45 +00:00
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
2011-04-09 06:20:24 +00:00
|
|
|
var _ = log.Printf
|
|
|
|
|
2011-03-25 02:22:35 +00:00
|
|
|
func (c *Client) FetchStreaming(b *blobref.BlobRef) (io.ReadCloser, int64, os.Error) {
|
2011-01-26 05:33:19 +00:00
|
|
|
return c.FetchVia(b, nil)
|
|
|
|
}
|
|
|
|
|
2011-03-25 02:22:35 +00:00
|
|
|
func (c *Client) FetchVia(b *blobref.BlobRef, v []*blobref.BlobRef) (io.ReadCloser, 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())
|
|
|
|
}
|
2011-05-10 19:56:40 +00:00
|
|
|
url = buf.String()
|
2011-01-26 05:33:19 +00:00
|
|
|
}
|
|
|
|
|
2011-03-02 02:02:01 +00:00
|
|
|
req := c.newRequest("GET", url)
|
2011-05-10 13:25:18 +00:00
|
|
|
resp, err := c.httpClient.Do(req)
|
2011-01-15 01:22:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, 0, err
|
|
|
|
}
|
|
|
|
|
2011-03-05 03:02:26 +00:00
|
|
|
if resp.StatusCode != 200 {
|
|
|
|
return nil, 0, os.NewError(fmt.Sprintf("Got status code %d from blobserver for %s", resp.StatusCode, b))
|
|
|
|
}
|
|
|
|
|
2011-03-03 05:39:58 +00:00
|
|
|
size := resp.ContentLength
|
|
|
|
if size == -1 {
|
|
|
|
return nil, 0, os.NewError("blobserver didn't return a Content-Length for blob")
|
2011-01-15 01:22:45 +00:00
|
|
|
}
|
|
|
|
|
2011-03-25 02:22:35 +00:00
|
|
|
return resp.Body, size, nil
|
2011-01-15 01:22:45 +00:00
|
|
|
}
|