2023-09-23 10:45:10 +00:00
|
|
|
package pkg
|
|
|
|
|
|
|
|
import (
|
|
|
|
"cli-go/pkg/model"
|
2023-09-25 02:40:15 +00:00
|
|
|
"cli-go/utils/encoding"
|
2023-09-23 10:45:10 +00:00
|
|
|
"context"
|
2023-09-25 06:23:12 +00:00
|
|
|
"encoding/json"
|
2023-09-23 10:45:10 +00:00
|
|
|
"fmt"
|
2023-09-25 06:23:12 +00:00
|
|
|
"log"
|
2023-09-24 20:21:11 +00:00
|
|
|
"strconv"
|
2023-09-25 08:33:58 +00:00
|
|
|
"time"
|
2023-09-23 10:45:10 +00:00
|
|
|
)
|
|
|
|
|
2023-09-25 06:44:41 +00:00
|
|
|
func (c *ClICtrl) fetchRemoteCollections(ctx context.Context) error {
|
2023-09-25 02:47:29 +00:00
|
|
|
lastSyncTime, err2 := c.GetInt64ConfigValue(ctx, model.CollectionsSyncKey)
|
|
|
|
if err2 != nil {
|
|
|
|
return err2
|
2023-09-24 20:21:11 +00:00
|
|
|
}
|
|
|
|
collections, err := c.Client.GetCollections(ctx, lastSyncTime)
|
2023-09-23 10:45:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to get collections: %s", err)
|
|
|
|
}
|
2023-09-24 20:21:11 +00:00
|
|
|
maxUpdated := lastSyncTime
|
2023-09-23 10:45:10 +00:00
|
|
|
for _, collection := range collections {
|
2023-09-24 20:22:03 +00:00
|
|
|
if lastSyncTime == 0 && collection.IsDeleted {
|
|
|
|
continue
|
|
|
|
}
|
2023-09-25 02:47:29 +00:00
|
|
|
album, mapErr := c.mapCollectionToAlbum(ctx, collection)
|
|
|
|
if mapErr != nil {
|
|
|
|
return mapErr
|
2023-09-23 10:45:10 +00:00
|
|
|
}
|
2023-09-24 20:21:11 +00:00
|
|
|
if album.LastUpdatedAt > maxUpdated {
|
|
|
|
maxUpdated = album.LastUpdatedAt
|
|
|
|
}
|
2023-09-25 02:40:15 +00:00
|
|
|
albumJson := encoding.MustMarshalJSON(album)
|
2023-09-25 02:47:29 +00:00
|
|
|
putErr := c.PutValue(ctx, model.RemoteAlbums, []byte(strconv.FormatInt(album.ID, 10)), albumJson)
|
|
|
|
if putErr != nil {
|
|
|
|
return putErr
|
2023-09-25 02:40:15 +00:00
|
|
|
}
|
2023-09-23 10:45:10 +00:00
|
|
|
}
|
2023-09-24 20:21:11 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
2023-09-23 10:45:10 +00:00
|
|
|
return nil
|
|
|
|
}
|
2023-09-25 06:23:12 +00:00
|
|
|
|
2023-09-25 06:44:41 +00:00
|
|
|
func (c *ClICtrl) fetchRemoteFiles(ctx context.Context) error {
|
2023-09-25 06:23:12 +00:00
|
|
|
albums, err := c.getRemoteAlbums(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, album := range albums {
|
|
|
|
if album.IsDeleted {
|
|
|
|
log.Printf("Skipping album %s as it is deleted", album.AlbumName)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
lastSyncTime, lastSyncTimeErr := c.GetInt64ConfigValue(ctx, fmt.Sprintf(model.CollectionsFileSyncKeyFmt, album.ID))
|
|
|
|
if lastSyncTimeErr != nil {
|
|
|
|
return lastSyncTimeErr
|
|
|
|
}
|
|
|
|
isFirstSync := lastSyncTime == 0
|
|
|
|
for {
|
2023-09-25 08:33:58 +00:00
|
|
|
|
2023-09-25 06:23:12 +00:00
|
|
|
if lastSyncTime == album.LastUpdatedAt {
|
|
|
|
break
|
|
|
|
}
|
2023-09-25 08:33:58 +00:00
|
|
|
if !isFirstSync {
|
|
|
|
t := time.UnixMicro(lastSyncTime)
|
|
|
|
log.Printf("Fetching files for album %s from %v\n", album.AlbumName, t)
|
|
|
|
}
|
2023-09-25 06:23:12 +00:00
|
|
|
files, hasMore, err := c.Client.GetFiles(ctx, album.ID, lastSyncTime)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
maxUpdated := lastSyncTime
|
|
|
|
for _, file := range files {
|
|
|
|
if file.UpdationTime > maxUpdated {
|
|
|
|
maxUpdated = file.UpdationTime
|
|
|
|
}
|
|
|
|
if isFirstSync && file.IsDeleted {
|
|
|
|
// on first sync, no need to sync delete markers
|
|
|
|
continue
|
|
|
|
}
|
2023-09-25 08:00:07 +00:00
|
|
|
photoFile, err := c.mapApiFileToPhotoFile(ctx, album, file)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
fileJson := encoding.MustMarshalJSON(photoFile)
|
2023-09-25 06:23:12 +00:00
|
|
|
putErr := c.PutValue(ctx, model.RemoteFiles, []byte(strconv.FormatInt(file.ID, 10)), fileJson)
|
|
|
|
if putErr != nil {
|
|
|
|
return putErr
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !hasMore {
|
|
|
|
maxUpdated = album.LastUpdatedAt
|
|
|
|
}
|
|
|
|
if maxUpdated > lastSyncTime || !hasMore {
|
|
|
|
err = c.PutConfigValue(ctx, fmt.Sprintf(model.CollectionsFileSyncKeyFmt, album.ID), []byte(strconv.FormatInt(maxUpdated, 10)))
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to update last sync time: %s", err)
|
|
|
|
} else {
|
|
|
|
lastSyncTime = maxUpdated
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2023-09-25 13:40:36 +00:00
|
|
|
|
2023-09-25 13:42:56 +00:00
|
|
|
func (c *ClICtrl) getRemoteAlbums(ctx context.Context) ([]model.RemoteAlbum, error) {
|
|
|
|
albums := make([]model.RemoteAlbum, 0)
|
2023-09-25 06:23:12 +00:00
|
|
|
albumBytes, err := c.GetAllValues(ctx, model.RemoteAlbums)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for _, albumJson := range albumBytes {
|
2023-09-25 13:42:56 +00:00
|
|
|
album := model.RemoteAlbum{}
|
2023-09-25 06:23:12 +00:00
|
|
|
err = json.Unmarshal(albumJson, &album)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
albums = append(albums, album)
|
|
|
|
}
|
|
|
|
return albums, nil
|
|
|
|
}
|