From 4294747d36a9b802eecc27eb24d56b5ce7e5de03 Mon Sep 17 00:00:00 2001 From: Neeraj Gupta <254676+ua741@users.noreply.github.com> Date: Mon, 25 Sep 2023 13:30:07 +0530 Subject: [PATCH] Add logic to decrypt file metadata and persist --- pkg/collections.go | 6 ++++- pkg/mappers.go | 51 +++++++++++++++++++++++++++++++++++++++++ pkg/model/photo_file.go | 18 +++++++++++++++ 3 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 pkg/model/photo_file.go diff --git a/pkg/collections.go b/pkg/collections.go index 11395bc5a..4272ed910 100644 --- a/pkg/collections.go +++ b/pkg/collections.go @@ -80,7 +80,11 @@ func (c *ClICtrl) fetchRemoteFiles(ctx context.Context) error { // on first sync, no need to sync delete markers continue } - fileJson := encoding.MustMarshalJSON(file) + photoFile, err := c.mapApiFileToPhotoFile(ctx, album, file) + if err != nil { + return err + } + fileJson := encoding.MustMarshalJSON(photoFile) putErr := c.PutValue(ctx, model.RemoteFiles, []byte(strconv.FormatInt(file.ID, 10)), fileJson) if putErr != nil { return putErr diff --git a/pkg/mappers.go b/pkg/mappers.go index 273c557f7..cd9b223b4 100644 --- a/pkg/mappers.go +++ b/pkg/mappers.go @@ -4,7 +4,10 @@ import ( "cli-go/internal/api" enteCrypto "cli-go/internal/crypto" "cli-go/pkg/model" + "cli-go/utils/encoding" "context" + "encoding/json" + "errors" "log" ) @@ -59,3 +62,51 @@ func (c *ClICtrl) mapCollectionToAlbum(ctx context.Context, collection api.Colle } return &album, nil } + +func (c *ClICtrl) mapApiFileToPhotoFile(ctx context.Context, album model.Album, file api.File) (*model.PhotoFile, error) { + if file.IsDeleted { + return nil, errors.New("file is deleted") + } + albumKey := album.AlbumKey.MustDecrypt(c.CliKey) + fileKey, err := enteCrypto.SecretBoxOpen( + encoding.DecodeBase64(file.EncryptedKey), + encoding.DecodeBase64(file.KeyDecryptionNonce), + albumKey) + if err != nil { + return nil, err + } + var photoFile model.PhotoFile + photoFile.ID = file.ID + photoFile.Key = *model.MakeEncString(fileKey, c.CliKey) + photoFile.FileNonce = file.File.DecryptionHeader + photoFile.ThumbnailNonce = file.Thumbnail.DecryptionHeader + photoFile.OwnerID = file.OwnerID + if file.Info != nil { + photoFile.PhotoInfo = model.PhotoInfo{ + FileSize: file.Info.FileSize, + ThumbnailSize: file.Info.ThumbnailSize, + } + } + if file.Metadata.DecryptionHeader != "" { + _, encodedJsonBytes, err := enteCrypto.DecryptChaChaBase64(file.Metadata.EncryptedData, fileKey, file.Metadata.DecryptionHeader) + if err != nil { + return nil, err + } + err = json.Unmarshal(encodedJsonBytes, &photoFile.PrivateMetadata) + if err != nil { + return nil, err + } + } + if file.MagicMetadata != nil { + _, encodedJsonBytes, err := enteCrypto.DecryptChaChaBase64(file.MagicMetadata.Data, fileKey, file.MagicMetadata.Header) + if err != nil { + return nil, err + } + err = json.Unmarshal(encodedJsonBytes, &photoFile.PublicMetadata) + if err != nil { + return nil, err + } + } + return &photoFile, nil + +} diff --git a/pkg/model/photo_file.go b/pkg/model/photo_file.go new file mode 100644 index 000000000..467be6a2a --- /dev/null +++ b/pkg/model/photo_file.go @@ -0,0 +1,18 @@ +package model + +type PhotoFile struct { + ID int64 `json:"id"` + OwnerID int64 `json:"ownerID"` + Key EncString `json:"key"` + LastUpdateTime int64 `json:"lastUpdateTime"` + FileNonce string `json:"fileNonce"` + ThumbnailNonce string `json:"thumbnailNonce"` + PrivateMetadata map[string]interface{} `json:"privateMetadata"` + PublicMetadata map[string]interface{} `json:"publicMetadata"` + PhotoInfo PhotoInfo `` +} + +type PhotoInfo struct { + FileSize int64 `json:"fileSize,omitempty"` + ThumbnailSize int64 `json:"thumbSize,omitempty"` +}