cwtch/storage/profile_store.go

40 lines
843 B
Go
Raw Normal View History

2018-10-05 03:18:34 +00:00
package storage
2018-10-06 03:50:55 +00:00
import (
"cwtch.im/cwtch/model"
"encoding/json"
2018-10-06 03:50:55 +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 {
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
}