π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
Persist and use collection last sync time
Neeraj Guptaa800ce745af5
diff --git a/pkg/bolt_db.go b/pkg/bolt_db.godeleted file mode 100644index 533ac71b1f..0000000000--- a/pkg/bolt_db.go+++ /dev/null@@ -1,16 +0,0 @@-package pkg--import (- "log"- "time"-- bolt "go.etcd.io/bbolt"-)--func GetDB(path string) (*bolt.DB, error) {- db, err := bolt.Open(path, 0600, &bolt.Options{Timeout: 1 * time.Second})- if err != nil {- log.Fatal(err)- }- return db, err-}diff --git a/pkg/collections.go b/pkg/collections.goindex 72e26eeb88..88dddf1d37 100644--- a/pkg/collections.go+++ b/pkg/collections.go@@ -5,20 +5,41 @@ import ("cli-go/pkg/model""context""fmt"+ "strconv")func (c *ClICtrl) syncRemoteCollections(ctx context.Context, info model.Account) error {- collections, err := c.Client.GetCollections(ctx, 0)+ valueBytes, err := c.GetConfigValue(ctx, model.CollectionsSyncKey)+ if err != nil {+ return fmt.Errorf("failed to get last sync time: %s", err)+ }+ var lastSyncTime int64+ if valueBytes != nil {+ lastSyncTime, err = strconv.ParseInt(string(valueBytes), 10, 64)+ if err != nil {+ return err+ }+ }+ collections, err := c.Client.GetCollections(ctx, lastSyncTime)if err != nil {return fmt.Errorf("failed to get collections: %s", err)}-+ maxUpdated := lastSyncTimefor _, collection := range collections {album, err2 := c.mapCollectionToAlbum(ctx, collection)if err2 != nil {return err2}+ if album.LastUpdatedAt > maxUpdated {+ maxUpdated = album.LastUpdatedAt+ }debuglog.PrintAlbum(album)}+ if maxUpdated > lastSyncTime {+ err = c.PutConfigValue(ctx, model.CollectionsSyncKey, []byte(strconv.FormatInt(maxUpdated, 10)))+ if err != nil {+ return fmt.Errorf("failed to update last sync time: %s", err)+ }+ }return nil}diff --git a/pkg/store.go b/pkg/store.gonew file mode 100644index 0000000000..c75bbde51c--- /dev/null+++ b/pkg/store.go@@ -0,0 +1,55 @@+package pkg++import (+ "cli-go/pkg/model"+ "context"+ "fmt"+ "log"+ "time"++ bolt "go.etcd.io/bbolt"+)++func GetDB(path string) (*bolt.DB, error) {+ db, err := bolt.Open(path, 0600, &bolt.Options{Timeout: 1 * time.Second})+ if err != nil {+ log.Fatal(err)+ }+ return db, err+}++func (c *ClICtrl) GetConfigValue(ctx context.Context, key string) ([]byte, error) {+ var value []byte+ err := c.DB.View(func(tx *bolt.Tx) error {+ kvBucket, err := getAccountStore(ctx, tx, model.KVConfig)+ if err != nil {+ return err+ }+ value = kvBucket.Get([]byte(key))+ return nil+ })+ return value, err+}++func (c *ClICtrl) PutConfigValue(ctx context.Context, key string, value []byte) error {+ return c.DB.Update(func(tx *bolt.Tx) error {+ kvBucket, err := getAccountStore(ctx, tx, model.KVConfig)+ if err != nil {+ return err+ }+ return kvBucket.Put([]byte(key), value)+ })+}++func getAccountStore(ctx context.Context, tx *bolt.Tx, storeType model.PhotosStore) (*bolt.Bucket, error) {+ accountId := ctx.Value("account_id").(string)+ accountBucket := tx.Bucket([]byte(accountId))+ if accountBucket == nil {+ return nil, fmt.Errorf("account bucket not found")+ }+ store := accountBucket.Bucket([]byte(storeType))+ if store == nil {+ return nil, fmt.Errorf("store %s not found", storeType)+ }+ return store, nil+}