mirror of
https://github.com/restic/restic.git
synced 2025-01-03 13:45:20 +00:00
server: use streams for LoadJSON*()
This commit is contained in:
parent
907d401e32
commit
68911ef11d
1 changed files with 24 additions and 14 deletions
38
server.go
38
server.go
|
@ -1,6 +1,7 @@
|
||||||
package restic
|
package restic
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"compress/zlib"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
@ -107,29 +108,38 @@ func (s Server) LoadID(t backend.Type, storageID backend.ID) ([]byte, error) {
|
||||||
// LoadJSON calls Load() to get content from the backend and afterwards calls
|
// LoadJSON calls Load() to get content from the backend and afterwards calls
|
||||||
// json.Unmarshal on the item.
|
// json.Unmarshal on the item.
|
||||||
func (s Server) LoadJSON(t backend.Type, blob Blob, item interface{}) error {
|
func (s Server) LoadJSON(t backend.Type, blob Blob, item interface{}) error {
|
||||||
// load from backend
|
return s.LoadJSONID(t, blob.Storage, item)
|
||||||
buf, err := s.Load(t, blob)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// inflate and unmarshal
|
|
||||||
err = json.Unmarshal(backend.Uncompress(buf), item)
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// LoadJSONID calls Load() to get content from the backend and afterwards calls
|
// LoadJSONID calls Load() to get content from the backend and afterwards calls
|
||||||
// json.Unmarshal on the item.
|
// json.Unmarshal on the item.
|
||||||
func (s Server) LoadJSONID(t backend.Type, storageID backend.ID, item interface{}) error {
|
func (s Server) LoadJSONID(t backend.Type, storageID backend.ID, item interface{}) error {
|
||||||
// load from backend
|
// read
|
||||||
buf, err := s.LoadID(t, storageID)
|
rd, err := s.GetReader(t, storageID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// inflate and unmarshal
|
// decrypt
|
||||||
err = json.Unmarshal(backend.Uncompress(buf), item)
|
decryptRd, err := s.key.DecryptFrom(rd)
|
||||||
return err
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// unzip
|
||||||
|
unzipRd, err := zlib.NewReader(decryptRd)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// decode
|
||||||
|
decoder := json.NewDecoder(unzipRd)
|
||||||
|
err = decoder.Decode(item)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Save encrypts data and stores it to the backend as type t.
|
// Save encrypts data and stores it to the backend as type t.
|
||||||
|
|
Loading…
Reference in a new issue