ente/cli/pkg/sync.go

119 lines
3.2 KiB
Go
Raw Normal View History

2023-09-27 05:32:36 +00:00
package pkg
import (
"context"
"encoding/base64"
"fmt"
2023-10-21 09:26:13 +00:00
"github.com/ente-io/cli/internal"
"github.com/ente-io/cli/internal/api"
"github.com/ente-io/cli/pkg/model"
bolt "go.etcd.io/bbolt"
"log"
"time"
2023-09-27 05:32:36 +00:00
)
func (c *ClICtrl) Export() error {
2023-09-27 05:32:36 +00:00
accounts, err := c.GetAccounts(context.Background())
if err != nil {
return err
}
if len(accounts) == 0 {
fmt.Printf("No accounts to sync\n Add account using `account add` cmd\n")
2023-09-27 05:32:36 +00:00
return nil
}
for _, account := range accounts {
log.SetPrefix(fmt.Sprintf("[%s-%s] ", account.App, account.Email))
2023-10-16 17:49:35 +00:00
if account.ExportDir == "" {
log.Printf("Skip account %s: no export directory configured", account.Email)
continue
}
2023-10-16 18:18:00 +00:00
_, err = internal.ValidateDirForWrite(account.ExportDir)
2023-10-16 17:49:35 +00:00
if err != nil {
log.Printf("Skip export, error: %v while validing exportDir %s\n", err, account.ExportDir)
continue
}
2023-10-18 07:11:20 +00:00
if account.App == api.AppAuth {
log.Printf("Skip account %s: auth export is not supported", account.Email)
continue
}
2023-09-27 05:32:36 +00:00
log.Println("start sync")
retryCount := 0
for {
err = c.SyncAccount(account)
if err != nil {
if model.ShouldRetrySync(err) && retryCount < 20 {
retryCount = retryCount + 1
timeInSecond := time.Duration(retryCount*10) * time.Second
log.Printf("Connection err, waiting for %s before trying again", timeInSecond.String())
time.Sleep(timeInSecond)
continue
}
fmt.Printf("Error syncing account %s: %s\n", account.Email, err)
return err
} else {
log.Println("sync done")
break
}
2023-09-27 05:32:36 +00:00
}
}
return nil
}
func (c *ClICtrl) SyncAccount(account model.Account) error {
2023-09-27 08:39:44 +00:00
secretInfo, err := c.KeyHolder.LoadSecrets(account)
2023-09-27 05:32:36 +00:00
if err != nil {
return err
}
ctx := c.buildRequestContext(context.Background(), account)
err = createDataBuckets(c.DB, account)
if err != nil {
return err
}
c.Client.AddToken(account.AccountKey(), base64.URLEncoding.EncodeToString(secretInfo.Token))
err = c.fetchRemoteCollections(ctx)
if err != nil {
log.Printf("Error fetching collections: %s", err)
return err
2023-09-27 05:32:36 +00:00
}
err = c.fetchRemoteFiles(ctx)
if err != nil {
log.Printf("Error fetching files: %s", err)
return err
2023-09-27 05:32:36 +00:00
}
2023-10-16 17:49:35 +00:00
err = c.createLocalFolderForRemoteAlbums(ctx, account)
if err != nil {
log.Printf("Error creating local folders: %s", err)
return err
}
2023-10-16 17:49:35 +00:00
err = c.syncFiles(ctx, account)
if err != nil {
log.Printf("Error syncing files: %s", err)
return err
}
2023-09-27 05:32:36 +00:00
return nil
}
func (c *ClICtrl) buildRequestContext(ctx context.Context, account model.Account) context.Context {
ctx = context.WithValue(ctx, "app", string(account.App))
2023-10-04 03:01:24 +00:00
ctx = context.WithValue(ctx, "account_key", account.AccountKey())
2023-09-27 05:32:36 +00:00
ctx = context.WithValue(ctx, "user_id", account.UserID)
return ctx
}
func createDataBuckets(db *bolt.DB, account model.Account) error {
return db.Update(func(tx *bolt.Tx) error {
dataBucket, err := tx.CreateBucketIfNotExists([]byte(account.AccountKey()))
if err != nil {
return fmt.Errorf("create bucket: %s", err)
}
2023-10-04 06:22:34 +00:00
for _, subBucket := range []model.PhotosStore{model.KVConfig, model.RemoteAlbums, model.RemoteFiles, model.RemoteAlbumEntries} {
2023-09-27 05:32:36 +00:00
_, err := dataBucket.CreateBucketIfNotExists([]byte(subBucket))
if err != nil {
return err
}
}
return nil
})
}