Move SaveFile to Archiver, add blobs status

This commit is contained in:
Alexander Neumann
2014-11-17 23:28:51 +01:00
parent 94d1482888
commit fe92062735
2 changed files with 51 additions and 60 deletions
-53
View File
@@ -3,13 +3,9 @@ package khepri
import (
"encoding/json"
"errors"
"io"
"io/ioutil"
"os"
"sync"
"github.com/fd0/khepri/backend"
"github.com/fd0/khepri/chunker"
)
type ContentHandler struct {
@@ -118,55 +114,6 @@ func (ch *ContentHandler) SaveJSON(t backend.Type, item interface{}) (*Blob, err
return ch.Save(t, backend.Compress(data))
}
// SaveFile stores the content of the file on the backend as a Blob by calling
// Save for each chunk.
func (ch *ContentHandler) SaveFile(filename string, size uint) (Blobs, error) {
file, err := os.Open(filename)
defer file.Close()
if err != nil {
return nil, err
}
// if the file is small enough, store it directly
if size < chunker.MinSize {
buf, err := ioutil.ReadAll(file)
if err != nil {
return nil, err
}
blob, err := ch.Save(backend.Data, buf)
if err != nil {
return nil, err
}
return Blobs{blob}, nil
}
// else store all chunks
blobs := Blobs{}
chunker := chunker.New(file)
for {
chunk, err := chunker.Next()
if err == io.EOF {
break
}
if err != nil {
return nil, err
}
blob, err := ch.Save(backend.Data, chunk.Data)
if err != nil {
return nil, err
}
blobs = append(blobs, blob)
}
return blobs, nil
}
// Load tries to load and decrypt content identified by t and id from the backend.
func (ch *ContentHandler) Load(t backend.Type, id backend.ID) ([]byte, error) {
if t == backend.Snapshot {