Przeglądaj źródła

Add logic to decrypt file metadata and persist

Neeraj Gupta 1 rok temu
rodzic
commit
4294747d36
3 zmienionych plików z 74 dodań i 1 usunięć
  1. 5 1
      pkg/collections.go
  2. 51 0
      pkg/mappers.go
  3. 18 0
      pkg/model/photo_file.go

+ 5 - 1
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

+ 51 - 0
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
+
+}

+ 18 - 0
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"`
+}