From 53667583c094252818f203b9233a7726d0a4e8a0 Mon Sep 17 00:00:00 2001 From: "Pablo M. Bermudo Garay" Date: Wed, 9 Feb 2022 19:58:30 +0100 Subject: Add proactive de-duplication to the memory backend --- main.go | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) (limited to 'main.go') diff --git a/main.go b/main.go index 71f8d34..c4104e3 100644 --- a/main.go +++ b/main.go @@ -26,6 +26,7 @@ func (s *storehouse) putObject(c echo.Context) error { bucketId := c.Param("bucketId") objectId := c.Param("objectId") + // Get body as a string bodyBytes, err := ioutil.ReadAll(c.Request().Body) if err != nil { log.Printf("Error reading body: %v", err) @@ -34,7 +35,23 @@ func (s *storehouse) putObject(c echo.Context) error { } content := string(bodyBytes) - s.objects.CreateObject(bucketId, objectId, content) + err = s.objects.CreateObject(bucketId, objectId, content) + // Check errors creating the object + if err != nil { + log.Printf("Error creating the object: %v", err) + switch err.(type) { + case *objectstore.DuplicateError: + // Return "409 Conflict" if there + // is a duplicate of the object + m := &errorJSON{Error: err.Error()} + return c.JSON(http.StatusConflict, m) + default: + m := &errorJSON{Error: "Internal error"} + return c.JSON(http.StatusInternalServerError, m) + } + } + + // Return success m := &objectIdJSON{Id: objectId} return c.JSON(http.StatusCreated, m) } -- cgit v1.2.3-70-g09d2