π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
Mapper for api.Collection to model.Album conversion
Neeraj Guptab6417d09b31c
diff --git a/pkg/collections.go b/pkg/collections.goindex b1149c1ec3..72e26eeb88 100644--- a/pkg/collections.go+++ b/pkg/collections.go@@ -1,13 +1,10 @@package pkgimport (- "cli-go/internal/api"- enteCrypto "cli-go/internal/crypto"+ debuglog "cli-go/pkg/log""cli-go/pkg/model"- "cli-go/utils/encoding""context""fmt"- "log")func (c *ClICtrl) syncRemoteCollections(ctx context.Context, info model.Account) error {@@ -15,21 +12,13 @@ func (c *ClICtrl) syncRemoteCollections(ctx context.Context, info model.Account)if err != nil {return fmt.Errorf("failed to get collections: %s", err)}+for _, collection := range collections {- collectionKey, err := c.KeyHolder.GetCollectionKey(ctx, collection)- if err != nil {- return err- }- name, nameErr := enteCrypto.SecretBoxOpenBase64(collection.EncryptedName, collection.NameDecryptionNonce, collectionKey)- if nameErr != nil {- log.Fatalf("failed to decrypt collection name: %v", nameErr)- }- if collection.Owner.ID != info.UserID {- fmt.Printf("Shared Album %s\n", string(name))- continue- } else {- fmt.Printf("Owned Name %s\n", string(name))+ album, err2 := c.mapCollectionToAlbum(ctx, collection)+ if err2 != nil {+ return err2}+ debuglog.PrintAlbum(album)}return nil}diff --git a/pkg/log/debug_print.go b/pkg/log/debug_print.goindex 762e504fe3..35ac29c38c 100644--- a/pkg/log/debug_print.go+++ b/pkg/log/debug_print.go@@ -1,4 +1,4 @@-package log+package debuglogimport ("cli-go/pkg/model"@@ -8,10 +8,13 @@ import (// This file contains functions that are used to print debug information to the console.func PrintAlbum(a *model.Album) {+ fmt.Printf("=======\n")fmt.Printf("ID: %d\n", a.ID)fmt.Printf("OwnerID: %d\n", a.OwnerID)- fmt.Printf("AlbumName: %s\n", a.AlbumName)- fmt.Printf("AlbumKey: %s\n", a.AlbumKey.CipherText)+ if a.IsShared {+ fmt.Printf("Shared album")+ }+ fmt.Printf(" Name: %s\n", a.AlbumName)if a.PrivateMeta != nil {fmt.Printf("PrivateMeta: %s\n", *a.PrivateMeta)}@@ -21,5 +24,6 @@ func PrintAlbum(a *model.Album) {if a.SharedMeta != nil {fmt.Printf("SharedMeta: %s\n", *a.SharedMeta)}- fmt.Printf("LastUpdatedAt: %d\n", a.LastUpdatedAt)+ fmt.Printf("LastUpdatedAt: %d", a.LastUpdatedAt)+ fmt.Printf("\n=======")}diff --git a/pkg/mappers.go b/pkg/mappers.gonew file mode 100644index 0000000000..48846403fa--- /dev/null+++ b/pkg/mappers.go@@ -0,0 +1,55 @@+package pkg++import (+ "cli-go/internal/api"+ enteCrypto "cli-go/internal/crypto"+ "cli-go/pkg/model"+ "context"+ "log"+)++func (c *ClICtrl) mapCollectionToAlbum(ctx context.Context, collection api.Collection) (*model.Album, error) {+ var album model.Album+ userID := ctx.Value("user_id").(int64)+ collectionKey, err := c.KeyHolder.GetCollectionKey(ctx, collection)+ album.OwnerID = collection.Owner.ID+ album.ID = collection.ID+ album.IsShared = collection.Owner.ID != userID+ album.AlbumKey = *model.MakeEncString(collectionKey, c.CliKey)+ album.LastUpdatedAt = collection.UpdationTime+ album.IsDeleted = collection.IsDeleted+ if err != nil {+ return nil, err+ }+ name, nameErr := enteCrypto.SecretBoxOpenBase64(collection.EncryptedName, collection.NameDecryptionNonce, collectionKey)+ if nameErr != nil {+ log.Fatalf("failed to decrypt collection name: %v", nameErr)+ }+ album.AlbumName = string(name)++ if collection.MagicMetadata != nil {+ _, encodedJsonBytes, err := enteCrypto.DecryptChaChaBase64(collection.MagicMetadata.Data, collectionKey, collection.MagicMetadata.Header)+ if err != nil {+ return nil, err+ }+ var val = string(encodedJsonBytes)+ album.PrivateMeta = &val+ }+ if collection.PublicMagicMetadata != nil {+ _, encodedJsonBytes, err := enteCrypto.DecryptChaChaBase64(collection.PublicMagicMetadata.Data, collectionKey, collection.PublicMagicMetadata.Header)+ if err != nil {+ return nil, err+ }+ var val = string(encodedJsonBytes)+ album.PublicMeta = &val+ }+ if album.IsShared && collection.SharedMagicMetadata != nil {+ _, encodedJsonBytes, err := enteCrypto.DecryptChaChaBase64(collection.SharedMagicMetadata.Data, collectionKey, collection.SharedMagicMetadata.Header)+ if err != nil {+ return nil, err+ }+ var val = string(encodedJsonBytes)+ album.SharedMeta = &val+ }+ return &album, nil+}diff --git a/pkg/mappers/album_mapper.go b/pkg/mappers/album_mapper.godeleted file mode 100644index bff8365e00..0000000000--- a/pkg/mappers/album_mapper.go+++ /dev/null@@ -1,10 +0,0 @@-package mappers--import (- "cli-go/internal/api"- "cli-go/pkg/model"-)--func MapCollectionToAlbum(collection *api.Collection, collectionKey, cliKey []byte) (*model.Album, error) {- panic("not implemented")-}diff --git a/pkg/model/album.go b/pkg/model/album.goindex 0f7272d2b5..dc4b1742c8 100644--- a/pkg/model/album.go+++ b/pkg/model/album.go@@ -3,6 +3,8 @@ package modeltype Album struct {ID int64 `json:"id"`OwnerID int64 `json:"ownerID"`+ IsShared bool `json:"isShared"`+ IsDeleted bool `json:"isDeleted"`AlbumName string `json:"albumName"`AlbumKey EncString `json:"albumKey"`PublicMeta *string `json:"publicMeta"`