ente/pkg/remote_sync.go

51 lines
1.5 KiB
Go
Raw Normal View History

2023-09-22 04:44:22 +00:00
package pkg
import (
"cli-go/pkg/model"
2023-09-22 08:20:50 +00:00
"context"
"encoding/base64"
2023-09-22 04:44:22 +00:00
"fmt"
bolt "go.etcd.io/bbolt"
"log"
)
2023-09-22 08:20:50 +00:00
func (c *ClICtrl) SyncAccount(account model.Account) error {
log.SetPrefix(fmt.Sprintf("[%s] ", account.Email))
2023-09-22 16:15:01 +00:00
secretInfo, err := c.KeyHolder.LoadSecrets(account, c.CliKey)
if err != nil {
return err
}
2023-09-23 04:04:21 +00:00
ctx := c.buildRequestContext(context.Background(), account)
2023-09-22 16:15:01 +00:00
err = createDataBuckets(c.DB, account)
2023-09-22 08:20:50 +00:00
if err != nil {
return err
}
2023-09-22 16:15:01 +00:00
c.Client.AddToken(account.AccountKey(), base64.URLEncoding.EncodeToString(secretInfo.Token))
2023-09-22 08:20:50 +00:00
return c.syncRemoteCollections(ctx, account)
}
2023-09-22 13:47:24 +00:00
func (c *ClICtrl) buildRequestContext(ctx context.Context, account model.Account) context.Context {
2023-09-22 08:20:50 +00:00
ctx = context.WithValue(ctx, "app", string(account.App))
ctx = context.WithValue(ctx, "account_id", account.AccountKey())
2023-09-23 04:04:21 +00:00
ctx = context.WithValue(ctx, "user_id", account.UserID)
2023-09-22 08:20:50 +00:00
return ctx
2023-09-22 04:44:22 +00:00
}
var dataCategories = []string{"remote-collections", "local-collections", "remote-files", "local-files", "remote-collection-removed", "remote-files-removed"}
2023-09-22 08:20:50 +00:00
func createDataBuckets(db *bolt.DB, account model.Account) error {
2023-09-22 04:44:22 +00:00
return db.Update(func(tx *bolt.Tx) error {
dataBucket, err := tx.CreateBucketIfNotExists([]byte(account.DataBucket()))
if err != nil {
return fmt.Errorf("create bucket: %s", err)
}
for _, category := range dataCategories {
2023-09-22 08:20:50 +00:00
_, err := dataBucket.CreateBucketIfNotExists([]byte(fmt.Sprintf(category)))
2023-09-22 04:44:22 +00:00
if err != nil {
return err
}
}
return nil
})
}