1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
16
17 package mongo
18
19 import (
20 "bytes"
21 "context"
22 "fmt"
23 "io"
24
25 "gopkg.in/mgo.v2/bson"
26 "perkeep.org/pkg/blob"
27 )
28
29 func (m *mongoStorage) Fetch(ctx context.Context, ref blob.Ref) (io.ReadCloser, uint32, error) {
30 var b blobDoc
31 err := m.c.Find(bson.M{"key": ref.String()}).One(&b)
32 if err != nil {
33 return nil, 0, err
34 }
35 if len(b.Blob) != int(b.Size) {
36 return nil, 0, fmt.Errorf("blob data size %d doesn't match meta size %d", len(b.Blob), b.Size)
37 }
38 return io.NopCloser(bytes.NewReader(b.Blob)), b.Size, nil
39 }