summaryrefslogtreecommitdiff
path: root/main.go
diff options
context:
space:
mode:
authorPablo M. Bermudo Garay <pablombg@gmail.com>2022-02-09 19:58:30 +0100
committerPablo M. Bermudo Garay <pablombg@gmail.com>2022-02-10 13:14:19 +0100
commit53667583c094252818f203b9233a7726d0a4e8a0 (patch)
tree7bcc7cbaf784f149113cba85a837515d70ae1c87 /main.go
parentc488ed1f80fd54846170c0ab04ce59a69f52a9e5 (diff)
Add proactive de-duplication to the memory backend
Diffstat (limited to 'main.go')
-rw-r--r--main.go19
1 files changed, 18 insertions, 1 deletions
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)
}