π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
[server] Alert on object replcement
Neeraj Guptaa54cbd779ad6
diff --git a/server/cmd/museum/main.go b/server/cmd/museum/main.goindex aa214fe724..b5f18af4b3 100644--- a/server/cmd/museum/main.go+++ b/server/cmd/museum/main.go@@ -255,6 +255,7 @@ func main() {UsageCtrl: usageController,CollectionRepo: collectionRepo,TaskLockingRepo: taskLockingRepo,+ DiscordController: discordController,QueueRepo: queueRepo,ObjectCleanupCtrl: objectCleanupController,LockController: lockController,diff --git a/server/pkg/controller/file.go b/server/pkg/controller/file.goindex 30e3fddc7b..9fd8cd7d23 100644--- a/server/pkg/controller/file.go+++ b/server/pkg/controller/file.go@@ -6,6 +6,8 @@ import ("encoding/json""errors""fmt"+ "github.com/ente-io/museum/pkg/controller/discord"+ "github.com/ente-io/museum/pkg/utils/network""runtime/debug""strconv""strings"@@ -43,6 +45,7 @@ type FileController struct {ObjectCleanupCtrl *ObjectCleanupControllerLockController *lock.LockControllerEmailNotificationCtrl *email.EmailNotificationController+ DiscordController *discord.DiscordControllerHostName stringcleanupCronRunning bool}@@ -102,7 +105,7 @@ func (c *FileController) validateFileCreateOrUpdateReq(userID int64, file ente.F}// Create adds an entry for a file in the respective tables-func (c *FileController) Create(ctx context.Context, userID int64, file ente.File, userAgent string, app ente.App) (ente.File, error) {+func (c *FileController) Create(ctx *gin.Context, userID int64, file ente.File, userAgent string, app ente.App) (ente.File, error) {err := c.validateFileCreateOrUpdateReq(userID, file)if err != nil {return file, stacktrace.Propagate(err, "")@@ -156,7 +159,7 @@ func (c *FileController) Create(ctx context.Context, userID int64, file ente.Filif err != nil {return file, stacktrace.Propagate(err, "")}- file, err = c.onDuplicateObjectDetected(file, existing, hotDC)+ file, err = c.onDuplicateObjectDetected(ctx, file, existing, hotDC)if err != nil {return file, stacktrace.Propagate(err, "")}@@ -787,7 +790,7 @@ func (c *FileController) sizeOf(objectKey string) (int64, error) {return *head.ContentLength, nil}-func (c *FileController) onDuplicateObjectDetected(file ente.File, existing ente.File, hotDC string) (ente.File, error) {+func (c *FileController) onDuplicateObjectDetected(ctx *gin.Context, file ente.File, existing ente.File, hotDC string) (ente.File, error) {newJSON, _ := json.Marshal(file)existingJSON, _ := json.Marshal(existing)log.Info("Comparing " + string(newJSON) + " against " + string(existingJSON))@@ -805,27 +808,60 @@ func (c *FileController) onDuplicateObjectDetected(file ente.File, existing entereturn file, nil} else {// Overwrote an existing file or thumbnail- go c.onExistingObjectsReplaced(file, hotDC)+ go c.onExistingObjectsReplaced(ctx, file, hotDC)return ente.File{}, ente.ErrBadRequest}}-func (c *FileController) onExistingObjectsReplaced(file ente.File, hotDC string) {+func (c *FileController) safeAlert(msg string) {defer func() {if r := recover(); r != nil {log.Errorf("Panic caught: %s, stack: %s", r, string(debug.Stack()))}}()+ c.DiscordController.Notify(msg)+}++func (c *FileController) onExistingObjectsReplaced(ctx *gin.Context, file ente.File, hotDC string) {+ defer func() {+ if r := recover(); r != nil {+ log.Errorf("Panic caught: %s, stack: %s", r, string(debug.Stack()))+ }+ }()+ client := network.GetClientInfo(ctx)+ reqId := requestid.Get(ctx)+ revertErr := false+ go c.safeAlert(fmt.Sprintf(`Client %s replaced an existing object req_id %s for (file: %s, thum %s)`, client, reqId, file.File.ObjectKey, file.Thumbnail.ObjectKey))log.Error("Replaced existing object, reverting", file)+ logger := log.WithFields(log.Fields{+ "req_id": reqId,+ "owner_id": file.OwnerID,+ "file_obj": file.File.ObjectKey,+ "fileSize": file.File.Size,+ "thumb_obj": file.Thumbnail.ObjectKey,+ "thumbSize": file.Thumbnail.Size,+ })+err := c.rollbackObject(file.File.ObjectKey)if err != nil {- log.Error("Error rolling back latest file from hot storage", err)+ logger.Error("Error rolling back latest file from hot storage", err)+ revertErr = true}err = c.rollbackObject(file.Thumbnail.ObjectKey)if err != nil {log.Error("Error rolling back latest thumbnail from hot storage", err)+ revertErr = true+ }+ resetErr := c.FileRepo.ResetNeedsReplication(file, hotDC)+ if resetErr != nil {+ log.Error("Error resetting needs replication", resetErr)+ revertErr = true+ }+ if revertErr {+ go c.safeAlert(fmt.Sprintf(`β οΈ Client %s replaced an existing object req_id %s for user %d, failed to revert`, client, reqId, file.OwnerID))+ } else {+ go c.safeAlert(fmt.Sprintf(`π Client %s replaced an existing object req_id %s for user %d, reverted`, client, reqId, file.OwnerID))}- c.FileRepo.ResetNeedsReplication(file, hotDC)}func (c *FileController) rollbackObject(objectKey string) error {