From 70b9a8f3571c0a296e69608ce329233ba2831e5e Mon Sep 17 00:00:00 2001
From: Tristan Cavelier <tristan.cavelier@tiolive.com>
Date: Thu, 18 Jul 2013 09:37:21 +0200
Subject: [PATCH] gidstorage get attachment method added

---
 src/jio.storage/gidstorage.js | 50 +++++++++++++++++++++++++++++++++++
 1 file changed, 50 insertions(+)

diff --git a/src/jio.storage/gidstorage.js b/src/jio.storage/gidstorage.js
index b7cbfbb..16640d4 100644
--- a/src/jio.storage/gidstorage.js
+++ b/src/jio.storage/gidstorage.js
@@ -420,6 +420,56 @@
       });
     };
 
+    /**
+     * Gets an attachment from a document thank to its gid, a sub allDocs and a
+     * complex query.
+     *
+     * @method getAttachment
+     * @param  {Command} command The JIO command
+     */
+    that.getAttachment = function (command) {
+      setTimeout(function () {
+        var gid_object, complex_query, doc = command.cloneDoc();
+        gid_object = gidParse(doc._id, priv.constraints);
+        if (gid_object === undefined) {
+          return that.error({
+            "status": 400,
+            "statusText": "Bad Request",
+            "error": "bad_request",
+            "message": "Cannot get attachment",
+            "reason": "metadata should respect constraints"
+          });
+        }
+        complex_query = gidToComplexQuery(gid_object);
+        that.addJob('allDocs', priv.sub_storage, {}, {
+          "query": complex_query,
+          "wildcard_character": null
+        }, function (response) {
+          if (response.total_rows === 0) {
+            return that.error({
+              "status": 404,
+              "statusText": "Not Found",
+              "error": "not_found",
+              "message": "Cannot get attachment",
+              "reason": "Document already exist"
+            });
+          }
+          gid_object = doc._id;
+          doc._id = response.rows[0].id;
+          that.addJob('getAttachment', priv.sub_storage, doc, {
+          }, function (response) {
+            that.success(response);
+          }, function (err) {
+            err.message = "Cannot put attachment";
+            that.error(err);
+          });
+        }, function (err) {
+          err.message = "Cannot put attachment";
+          that.error(err);
+        });
+      });
+    };
+
     /**
      * Remove a document thank to its gid, sub allDocs and a complex query.
      *
-- 
2.30.9