-
Notifications
You must be signed in to change notification settings - Fork 44
/
remotehttpindex.go
59 lines (50 loc) · 1.33 KB
/
remotehttpindex.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package desync
import (
"bytes"
"io"
"io/ioutil"
"net/url"
)
// RemoteHTTPIndex is a remote index store accessed via HTTP.
type RemoteHTTPIndex struct {
*RemoteHTTPBase
}
// NewRemoteHTTPIndexStore initializes a new store that pulls the specified index file via HTTP(S) from
// a remote web server.
func NewRemoteHTTPIndexStore(location *url.URL, opt StoreOptions) (*RemoteHTTPIndex, error) {
b, err := NewRemoteHTTPStoreBase(location, opt)
if err != nil {
return nil, err
}
return &RemoteHTTPIndex{b}, nil
}
// GetIndexReader returns an index reader from an HTTP store. Fails if the specified index
// file does not exist.
func (r RemoteHTTPIndex) GetIndexReader(name string) (rdr io.ReadCloser, e error) {
b, err := r.GetObject(name)
if err != nil {
return rdr, err
}
rc := ioutil.NopCloser(bytes.NewReader(b))
return rc, nil
}
// GetIndex returns an Index structure from the store
func (r *RemoteHTTPIndex) GetIndex(name string) (i Index, e error) {
ir, err := r.GetIndexReader(name)
if err != nil {
return i, err
}
return IndexFromReader(ir)
}
// StoreIndex adds a new chunk to the store
func (r *RemoteHTTPIndex) StoreIndex(name string, idx Index) error {
getReader := func() io.Reader {
rdr, w := io.Pipe()
go func() {
defer w.Close()
idx.WriteTo(w)
}()
return rdr
}
return r.StoreObject(name, getReader)
}