diff options
| author | Pablo M. Bermudo Garay <pablombg@gmail.com> | 2022-02-09 19:58:30 +0100 |
|---|---|---|
| committer | Pablo M. Bermudo Garay <pablombg@gmail.com> | 2022-02-10 13:14:19 +0100 |
| commit | 53667583c094252818f203b9233a7726d0a4e8a0 (patch) | |
| tree | 7bcc7cbaf784f149113cba85a837515d70ae1c87 /main.go | |
| parent | c488ed1f80fd54846170c0ab04ce59a69f52a9e5 (diff) | |
Add proactive de-duplication to the memory backend
Diffstat (limited to 'main.go')
| -rw-r--r-- | main.go | 19 |
1 files changed, 18 insertions, 1 deletions
@@ -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) } |
