From a51a965fc8efd6b63ad0fd6b0e4b7bf41ad631d2 Mon Sep 17 00:00:00 2001 From: Neeraj Gupta Date: Sat, 30 Mar 2024 14:14:03 +0530 Subject: [PATCH] [cli] Fix handling of delete file with stale colleciton entry (#1259) ## Description ## Tests Tested locally --- cli/internal/api/file_type.go | 4 ++++ cli/pkg/mapper/photo.go | 2 +- cli/pkg/remote_sync.go | 6 +++--- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/cli/internal/api/file_type.go b/cli/internal/api/file_type.go index f241f8fe5..93a23b7b0 100644 --- a/cli/internal/api/file_type.go +++ b/cli/internal/api/file_type.go @@ -18,6 +18,10 @@ type File struct { Info *FileInfo `json:"info,omitempty"` } +func (f File) IsRemovedFromAlbum() bool { + return f.IsDeleted || f.File.EncryptedData == "-" +} + // FileInfo has information about storage used by the file & it's metadata(future) type FileInfo struct { FileSize int64 `json:"fileSize,omitempty"` diff --git a/cli/pkg/mapper/photo.go b/cli/pkg/mapper/photo.go index a7cf77bfc..ac16f44f7 100644 --- a/cli/pkg/mapper/photo.go +++ b/cli/pkg/mapper/photo.go @@ -73,7 +73,7 @@ func MapCollectionToAlbum(ctx context.Context, collection api.Collection, holder } func MapApiFileToPhotoFile(ctx context.Context, album model.RemoteAlbum, file api.File, holder *secrets.KeyHolder) (*model.RemoteFile, error) { - if file.IsDeleted { + if file.IsRemovedFromAlbum() { return nil, errors.New("file is deleted") } albumKey := album.AlbumKey.MustDecrypt(holder.DeviceKey) diff --git a/cli/pkg/remote_sync.go b/cli/pkg/remote_sync.go index a37a4a58c..5ca149d71 100644 --- a/cli/pkg/remote_sync.go +++ b/cli/pkg/remote_sync.go @@ -87,16 +87,16 @@ func (c *ClICtrl) fetchRemoteFiles(ctx context.Context) error { if file.UpdationTime > maxUpdated { maxUpdated = file.UpdationTime } - if isFirstSync && file.IsDeleted { + if isFirstSync && file.IsRemovedFromAlbum() { // on first sync, no need to sync delete markers continue } - albumEntry := model.AlbumFileEntry{AlbumID: album.ID, FileID: file.ID, IsDeleted: file.IsDeleted, SyncedLocally: false} + albumEntry := model.AlbumFileEntry{AlbumID: album.ID, FileID: file.ID, IsDeleted: file.IsRemovedFromAlbum(), SyncedLocally: false} putErr := c.UpsertAlbumEntry(ctx, &albumEntry) if putErr != nil { return putErr } - if file.IsDeleted { + if file.IsRemovedFromAlbum() { continue } photoFile, err := mapper.MapApiFileToPhotoFile(ctx, album, file, c.KeyHolder)