sync.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. package pkg
  2. import (
  3. "context"
  4. "encoding/base64"
  5. "fmt"
  6. "github.com/ente-io/cli/internal"
  7. "github.com/ente-io/cli/internal/api"
  8. "github.com/ente-io/cli/pkg/model"
  9. bolt "go.etcd.io/bbolt"
  10. "log"
  11. "time"
  12. )
  13. func (c *ClICtrl) Export() error {
  14. accounts, err := c.GetAccounts(context.Background())
  15. if err != nil {
  16. return err
  17. }
  18. if len(accounts) == 0 {
  19. fmt.Printf("No accounts to sync\n Add account using `account add` cmd\n")
  20. return nil
  21. }
  22. for _, account := range accounts {
  23. log.SetPrefix(fmt.Sprintf("[%s-%s] ", account.App, account.Email))
  24. if account.ExportDir == "" {
  25. log.Printf("Skip account %s: no export directory configured", account.Email)
  26. continue
  27. }
  28. _, err = internal.ValidateDirForWrite(account.ExportDir)
  29. if err != nil {
  30. log.Printf("Skip export, error: %v while validing exportDir %s\n", err, account.ExportDir)
  31. continue
  32. }
  33. if account.App == api.AppAuth {
  34. log.Printf("Skip account %s: auth export is not supported", account.Email)
  35. continue
  36. }
  37. log.Println("start sync")
  38. retryCount := 0
  39. for {
  40. err = c.SyncAccount(account)
  41. if err != nil {
  42. if model.ShouldRetrySync(err) && retryCount < 20 {
  43. retryCount = retryCount + 1
  44. timeInSecond := time.Duration(retryCount*10) * time.Second
  45. log.Printf("Connection err, waiting for %s before trying again", timeInSecond.String())
  46. time.Sleep(timeInSecond)
  47. continue
  48. }
  49. fmt.Printf("Error syncing account %s: %s\n", account.Email, err)
  50. return err
  51. } else {
  52. log.Println("sync done")
  53. break
  54. }
  55. }
  56. }
  57. return nil
  58. }
  59. func (c *ClICtrl) SyncAccount(account model.Account) error {
  60. secretInfo, err := c.KeyHolder.LoadSecrets(account)
  61. if err != nil {
  62. return err
  63. }
  64. ctx := c.buildRequestContext(context.Background(), account)
  65. err = createDataBuckets(c.DB, account)
  66. if err != nil {
  67. return err
  68. }
  69. c.Client.AddToken(account.AccountKey(), base64.URLEncoding.EncodeToString(secretInfo.Token))
  70. err = c.fetchRemoteCollections(ctx)
  71. if err != nil {
  72. log.Printf("Error fetching collections: %s", err)
  73. return err
  74. }
  75. err = c.fetchRemoteFiles(ctx)
  76. if err != nil {
  77. log.Printf("Error fetching files: %s", err)
  78. return err
  79. }
  80. err = c.createLocalFolderForRemoteAlbums(ctx, account)
  81. if err != nil {
  82. log.Printf("Error creating local folders: %s", err)
  83. return err
  84. }
  85. err = c.syncFiles(ctx, account)
  86. if err != nil {
  87. log.Printf("Error syncing files: %s", err)
  88. return err
  89. }
  90. return nil
  91. }
  92. func (c *ClICtrl) buildRequestContext(ctx context.Context, account model.Account) context.Context {
  93. ctx = context.WithValue(ctx, "app", string(account.App))
  94. ctx = context.WithValue(ctx, "account_key", account.AccountKey())
  95. ctx = context.WithValue(ctx, "user_id", account.UserID)
  96. return ctx
  97. }
  98. func createDataBuckets(db *bolt.DB, account model.Account) error {
  99. return db.Update(func(tx *bolt.Tx) error {
  100. dataBucket, err := tx.CreateBucketIfNotExists([]byte(account.AccountKey()))
  101. if err != nil {
  102. return fmt.Errorf("create bucket: %s", err)
  103. }
  104. for _, subBucket := range []model.PhotosStore{model.KVConfig, model.RemoteAlbums, model.RemoteFiles, model.RemoteAlbumEntries} {
  105. _, err := dataBucket.CreateBucketIfNotExists([]byte(subBucket))
  106. if err != nil {
  107. return err
  108. }
  109. }
  110. return nil
  111. })
  112. }