Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

[server] verify staged attachments before commit

Neeraj Guptaaaa6d97ea944
diff --git a/server/pkg/controller/contact/controller.go b/server/pkg/controller/contact/controller.go
index 504d727d3e..7c0ca8c3b5 100644
--- a/server/pkg/controller/contact/controller.go
+++ b/server/pkg/controller/contact/controller.go
@@ -2,6 +2,7 @@ package contact
import (
"context"
+ "errors"
"io"
"os"
"time"
@@ -31,6 +32,7 @@ type Controller struct {
tempStorage string
replicateAttachmentFn func(context.Context, contactmodel.Attachment, string) error
deleteAttachmentFn func(contactmodel.Attachment, string) error
+ verifyAttachmentFn func(string, string, int64) error
}
func New(
@@ -61,6 +63,7 @@ func New(
}
ctrl.replicateAttachmentFn = ctrl.replicateAttachmentObject
ctrl.deleteAttachmentFn = ctrl.deleteAttachmentObject
+ ctrl.verifyAttachmentFn = ctrl.verifyAttachmentSize
return ctrl
}
@@ -144,26 +147,19 @@ func (c *Controller) Delete(ctx *gin.Context, contactID string) error {
return c.Repo.Delete(ctx, userID, contactID)
}
-func (c *Controller) GetProfilePictureUploadURL(ctx *gin.Context, contactID string, req contactmodel.ProfilePictureUploadURLRequest) (*contactmodel.ProfilePictureUploadURL, error) {
- if !contactmodel.IsValidContactID(contactID) {
- return nil, ente.NewBadRequestWithMessage("invalid contact id")
- }
- if err := req.Validate(); err != nil {
- return nil, stacktrace.Propagate(err, "invalid profile picture upload-url request")
- }
-
- userID := auth.GetUserID(ctx.Request.Header)
- entity, err := c.Repo.Get(ctx, userID, contactID)
+func (c *Controller) GetAttachmentUploadURL(ctx *gin.Context, attachmentTypeRaw string, req contactmodel.AttachmentUploadURLRequest) (*contactmodel.AttachmentUploadURL, error) {
+ attachmentType, policy, err := c.attachmentTypeAndPolicy(attachmentTypeRaw)
if err != nil {
- return nil, stacktrace.Propagate(err, "")
+ return nil, stacktrace.Propagate(err, "invalid attachment type")
}
- if entity.IsDeleted {
- return nil, stacktrace.Propagate(ente.NewBadRequestWithMessage("contact is already deleted"), "")
+ if err := req.Validate(policy); err != nil {
+ return nil, stacktrace.Propagate(err, "invalid attachment upload-url request")
}
+ userID := auth.GetUserID(ctx.Request.Header)
attachmentID := contactmodel.NewAttachmentID()
- objectKey := contactmodel.AttachmentObjectKey(userID, contactmodel.ProfilePicture, attachmentID)
- dc := c.S3Config.GetAttachmentBucketID(string(contactmodel.ProfilePicture))
+ objectKey := contactmodel.AttachmentObjectKey(userID, attachmentType, attachmentID)
+ dc := c.S3Config.GetAttachmentBucketID(string(attachmentType))
s3Client := c.S3Config.GetS3Client(dc)
putReq, _ := s3Client.PutObjectRequest(&s3.PutObjectInput{
@@ -177,46 +173,68 @@ func (c *Controller) GetProfilePictureUploadURL(ctx *gin.Context, contactID stri
return nil, stacktrace.Propagate(err, "failed to presign profile picture upload url")
}
if err := c.ObjectCleanupController.AddTempObjectKey(objectKey, dc); err != nil {
- return nil, stacktrace.Propagate(err, "failed to stage temp object for profile picture upload")
+ return nil, stacktrace.Propagate(err, "failed to stage temp object for attachment upload")
}
- return &contactmodel.ProfilePictureUploadURL{
+ return &contactmodel.AttachmentUploadURL{
AttachmentID: attachmentID,
URL: url,
}, nil
}
-func (c *Controller) AttachProfilePicture(ctx *gin.Context, contactID string, req contactmodel.CommitProfilePictureRequest) (*contactmodel.Entity, error) {
+func (c *Controller) GetProfilePictureUploadURL(ctx *gin.Context, req contactmodel.AttachmentUploadURLRequest) (*contactmodel.AttachmentUploadURL, error) {
+ return c.GetAttachmentUploadURL(ctx, string(contactmodel.ProfilePicture), req)
+}
+
+func (c *Controller) AttachContactAttachment(ctx *gin.Context, contactID string, attachmentTypeRaw string, req contactmodel.CommitAttachmentRequest) (*contactmodel.Entity, error) {
if !contactmodel.IsValidContactID(contactID) {
return nil, ente.NewBadRequestWithMessage("invalid contact id")
}
- if err := req.Validate(); err != nil {
- return nil, stacktrace.Propagate(err, "invalid attach profile picture request")
+ attachmentType, policy, err := c.attachmentTypeAndPolicy(attachmentTypeRaw)
+ if err != nil {
+ return nil, stacktrace.Propagate(err, "invalid attachment type")
+ }
+ if err := req.Validate(policy); err != nil {
+ return nil, stacktrace.Propagate(err, "invalid attach attachment request")
}
userID := auth.GetUserID(ctx.Request.Header)
- return c.Repo.AttachProfilePicture(ctx, userID, contactID, req.AttachmentID, req.Size)
+ stagedBucketID, err := c.Repo.GetStagedAttachmentBucket(ctx, userID, attachmentType, req.AttachmentID)
+ if err != nil {
+ if errors.Is(err, &ente.ErrNotFoundError) {
+ return nil, stacktrace.Propagate(
+ ente.NewBadRequestWithMessage("staged attachment upload not found"),
+ "",
+ )
+ }
+ return nil, stacktrace.Propagate(err, "failed to look up staged attachment")
+ }
+ objectKey := contactmodel.AttachmentObjectKey(userID, attachmentType, req.AttachmentID)
+ if err := c.verifyAttachmentFn(stagedBucketID, objectKey, req.Size); err != nil {
+ return nil, stacktrace.Propagate(err, "staged attachment verification failed")
+ }
+ return c.Repo.AttachContactAttachment(ctx, userID, contactID, attachmentType, req.AttachmentID, req.Size)
}
-func (c *Controller) GetProfilePictureURL(ctx *gin.Context, contactID string) (*contactmodel.SignedURLResponse, error) {
- if !contactmodel.IsValidContactID(contactID) {
- return nil, ente.NewBadRequestWithMessage("invalid contact id")
- }
- userID := auth.GetUserID(ctx.Request.Header)
- entity, err := c.Repo.Get(ctx, userID, contactID)
+func (c *Controller) AttachProfilePicture(ctx *gin.Context, contactID string, req contactmodel.CommitAttachmentRequest) (*contactmodel.Entity, error) {
+ return c.AttachContactAttachment(ctx, contactID, string(contactmodel.ProfilePicture), req)
+}
+
+func (c *Controller) GetAttachmentURL(ctx *gin.Context, attachmentTypeRaw string, attachmentID string) (*contactmodel.SignedURLResponse, error) {
+ attachmentType, _, err := c.attachmentTypeAndPolicy(attachmentTypeRaw)
if err != nil {
- return nil, stacktrace.Propagate(err, "")
+ return nil, stacktrace.Propagate(err, "invalid attachment type")
}
- if entity.IsDeleted || entity.ProfilePictureAttachmentID == nil {
- return nil, &ente.ErrNotFoundError
+ if !contactmodel.IsValidAttachmentID(attachmentID) {
+ return nil, ente.NewBadRequestWithMessage("invalid attachment id")
}
- attachment, err := c.Repo.GetAttachment(ctx, userID, *entity.ProfilePictureAttachmentID)
+ userID := auth.GetUserID(ctx.Request.Header)
+ attachment, err := c.Repo.GetAttachment(ctx, userID, attachmentID)
if err != nil {
return nil, stacktrace.Propagate(err, "")
}
- if attachment.IsDeleted || attachment.AttachmentType != contactmodel.ProfilePicture {
+ if attachment.IsDeleted || attachment.AttachmentType != attachmentType {
return nil, &ente.ErrNotFoundError
}
-
objectKey := contactmodel.AttachmentObjectKey(userID, attachment.AttachmentType, attachment.AttachmentID)
s3Client := c.S3Config.GetS3Client(attachment.LatestBucket)
input := &s3.GetObjectInput{
@@ -232,12 +250,68 @@ func (c *Controller) GetProfilePictureURL(ctx *gin.Context, contactID string) (*
return &contactmodel.SignedURLResponse{URL: url}, nil
}
-func (c *Controller) DeleteProfilePicture(ctx *gin.Context, contactID string) (*contactmodel.Entity, error) {
+func (c *Controller) GetCurrentContactAttachmentURL(ctx *gin.Context, contactID string, attachmentTypeRaw string) (*contactmodel.SignedURLResponse, error) {
if !contactmodel.IsValidContactID(contactID) {
return nil, ente.NewBadRequestWithMessage("invalid contact id")
}
+ attachmentType, _, err := c.attachmentTypeAndPolicy(attachmentTypeRaw)
+ if err != nil {
+ return nil, stacktrace.Propagate(err, "invalid attachment type")
+ }
userID := auth.GetUserID(ctx.Request.Header)
- return c.Repo.DeleteProfilePicture(ctx, userID, contactID)
+ entity, err := c.Repo.Get(ctx, userID, contactID)
+ if err != nil {
+ return nil, stacktrace.Propagate(err, "")
+ }
+ if entity.IsDeleted {
+ return nil, &ente.ErrNotFoundError
+ }
+ attachmentID := c.currentAttachmentID(entity, attachmentType)
+ if attachmentID == nil {
+ return nil, &ente.ErrNotFoundError
+ }
+ return c.GetAttachmentURL(ctx, attachmentTypeRaw, *attachmentID)
+}
+
+func (c *Controller) GetProfilePictureURL(ctx *gin.Context, contactID string) (*contactmodel.SignedURLResponse, error) {
+ return c.GetCurrentContactAttachmentURL(ctx, contactID, string(contactmodel.ProfilePicture))
+}
+
+func (c *Controller) DeleteContactAttachment(ctx *gin.Context, contactID string, attachmentTypeRaw string) (*contactmodel.Entity, error) {
+ if !contactmodel.IsValidContactID(contactID) {
+ return nil, ente.NewBadRequestWithMessage("invalid contact id")
+ }
+ attachmentType, _, err := c.attachmentTypeAndPolicy(attachmentTypeRaw)
+ if err != nil {
+ return nil, stacktrace.Propagate(err, "invalid attachment type")
+ }
+ userID := auth.GetUserID(ctx.Request.Header)
+ return c.Repo.DeleteContactAttachment(ctx, userID, contactID, attachmentType)
+}
+
+func (c *Controller) DeleteProfilePicture(ctx *gin.Context, contactID string) (*contactmodel.Entity, error) {
+ return c.DeleteContactAttachment(ctx, contactID, string(contactmodel.ProfilePicture))
+}
+
+func (c *Controller) attachmentTypeAndPolicy(raw string) (contactmodel.AttachmentType, contactmodel.AttachmentPolicy, error) {
+ attachmentType, err := contactmodel.ParseAttachmentType(raw)
+ if err != nil {
+ return "", contactmodel.AttachmentPolicy{}, err
+ }
+ policy, err := attachmentType.Policy()
+ if err != nil {
+ return "", contactmodel.AttachmentPolicy{}, err
+ }
+ return attachmentType, policy, nil
+}
+
+func (c *Controller) currentAttachmentID(entity *contactmodel.Entity, attachmentType contactmodel.AttachmentType) *string {
+ switch attachmentType {
+ case contactmodel.ProfilePicture:
+ return entity.ProfilePictureAttachmentID
+ default:
+ return nil
+ }
}
func (c *Controller) StartReplication() error {
diff --git a/server/pkg/controller/contact/controller_test.go b/server/pkg/controller/contact/controller_test.go
index cb3993feb2..8ccae19b7e 100644
--- a/server/pkg/controller/contact/controller_test.go
+++ b/server/pkg/controller/contact/controller_test.go
@@ -64,6 +64,9 @@ func setupContactControllerTest(t *testing.T) (*Controller, *sql.DB, *gin.Contex
SecretEncryptionKey: testutil.SecretEncryptionKey(),
}
ctrl := New(contactRepository, objectCleanupCtrl, s3Cfg)
+ ctrl.verifyAttachmentFn = func(bucketID string, objectKey string, expectedSize int64) error {
+ return nil
+ }
return ctrl, db, ctx, s3Cfg
}
@@ -614,18 +617,18 @@ func TestUserAttachmentsRejectDuplicateBucketMembership(t *testing.T) {
}
}
-func TestProfilePictureLifecycle(t *testing.T) {
+func TestAttachmentLifecycle(t *testing.T) {
ctrl, db, ctx, s3Cfg := setupContactControllerTest(t)
mustInsertTestUser(t, db, 1)
created := createContactForTest(t, db, ctrl, ctx, 41, "wrapped-key-1", "payload-1")
- upload1, err := ctrl.GetProfilePictureUploadURL(ctx, created.ID, contactmodel.ProfilePictureUploadURLRequest{
+ upload1, err := ctrl.GetAttachmentUploadURL(ctx, string(contactmodel.ProfilePicture), contactmodel.AttachmentUploadURLRequest{
ContentLength: 128,
ContentMD5: "ZmFrZS1tZDU=",
})
if err != nil {
- t.Fatalf("GetProfilePictureUploadURL() error = %v", err)
+ t.Fatalf("GetAttachmentUploadURL() error = %v", err)
}
if !strings.HasPrefix(upload1.AttachmentID, "ua_") {
t.Fatalf("attachment id = %q, want ua_ prefix", upload1.AttachmentID)
@@ -640,12 +643,12 @@ func TestProfilePictureLifecycle(t *testing.T) {
t.Fatalf("bucket_id = %q, want %q", bucketID, s3Cfg.GetAttachmentBucketID(string(contactmodel.ProfilePicture)))
}
- withPicture, err := ctrl.AttachProfilePicture(ctx, created.ID, contactmodel.CommitProfilePictureRequest{
+ withPicture, err := ctrl.AttachContactAttachment(ctx, created.ID, string(contactmodel.ProfilePicture), contactmodel.CommitAttachmentRequest{
AttachmentID: upload1.AttachmentID,
Size: 128,
})
if err != nil {
- t.Fatalf("AttachProfilePicture() error = %v", err)
+ t.Fatalf("AttachContactAttachment() error = %v", err)
}
if withPicture.ProfilePictureAttachmentID == nil || *withPicture.ProfilePictureAttachmentID != upload1.AttachmentID {
t.Fatalf("unexpected profile picture ref after attach: %+v", withPicture)
@@ -654,27 +657,27 @@ func TestProfilePictureLifecycle(t *testing.T) {
t.Fatalf("temp_objects row should be removed after attach, got err=%v bucket=%q", err, bucketID)
}
- signedURL, err := ctrl.GetProfilePictureURL(ctx, created.ID)
+ signedURL, err := ctrl.GetAttachmentURL(ctx, string(contactmodel.ProfilePicture), upload1.AttachmentID)
if err != nil {
- t.Fatalf("GetProfilePictureURL() error = %v", err)
+ t.Fatalf("GetAttachmentURL() error = %v", err)
}
if signedURL.URL == "" {
- t.Fatal("expected signed profile picture url")
+ t.Fatal("expected signed attachment url")
}
- upload2, err := ctrl.GetProfilePictureUploadURL(ctx, created.ID, contactmodel.ProfilePictureUploadURLRequest{
+ upload2, err := ctrl.GetAttachmentUploadURL(ctx, string(contactmodel.ProfilePicture), contactmodel.AttachmentUploadURLRequest{
ContentLength: 256,
ContentMD5: "ZmFrZS1tZDUtMg==",
})
if err != nil {
- t.Fatalf("second GetProfilePictureUploadURL() error = %v", err)
+ t.Fatalf("second GetAttachmentUploadURL() error = %v", err)
}
- replaced, err := ctrl.AttachProfilePicture(ctx, created.ID, contactmodel.CommitProfilePictureRequest{
+ replaced, err := ctrl.AttachContactAttachment(ctx, created.ID, string(contactmodel.ProfilePicture), contactmodel.CommitAttachmentRequest{
AttachmentID: upload2.AttachmentID,
Size: 256,
})
if err != nil {
- t.Fatalf("second AttachProfilePicture() error = %v", err)
+ t.Fatalf("second AttachContactAttachment() error = %v", err)
}
if replaced.ProfilePictureAttachmentID == nil || *replaced.ProfilePictureAttachmentID != upload2.AttachmentID {
t.Fatalf("unexpected profile picture ref after replace: %+v", replaced)
@@ -688,9 +691,9 @@ func TestProfilePictureLifecycle(t *testing.T) {
t.Fatalf("old attachment should be marked deleted after replace")
}
- cleared, err := ctrl.DeleteProfilePicture(ctx, created.ID)
+ cleared, err := ctrl.DeleteContactAttachment(ctx, created.ID, string(contactmodel.ProfilePicture))
if err != nil {
- t.Fatalf("DeleteProfilePicture() error = %v", err)
+ t.Fatalf("DeleteContactAttachment() error = %v", err)
}
if cleared.ProfilePictureAttachmentID != nil {
t.Fatalf("profile picture should be cleared, got %+v", cleared.ProfilePictureAttachmentID)
@@ -704,24 +707,68 @@ func TestProfilePictureLifecycle(t *testing.T) {
}
}
+func TestAttachContactAttachmentFailsWhenStagedObjectVerificationFails(t *testing.T) {
+ ctrl, db, ctx, _ := setupContactControllerTest(t)
+ mustInsertTestUser(t, db, 1)
+
+ created := createContactForTest(t, db, ctrl, ctx, 42, "wrapped-key-1", "payload-1")
+ upload, err := ctrl.GetAttachmentUploadURL(ctx, string(contactmodel.ProfilePicture), contactmodel.AttachmentUploadURLRequest{
+ ContentLength: 128,
+ ContentMD5: "ZmFrZS1tZDU=",
+ })
+ if err != nil {
+ t.Fatalf("GetAttachmentUploadURL() error = %v", err)
+ }
+
+ ctrl.verifyAttachmentFn = func(bucketID string, objectKey string, expectedSize int64) error {
+ return errors.New("staged object missing")
+ }
+
+ if _, err := ctrl.AttachContactAttachment(ctx, created.ID, string(contactmodel.ProfilePicture), contactmodel.CommitAttachmentRequest{
+ AttachmentID: upload.AttachmentID,
+ Size: 128,
+ }); err == nil {
+ t.Fatal("expected attach to fail when staged object verification fails")
+ }
+
+ objectKey := contactmodel.AttachmentObjectKey(1, contactmodel.ProfilePicture, upload.AttachmentID)
+ var bucketID string
+ if err := db.QueryRow(`SELECT bucket_id FROM temp_objects WHERE object_key = $1`, objectKey).Scan(&bucketID); err != nil {
+ t.Fatalf("temp_objects row should remain after failed attach, err=%v", err)
+ }
+ if _, err := ctrl.Repo.GetAttachment(ctx, 1, upload.AttachmentID); err == nil {
+ t.Fatal("attachment row should not be created when staged verification fails")
+ } else if !errors.Is(err, &ente.ErrNotFoundError) {
+ t.Fatalf("unexpected GetAttachment error = %v", err)
+ }
+
+ refetched, err := ctrl.Get(ctx, created.ID)
+ if err != nil {
+ t.Fatalf("Get() error = %v", err)
+ }
+ if refetched.ProfilePictureAttachmentID != nil {
+ t.Fatalf("contact should not reference profile picture after failed attach")
+ }
+}
+
func TestDeleteContactMarksCurrentProfilePictureDeleted(t *testing.T) {
ctrl, db, ctx, _ := setupContactControllerTest(t)
mustInsertTestUser(t, db, 1)
created := createContactForTest(t, db, ctrl, ctx, 51, "wrapped-key-1", "payload-1")
- upload, err := ctrl.GetProfilePictureUploadURL(ctx, created.ID, contactmodel.ProfilePictureUploadURLRequest{
+ upload, err := ctrl.GetAttachmentUploadURL(ctx, string(contactmodel.ProfilePicture), contactmodel.AttachmentUploadURLRequest{
ContentLength: 128,
ContentMD5: "ZmFrZS1tZDU=",
})
if err != nil {
- t.Fatalf("GetProfilePictureUploadURL() error = %v", err)
+ t.Fatalf("GetAttachmentUploadURL() error = %v", err)
}
- withPicture, err := ctrl.AttachProfilePicture(ctx, created.ID, contactmodel.CommitProfilePictureRequest{
+ withPicture, err := ctrl.AttachContactAttachment(ctx, created.ID, string(contactmodel.ProfilePicture), contactmodel.CommitAttachmentRequest{
AttachmentID: upload.AttachmentID,
Size: 128,
})
if err != nil {
- t.Fatalf("AttachProfilePicture() error = %v", err)
+ t.Fatalf("AttachContactAttachment() error = %v", err)
}
if withPicture.ProfilePictureAttachmentID == nil {
t.Fatal("expected profile picture ref before delete")
@@ -746,6 +793,45 @@ func TestDeleteContactMarksCurrentProfilePictureDeleted(t *testing.T) {
}
}
+func TestAttachmentUploadURLRejectsInvalidType(t *testing.T) {
+ ctrl, _, ctx, _ := setupContactControllerTest(t)
+
+ _, err := ctrl.GetAttachmentUploadURL(ctx, "unknown_type", contactmodel.AttachmentUploadURLRequest{
+ ContentLength: 128,
+ ContentMD5: "ZmFrZS1tZDU=",
+ })
+ if err == nil {
+ t.Fatal("expected invalid attachment type error")
+ }
+}
+
+func TestGetAttachmentURLRejectsWrongUserAndDeletedRows(t *testing.T) {
+ ctrl, db, ctx, _ := setupContactControllerTest(t)
+ mustInsertTestUser(t, db, 1)
+ mustInsertTestUser(t, db, 2)
+
+ mustInsertAttachmentRow(t, db, "ua_owned", 1, false, "b2-eu-cen", nil, nil, nil)
+ if _, err := ctrl.GetAttachmentURL(ctx, string(contactmodel.ProfilePicture), "ua_owned"); err != nil {
+ t.Fatalf("GetAttachmentURL() for owner error = %v", err)
+ }
+
+ ctxOther := ctx.Copy()
+ ctxOther.Request = ctx.Request.Clone(ctx.Request.Context())
+ ctxOther.Request.Header.Set("X-Auth-User-ID", "2")
+ if _, err := ctrl.GetAttachmentURL(ctxOther, string(contactmodel.ProfilePicture), "ua_owned"); err == nil {
+ t.Fatal("expected wrong-user attachment lookup to fail")
+ }
+
+ mustInsertAttachmentRow(t, db, "ua_deleted", 1, true, "b2-eu-cen", nil, []string{"scw-eu-fr"}, nil)
+ if _, err := ctrl.GetAttachmentURL(ctx, string(contactmodel.ProfilePicture), "ua_deleted"); err == nil {
+ t.Fatal("expected deleted attachment lookup to fail")
+ }
+
+ if _, err := ctrl.GetAttachmentURL(ctx, "unknown_type", "ua_owned"); err == nil {
+ t.Fatal("expected invalid attachment type error")
+ }
+}
+
func TestAttachmentReplicationLifecycle(t *testing.T) {
ctrl, db, _, _ := setupContactControllerTest(t)
mustInsertTestUser(t, db, 1)
diff --git a/server/pkg/repo/contact/contact.go b/server/pkg/repo/contact/contact.go
index 6ba0275a58..955ae98db2 100644
--- a/server/pkg/repo/contact/contact.go
+++ b/server/pkg/repo/contact/contact.go
@@ -233,7 +233,7 @@ func (r *Repository) Delete(ctx context.Context, userID int64, id string) error
}
if entity.ProfilePictureAttachmentID != nil {
- if err := markAttachmentDeletedTx(ctx, tx, userID, *entity.ProfilePictureAttachmentID); err != nil {
+ if err := markAttachmentDeletedTx(ctx, tx, userID, contactmodel.ProfilePicture, *entity.ProfilePictureAttachmentID); err != nil {
return stacktrace.Propagate(err, "")
}
}
@@ -295,7 +295,18 @@ func (r *Repository) TouchContactsForContactUser(ctx context.Context, contactUse
return stacktrace.Propagate(err, "failed to touch contacts for contact user")
}
-func (r *Repository) AttachProfilePicture(ctx context.Context, userID int64, contactID string, attachmentID string, size int64) (*contactmodel.Entity, error) {
+func (r *Repository) AttachContactAttachment(
+ ctx context.Context,
+ userID int64,
+ contactID string,
+ attachmentType contactmodel.AttachmentType,
+ attachmentID string,
+ size int64,
+) (*contactmodel.Entity, error) {
+ policy, err := attachmentType.Policy()
+ if err != nil {
+ return nil, stacktrace.Propagate(err, "")
+ }
tx, err := r.DB.BeginTx(ctx, nil)
if err != nil {
return nil, stacktrace.Propagate(err, "")
@@ -310,7 +321,7 @@ func (r *Repository) AttachProfilePicture(ctx context.Context, userID int64, con
return nil, stacktrace.Propagate(ente.NewBadRequestWithMessage("contact is already deleted"), "")
}
- objectKey := contactmodel.AttachmentObjectKey(userID, contactmodel.ProfilePicture, attachmentID)
+ objectKey := contactmodel.AttachmentObjectKey(userID, attachmentType, attachmentID)
var latestBucket string
if err := tx.QueryRowContext(
ctx,
@@ -318,9 +329,9 @@ func (r *Repository) AttachProfilePicture(ctx context.Context, userID int64, con
objectKey,
).Scan(&latestBucket); err != nil {
if errors.Is(err, sql.ErrNoRows) {
- return nil, stacktrace.Propagate(ente.NewBadRequestWithMessage("staged profile picture upload not found"), "")
+ return nil, stacktrace.Propagate(ente.NewBadRequestWithMessage("staged attachment upload not found"), "")
}
- return nil, stacktrace.Propagate(err, "failed to fetch staged profile picture")
+ return nil, stacktrace.Propagate(err, "failed to fetch staged attachment")
}
if _, err := tx.ExecContext(
@@ -329,7 +340,7 @@ func (r *Repository) AttachProfilePicture(ctx context.Context, userID int64, con
VALUES($1, $2, $3, $4, $5)`,
attachmentID,
userID,
- string(contactmodel.ProfilePicture),
+ string(attachmentType),
size,
latestBucket,
); err != nil {
@@ -340,26 +351,31 @@ func (r *Repository) AttachProfilePicture(ctx context.Context, userID int64, con
return nil, stacktrace.Propagate(err, "failed to insert attachment row")
}
- if entity.ProfilePictureAttachmentID != nil && *entity.ProfilePictureAttachmentID != "" {
- if err := markAttachmentDeletedTx(ctx, tx, userID, *entity.ProfilePictureAttachmentID); err != nil {
+ currentAttachmentID := attachmentIDForPolicy(entity, policy)
+ if currentAttachmentID != nil && *currentAttachmentID != "" {
+ if err := markAttachmentDeletedTx(ctx, tx, userID, attachmentType, *currentAttachmentID); err != nil {
return nil, stacktrace.Propagate(err, "")
}
}
- if _, err := tx.ExecContext(
- ctx,
+ updateSlotQuery := fmt.Sprintf(
`UPDATE contact_entity
- SET profile_picture_attachment_id = $1
+ SET %s = $1
WHERE id = $2 AND user_id = $3 AND is_deleted = FALSE`,
+ policy.SlotColumn,
+ )
+ if _, err := tx.ExecContext(
+ ctx,
+ updateSlotQuery,
attachmentID,
contactID,
userID,
); err != nil {
- return nil, stacktrace.Propagate(err, "failed to update contact profile picture")
+ return nil, stacktrace.Propagate(err, "failed to update contact attachment")
}
if err := r.ObjectCleanupRepo.RemoveTempObjectFromDC(ctx, tx, objectKey, latestBucket); err != nil {
- return nil, stacktrace.Propagate(err, "failed to remove profile picture from temp_objects")
+ return nil, stacktrace.Propagate(err, "failed to remove attachment from temp_objects")
}
if err := tx.Commit(); err != nil {
@@ -368,7 +384,16 @@ func (r *Repository) AttachProfilePicture(ctx context.Context, userID int64, con
return r.Get(ctx, userID, contactID)
}
-func (r *Repository) DeleteProfilePicture(ctx context.Context, userID int64, contactID string) (*contactmodel.Entity, error) {
+func (r *Repository) DeleteContactAttachment(
+ ctx context.Context,
+ userID int64,
+ contactID string,
+ attachmentType contactmodel.AttachmentType,
+) (*contactmodel.Entity, error) {
+ policy, err := attachmentType.Policy()
+ if err != nil {
+ return nil, stacktrace.Propagate(err, "")
+ }
tx, err := r.DB.BeginTx(ctx, nil)
if err != nil {
return nil, stacktrace.Propagate(err, "")
@@ -382,19 +407,24 @@ func (r *Repository) DeleteProfilePicture(ctx context.Context, userID int64, con
if entity.IsDeleted {
return nil, stacktrace.Propagate(ente.NewBadRequestWithMessage("contact is already deleted"), "")
}
- if entity.ProfilePictureAttachmentID != nil {
- if err := markAttachmentDeletedTx(ctx, tx, userID, *entity.ProfilePictureAttachmentID); err != nil {
+ currentAttachmentID := attachmentIDForPolicy(entity, policy)
+ if currentAttachmentID != nil {
+ if err := markAttachmentDeletedTx(ctx, tx, userID, attachmentType, *currentAttachmentID); err != nil {
return nil, stacktrace.Propagate(err, "")
}
- if _, err := tx.ExecContext(
- ctx,
+ clearSlotQuery := fmt.Sprintf(
`UPDATE contact_entity
- SET profile_picture_attachment_id = NULL
+ SET %s = NULL
WHERE id = $1 AND user_id = $2 AND is_deleted = FALSE`,
+ policy.SlotColumn,
+ )
+ if _, err := tx.ExecContext(
+ ctx,
+ clearSlotQuery,
contactID,
userID,
); err != nil {
- return nil, stacktrace.Propagate(err, "failed to clear contact profile picture")
+ return nil, stacktrace.Propagate(err, "failed to clear contact attachment")
}
}
if err := tx.Commit(); err != nil {
@@ -424,6 +454,30 @@ func (r *Repository) GetAttachment(ctx context.Context, userID int64, attachment
return attachment, nil
}
+func (r *Repository) GetStagedAttachmentBucket(
+ ctx context.Context,
+ userID int64,
+ attachmentType contactmodel.AttachmentType,
+ attachmentID string,
+) (string, error) {
+ objectKey := contactmodel.AttachmentObjectKey(userID, attachmentType, attachmentID)
+ var bucketID string
+ err := r.DB.QueryRowContext(
+ ctx,
+ `SELECT bucket_id
+ FROM temp_objects
+ WHERE object_key = $1`,
+ objectKey,
+ ).Scan(&bucketID)
+ if err != nil {
+ if errors.Is(err, sql.ErrNoRows) {
+ return "", &ente.ErrNotFoundError
+ }
+ return "", stacktrace.Propagate(err, "failed to fetch staged attachment bucket")
+ }
+ return bucketID, nil
+}
+
func (r *Repository) AddBucket(row contactmodel.Attachment, bucketID string, columnName string) error {
query := fmt.Sprintf(`
UPDATE user_attachments
@@ -661,7 +715,7 @@ func (r *Repository) getForUpdate(ctx context.Context, tx *sql.Tx, userID int64,
return entity, nil
}
-func markAttachmentDeletedTx(ctx context.Context, tx *sql.Tx, userID int64, attachmentID string) error {
+func markAttachmentDeletedTx(ctx context.Context, tx *sql.Tx, userID int64, attachmentType contactmodel.AttachmentType, attachmentID string) error {
_, err := tx.ExecContext(
ctx,
`UPDATE user_attachments
@@ -681,11 +735,20 @@ func markAttachmentDeletedTx(ctx context.Context, tx *sql.Tx, userID int64, atta
AND is_deleted = FALSE`,
attachmentID,
userID,
- string(contactmodel.ProfilePicture),
+ string(attachmentType),
)
return stacktrace.Propagate(err, "failed to mark attachment deleted")
}
+func attachmentIDForPolicy(entity *contactmodel.Entity, policy contactmodel.AttachmentPolicy) *string {
+ switch policy.Type {
+ case contactmodel.ProfilePicture:
+ return entity.ProfilePictureAttachmentID
+ default:
+ return nil
+ }
+}
+
type rowScanner interface {
Scan(dest ...interface{}) error
}