2018-10-05 03:18:34 +00:00
|
|
|
package storage
|
2018-10-06 03:50:55 +00:00
|
|
|
|
|
|
|
import (
|
2019-01-14 20:09:25 +00:00
|
|
|
"cwtch.im/cwtch/model"
|
|
|
|
"encoding/json"
|
2018-10-06 03:50:55 +00:00
|
|
|
)
|
|
|
|
|
2019-01-14 20:09:25 +00:00
|
|
|
type profileStore struct {
|
|
|
|
fs FileStore
|
|
|
|
}
|
|
|
|
|
2018-10-06 03:50:55 +00:00
|
|
|
// ProfileStore is an interface to managing the storage of Cwtch Profiles
|
|
|
|
type ProfileStore interface {
|
2019-01-14 20:09:25 +00:00
|
|
|
Save(profile *model.Profile) error
|
|
|
|
Load() (*model.Profile, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewProfileStore(filename string, password string) ProfileStore {
|
|
|
|
return &profileStore{NewFileStore(filename, password)}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ps *profileStore) Save(profile *model.Profile) error {
|
|
|
|
bytes, _ := json.Marshal(profile)
|
|
|
|
return ps.fs.Save(bytes)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load instantiates a cwtchPeer from the file store
|
|
|
|
func (ps *profileStore) Load() (*model.Profile, error) {
|
|
|
|
decrypted, err := ps.fs.Load()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
cp := new(model.Profile)
|
|
|
|
err = json.Unmarshal(decrypted, &cp)
|
|
|
|
if err == nil {
|
|
|
|
return cp, nil
|
|
|
|
}
|
|
|
|
return nil, err
|
2018-10-06 03:50:55 +00:00
|
|
|
}
|