π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
feat(server): support lane type and ordered memory share files
AmanRajSinghMourya4b1ae6e1c586
diff --git a/server/ente/memory_share.go b/server/ente/memory_share.goindex e4106d6609..b48d287ce2 100644--- a/server/ente/memory_share.go+++ b/server/ente/memory_share.go@@ -33,6 +33,7 @@ type MemoryShareFile struct {MemoryShareID int64 `json:"-"`FileID int64 `json:"fileID"`FileOwnerID int64 `json:"-"`+ Position int64 `json:"position,omitempty"`EncryptedKey string `json:"encryptedKey"`KeyDecryptionNonce string `json:"keyDecryptionNonce"`CreatedAt int64 `json:"createdAt"`@@ -40,6 +41,7 @@ type MemoryShareFile struct {// CreateMemoryShareRequest is the request body for creating a memory sharetype CreateMemoryShareRequest struct {+ Type MemoryShareType `json:"type,omitempty"`MemoryHash string `json:"memoryHash,omitempty"`MetadataCipher string `json:"metadataCipher"`MetadataNonce string `json:"metadataNonce"`@@ -51,6 +53,7 @@ type CreateMemoryShareRequest struct {// MemoryShareFileItem represents a file in the create requesttype MemoryShareFileItem struct {FileID int64 `json:"fileID" binding:"required"`+ Position *int64 `json:"position,omitempty"`EncryptedKey string `json:"encryptedKey" binding:"required"`KeyDecryptionNonce string `json:"keyDecryptionNonce" binding:"required"`}diff --git a/server/migrations/117_create_memory_shares.up.sql b/server/migrations/117_create_memory_shares.up.sqlindex c8322f744a..62b0eed1ad 100644--- a/server/migrations/117_create_memory_shares.up.sql+++ b/server/migrations/117_create_memory_shares.up.sql@@ -20,8 +20,9 @@ CREATE TABLE IF NOT EXISTS memory_share_files (memory_share_id BIGINT NOT NULL REFERENCES memory_shares(id) ON DELETE CASCADE,file_id BIGINT NOT NULL,file_owner_id BIGINT NOT NULL,+ position BIGINT NOT NULL,file_enc_key TEXT NOT NULL,file_key_decryption_nonce TEXT NOT NULL,created_at BIGINT NOT NULL,- UNIQUE(memory_share_id, file_id)+ UNIQUE(memory_share_id, position));diff --git a/server/pkg/controller/memory_share/controller.go b/server/pkg/controller/memory_share/controller.goindex 85e68e312b..015bce6808 100644--- a/server/pkg/controller/memory_share/controller.go+++ b/server/pkg/controller/memory_share/controller.go@@ -40,6 +40,15 @@ func NewController(// Create creates a new memory sharefunc (c *Controller) Create(ctx *gin.Context, userID int64, req ente.CreateMemoryShareRequest) (*ente.CreateMemoryShareResponse, error) {+ shareType := req.Type+ if shareType == "" {+ shareType = ente.MemoryShareTypeShare+ }+ if shareType != ente.MemoryShareTypeShare &&+ shareType != ente.MemoryShareTypeLane {+ return nil, stacktrace.Propagate(ente.ErrBadRequest, "invalid memory share type")+ }+fileIDs := make([]int64, len(req.Files))for i, f := range req.Files {fileIDs[i] = f.FileID@@ -72,9 +81,17 @@ func (c *Controller) Create(ctx *gin.Context, userID int64, req ente.CreateMemorshareFiles := make([]ente.MemoryShareFile, len(req.Files))now := time.Microseconds()for i, f := range req.Files {+ position := int64(i)+ if f.Position != nil {+ if *f.Position < 0 {+ return nil, stacktrace.Propagate(ente.ErrBadRequest, "position cannot be negative")+ }+ position = *f.Position+ }shareFiles[i] = ente.MemoryShareFile{FileID: f.FileID,FileOwnerID: fileOwnerMap[f.FileID],+ Position: position,EncryptedKey: f.EncryptedKey,KeyDecryptionNonce: f.KeyDecryptionNonce,CreatedAt: now,@@ -88,7 +105,7 @@ func (c *Controller) Create(ctx *gin.Context, userID int64, req ente.CreateMemorshare = ente.MemoryShare{UserID: userID,- Type: ente.MemoryShareTypeShare,+ Type: shareType,MemoryHash: req.MemoryHash,MetadataCipher: req.MetadataCipher,MetadataNonce: req.MetadataNonce,diff --git a/server/pkg/repo/memory_share.go b/server/pkg/repo/memory_share.goindex 93b1c6e2d5..22a446953f 100644--- a/server/pkg/repo/memory_share.go+++ b/server/pkg/repo/memory_share.go@@ -88,9 +88,9 @@ func (r *MemoryShareRepository) CreateWithFiles(ctx context.Context, share ente.for _, file := range files {_, err = tx.ExecContext(ctx, `INSERT INTO memory_share_files- (memory_share_id, file_id, file_owner_id, file_enc_key, file_key_decryption_nonce, created_at)- VALUES ($1, $2, $3, $4, $5, $6)`,- share.ID, file.FileID, file.FileOwnerID, file.EncryptedKey, file.KeyDecryptionNonce, now)+ (memory_share_id, file_id, file_owner_id, position, file_enc_key, file_key_decryption_nonce, created_at)+ VALUES ($1, $2, $3, $4, $5, $6, $7)`,+ share.ID, file.FileID, file.FileOwnerID, file.Position, file.EncryptedKey, file.KeyDecryptionNonce, now)if err != nil {return share, stacktrace.Propagate(err, "failed to insert share file")}@@ -118,9 +118,9 @@ func (r *MemoryShareRepository) AddFiles(ctx context.Context, shareID int64, filfor _, file := range files {_, err = tx.ExecContext(ctx, `INSERT INTO memory_share_files- (memory_share_id, file_id, file_owner_id, file_enc_key, file_key_decryption_nonce, created_at)- VALUES ($1, $2, $3, $4, $5, $6)`,- shareID, file.FileID, file.FileOwnerID, file.EncryptedKey, file.KeyDecryptionNonce, now)+ (memory_share_id, file_id, file_owner_id, position, file_enc_key, file_key_decryption_nonce, created_at)+ VALUES ($1, $2, $3, $4, $5, $6, $7)`,+ shareID, file.FileID, file.FileOwnerID, file.Position, file.EncryptedKey, file.KeyDecryptionNonce, now)if err != nil {return stacktrace.Propagate(err, "failed to insert share file")}@@ -175,8 +175,10 @@ func (r *MemoryShareRepository) GetByAccessToken(ctx context.Context, token stri// GetFiles returns all file rows for a memory share.func (r *MemoryShareRepository) GetFiles(ctx context.Context, shareID int64) ([]ente.MemoryShareFile, error) {rows, err := r.DB.QueryContext(ctx, `- SELECT id, memory_share_id, file_id, file_owner_id, file_enc_key, file_key_decryption_nonce, created_at- FROM memory_share_files WHERE memory_share_id = $1`, shareID)+ SELECT id, memory_share_id, file_id, file_owner_id, position, file_enc_key, file_key_decryption_nonce, created_at+ FROM memory_share_files+ WHERE memory_share_id = $1+ ORDER BY position ASC, id ASC`, shareID)if err != nil {return nil, stacktrace.Propagate(err, "failed to query share files")}@@ -186,7 +188,7 @@ func (r *MemoryShareRepository) GetFiles(ctx context.Context, shareID int64) ([]for rows.Next() {var file ente.MemoryShareFileif err = rows.Scan(&file.ID, &file.MemoryShareID, &file.FileID, &file.FileOwnerID,- &file.EncryptedKey, &file.KeyDecryptionNonce, &file.CreatedAt); err != nil {+ &file.Position, &file.EncryptedKey, &file.KeyDecryptionNonce, &file.CreatedAt); err != nil {return nil, stacktrace.Propagate(err, "failed to scan share file")}files = append(files, file)@@ -197,7 +199,10 @@ func (r *MemoryShareRepository) GetFiles(ctx context.Context, shareID int64) ([]// GetFileIDs returns just the file IDs for a memory share.func (r *MemoryShareRepository) GetFileIDs(ctx context.Context, shareID int64) ([]int64, error) {rows, err := r.DB.QueryContext(ctx, `- SELECT file_id FROM memory_share_files WHERE memory_share_id = $1`, shareID)+ SELECT file_id+ FROM memory_share_files+ WHERE memory_share_id = $1+ ORDER BY position ASC, id ASC`, shareID)if err != nil {return nil, stacktrace.Propagate(err, "failed to query file IDs")}@@ -283,10 +288,13 @@ func (r *MemoryShareRepository) GetFileOwnerMap(ctx context.Context, shareID intfunc (r *MemoryShareRepository) GetFileByID(ctx context.Context, shareID int64, fileID int64) (*ente.MemoryShareFile, error) {file := &ente.MemoryShareFile{}err := r.DB.QueryRowContext(ctx, `- SELECT id, memory_share_id, file_id, file_owner_id, file_enc_key, file_key_decryption_nonce, created_at- FROM memory_share_files WHERE memory_share_id = $1 AND file_id = $2`, shareID, fileID).Scan(+ SELECT id, memory_share_id, file_id, file_owner_id, position, file_enc_key, file_key_decryption_nonce, created_at+ FROM memory_share_files+ WHERE memory_share_id = $1 AND file_id = $2+ ORDER BY position ASC, id ASC+ LIMIT 1`, shareID, fileID).Scan(&file.ID, &file.MemoryShareID, &file.FileID, &file.FileOwnerID,- &file.EncryptedKey, &file.KeyDecryptionNonce, &file.CreatedAt)+ &file.Position, &file.EncryptedKey, &file.KeyDecryptionNonce, &file.CreatedAt)if err != nil {if err == sql.ErrNoRows {return nil, stacktrace.Propagate(ente.ErrNotFound, "file not found in share")@@ -314,8 +322,10 @@ func (r *MemoryShareRepository) FileExistsInShare(ctx context.Context, shareID i// GetFilesWithKeys returns file entries and re-encrypted keys for the requested file IDs.func (r *MemoryShareRepository) GetFilesWithKeys(ctx context.Context, shareID int64, fileIDs []int64) ([]ente.MemoryShareFile, error) {rows, err := r.DB.QueryContext(ctx, `- SELECT id, memory_share_id, file_id, file_owner_id, file_enc_key, file_key_decryption_nonce, created_at- FROM memory_share_files WHERE memory_share_id = $1 AND file_id = ANY($2)`, shareID, pq.Array(fileIDs))+ SELECT id, memory_share_id, file_id, file_owner_id, position, file_enc_key, file_key_decryption_nonce, created_at+ FROM memory_share_files+ WHERE memory_share_id = $1 AND file_id = ANY($2)+ ORDER BY position ASC, id ASC`, shareID, pq.Array(fileIDs))if err != nil {return nil, stacktrace.Propagate(err, "failed to query files with keys")}@@ -325,7 +335,7 @@ func (r *MemoryShareRepository) GetFilesWithKeys(ctx context.Context, shareID infor rows.Next() {var file ente.MemoryShareFileif err = rows.Scan(&file.ID, &file.MemoryShareID, &file.FileID, &file.FileOwnerID,- &file.EncryptedKey, &file.KeyDecryptionNonce, &file.CreatedAt); err != nil {+ &file.Position, &file.EncryptedKey, &file.KeyDecryptionNonce, &file.CreatedAt); err != nil {return nil, stacktrace.Propagate(err, "failed to scan file with keys")}files = append(files, file)