davstorage.js 28 KB
Newer Older
1
/*
2 3 4 5
 * Copyright 2013, Nexedi SA
 * Released under the LGPL license.
 * http://www.gnu.org/licenses/lgpl.html
 */
6

7
/*jslint indent: 2, maxlen: 80, nomen: true */
8
/*global define, jIO, jQuery, btoa */
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49

// JIO Dav Storage Description :
// {
//   type: "dav",
//   url: {string}
// }

// {
//   type: "dav",
//   url: {string},
//   auth_type: {string}, (optional)
//     - "auto" (default) (not implemented)
//     - "basic"
//     - "digest" (not implemented)
//   realm: {string}, (optional)
//     - undefined (default) (not implemented)
//     - "<string>" realm name (not implemented)
//   username: {string},
//   password: {string}  (optional)
// }

// {
//   type: "dav",
//   url: {string},
//   encoded_login: {string}
// }

// {
//   type: "dav",
//   url: {string},
//   secured_login: {string} (not implemented)
// }

// NOTE: to get the authentication type ->
// curl --verbose  -X OPTION http://domain/
// In the headers: "WWW-Authenticate: Basic realm="DAV-upload"

// URL Characters convertion:
// If I want to retrieve the file which id is -> http://100%.json
// http://domain/collection/http://100%.json cannot be applied
// - '/' is col separator,
50
// - '?' is url/parameter separator
51 52 53 54 55 56 57 58 59
// - '%' is special char
// - '.' document and attachment separator
// http://100%.json will become
// - http:%2F%2F100%25.json to avoid bad request ('/', '%' -> '%2F', '%25')
// - http:%2F%2F100%25_.json to avoid ids conflicts ('.' -> '_.')
// - http:%252F%252F100%2525_.json to avoid request bad interpretation
//   ('%', '%25')
// The file will be saved as http:%2F%2F100%25_.json

60 61
// define([module_name], [dependencies], module);
(function (dependencies, module) {
62
  "use strict";
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
  if (typeof define === 'function' && define.amd) {
    return define(dependencies, module);
  }
  module(jIO, jQuery);
}(['jio', 'jquery'], function (jIO, $) {
  "use strict";
  jIO.addStorageType("dav", function (spec, my) {
    var priv = {}, that = my.basicStorage(spec, my), dav = {};

    // ATTRIBUTES //
    priv.url = null;
    priv.username = null;
    priv.password = null;
    priv.encoded_login = null;

    // CONSTRUCTOR //
    /**
     * Init the dav storage connector thanks to the description
     * @method __init__
     * @param  {object} description The description object
     */
    priv.__init__ = function (description) {
      priv.url = description.url || "";
      priv.url = priv.removeSlashIfLast(priv.url);
      // if (description.secured_login) {
      //    not implemented
      // } else
      if (description.encoded_login) {
        priv.encoded_login = description.encoded_login;
      } else if (description.auth_type) {
        if (description.auth_type === "basic") {
          priv.encoded_login = "Basic " +
            btoa((description.username || "") + ":" +
                 (description.password || ""));
        }
      } else {
        priv.encoded_login = "";
Sven Franck's avatar
Sven Franck committed
100
      }
101
    };
102 103 104 105 106 107 108 109 110 111 112 113

    // OVERRIDES //
    that.specToStore = function () {
      // TODO: secured password
      // The encoded_login can be seen by anyone,
      // we must find a way to secure it!
      // secured_login = encrypt(encoded_login)
      // encoded_login = decrypt(secured_login)
      return {
        "url": priv.url,
        "encoded_login": priv.encoded_login
      };
114
    };
115 116 117 118

    that.validateState = function () {
      if (typeof priv.url !== "string" || priv.url === "") {
        return "The webDav server URL is not provided";
119
      }
120 121
      if (priv.encoded_login === null) {
        return "Impossible to create the authorization";
122
      }
123
      return "";
124
    };
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139

    // TOOLS //
    /**
     * Generate a new uuid
     * @method generateUuid
     * @return {string} The new uuid
     */
    priv.generateUuid = function () {
      var S4 = function () {
        /* 65536 */
        var i, string = Math.floor(
          Math.random() * 0x10000
        ).toString(16);
        for (i = string.length; i < 4; i += 1) {
          string = "0" + string;
140
        }
141 142 143 144
        return string;
      };
      return S4() + S4() + "-" + S4() + "-" + S4() + "-" + S4() + "-" + S4() +
        S4() + S4();
145
    };
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176

    // /**
    //  * Clones an object in deep
    //  * @method clone
    //  * @param  {object} object The object to clone
    //  * @return {object} The cloned object
    //  */
    // priv.clone = function (object) {
    //   var tmp = JSON.stringify(object);
    //   if (tmp === undefined) {
    //     return undefined;
    //   }
    //   return JSON.parse(tmp);
    // };

    /**
     * Replace substrings to another strings
     * @method recursiveReplace
     * @param  {string} string The string to do replacement
     * @param  {array} list_of_replacement An array of couple
     * ["substring to select", "selected substring replaced by this string"].
     * @return {string} The replaced string
     */
    priv.recursiveReplace = function (string, list_of_replacement) {
      var i, split_string = string.split(list_of_replacement[0][0]);
      if (list_of_replacement[1]) {
        for (i = 0; i < split_string.length; i += 1) {
          split_string[i] = priv.recursiveReplace(
            split_string[i],
            list_of_replacement.slice(1)
          );
177 178
        }
      }
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
      return split_string.join(list_of_replacement[0][1]);
    };

    /**
     * Changes spaces to %20, / to %2f, % to %25 and ? to %3f
     * @method secureName
     * @param  {string} name The name to secure
     * @return {string} The secured name
     */
    priv.secureName = function (name) {
      return priv.recursiveReplace(name, [
        [" ", "%20"],
        ["/", "%2F"],
        ["%", "%25"],
        ["?", "%3F"]
      ]);
    };

    /**
     * Restores the original name from a secured name
     * @method restoreName
     * @param  {string} secured_name The secured name to restore
     * @return {string} The original name
     */
    priv.restoreName = function (secured_name) {
      return priv.recursiveReplace(secured_name, [
        ["%20", " "],
        ["%2F", "/"],
        ["%25", "%"],
        ["%3F", "?"]
      ]);
    };

    /**
     * Convert document id and attachment id to a file name
     * @method idsToFileName
     * @param  {string} doc_id The document id
     * @param  {string} attachment_id The attachment id (optional)
     * @return {string} The file name
     */
    priv.idsToFileName = function (doc_id, attachment_id) {
      doc_id = priv.secureName(doc_id).split(".").join("_.");
      if (typeof attachment_id === "string") {
        attachment_id = priv.secureName(attachment_id).split(".").join("_.");
        return doc_id + "." + attachment_id;
224
      }
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
      return doc_id;
    };

    /**
     * Convert a file name to a document id (and attachment id if there)
     * @method fileNameToIds
     * @param  {string} file_name The file name to convert
     * @return {array} ["document id", "attachment id"] or ["document id"]
     */
    priv.fileNameToIds = function (file_name) {
      var separator_index = -1, split = file_name.split(".");
      split.slice(0, -1).forEach(function (file_name_part, index) {
        if (file_name_part.slice(-1) !== "_") {
          if (separator_index !== -1) {
            separator_index = new TypeError("Corrupted file name");
            separator_index.status = 24;
            throw separator_index;
          }
          separator_index = index;
244
        }
245
      });
246 247 248 249
      if (separator_index === -1) {
        return [priv.restoreName(priv.restoreName(
          file_name
        ).split("_.").join("."))];
250
      }
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
      return [
        priv.restoreName(priv.restoreName(
          split.slice(0, separator_index + 1).join(".")
        ).split("_.").join(".")),
        priv.restoreName(priv.restoreName(
          split.slice(separator_index + 1).join(".")
        ).split("_.").join("."))
      ];
    };

    /**
     * Removes the last character if it is a "/". "/a/b/c/" become "/a/b/c"
     * @method removeSlashIfLast
     * @param  {string} string The string to modify
     * @return {string} The modified string
     */
    priv.removeSlashIfLast = function (string) {
      if (string[string.length - 1] === "/") {
        return string.slice(0, -1);
270
      }
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
      return string;
    };

    /**
     * Modify an ajax object to add default values
     * @method makeAjaxObject
     * @param  {string} file_name The file name to add to the url
     * @param  {object} ajax_object The ajax object to override
     * @return {object} A new ajax object with default values
     */
    priv.makeAjaxObject = function (file_name, method, ajax_object) {
      ajax_object.type = method || ajax_object.type || "GET";
      ajax_object.url = priv.url + "/" + priv.secureName(file_name) +
        "?_=" + Date.now();
      ajax_object.async = ajax_object.async === false ? false : true;
      ajax_object.crossdomain =
        ajax_object.crossdomain === false ? false : true;
      ajax_object.headers = ajax_object.headers || {};
      ajax_object.headers.Authorization = ajax_object.headers.Authorization ||
        priv.encoded_login;
      return ajax_object;
    };

    /**
     * Runs all ajax requests for davStorage
     * @method ajax
     * @param  {string} doc_id The document id
     * @param  {string} attachment_id The attachment id, can be undefined
     * @param  {string} method The request method
     * @param  {object} ajax_object The request parameters (optional)
     */
    priv.ajax = function (doc_id, attachment_id, method, ajax_object) {
      var new_ajax_object = JSON.parse(JSON.stringify(ajax_object) || "{}");
      return $.ajax(priv.makeAjaxObject(
        priv.idsToFileName(doc_id || '', attachment_id),
        method,
        new_ajax_object
      ));//.always(then || function () {});
    };

    /**
     * Creates error objects for this storage
     * @method createError
     * @param {string} url url to clean up
     * @return {object} error The error object
     */
    priv.createError = function (status, message, reason) {
      var error = {
        "status": status,
        "message": message,
        "reason": reason
      };
      switch (status) {
      case 404:
        error.statusText = "Not found";
        break;
      case 405:
        error.statusText = "Method Not Allowed";
        break;
      case 409:
        error.statusText = "Conflicts";
        break;
      case 24:
        error.statusText = "Corrupted Document";
        break;
336
      }
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376
      error.error = error.statusText.toLowerCase().split(" ").join("_");
      return error;
    };

    /**
     * Converts ajax error object to a JIO error object
     * @method ajaxErrorToJioError
     * @param  {object} ajax_error_object The ajax error object
     * @param  {string} message The error message
     * @param  {string} reason The error reason
     * @return {object} The JIO error object
     */
    priv.ajaxErrorToJioError = function (ajax_error_object, message, reason) {
      var jio_error_object = {};
      jio_error_object.status = ajax_error_object.status;
      jio_error_object.statusText = ajax_error_object.statusText;
      jio_error_object.error =
        ajax_error_object.statusText.toLowerCase().split(" ").join("_");
      jio_error_object.message = message;
      jio_error_object.reason = reason;
      return jio_error_object;
    };

    /**
     * Function that create an object containing jQuery like callbacks
     * @method makeJQLikeCallback
     * @return {object} jQuery like callback methods
     */
    priv.makeJQLikeCallback = function () {
      var result = null, emptyFun = function () {}, jql = {
        "respond": function () {
          result = arguments;
        },
        "to_return": {
          "always": function (func) {
            if (result) {
              func.apply(func, result);
              jql.to_return.always = emptyFun;
            } else {
              jql.respond = func;
377
            }
378 379 380 381 382 383
            return jql.to_return;
          },
          "then": function (func) {
            if (result) {
              func(result[1]);
              jql.to_return.then = emptyFun;
384
            } else {
385 386 387
              jql.respond = function (err, response) {
                func(response);
              };
388
            }
389
            return jql.to_return;
390 391
          }
        }
392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426
      };
      return jql;
    };

    // DAV REQUESTS //
    /**
     * Retrieve a document file
     * @method dav.getDocument
     * @param  {string} doc_id The document id
     */
    dav.getDocument = function (doc_id) {
      var doc, jql = priv.makeJQLikeCallback(), error = null;
      priv.ajax(doc_id, undefined, "GET").always(function (one, state, three) {
        if (state !== "success") {
          error = priv.ajaxErrorToJioError(
            one,
            "Cannot retrieve document",
            "Unknown"
          );
          if (one.status === 404) {
            error.reason = "Not Found";
          }
          return jql.respond(error, undefined);
        }
        try {
          doc = JSON.parse(one);
        } catch (e) {
          return jql.respond(priv.createError(
            24,
            "Cannot parse document",
            "Document is corrupted"
          ), undefined);
        }
        // document health is good
        return jql.respond(undefined, doc);
427
      });
428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455
      return jql.to_return;
    };

    /**
     * Retrieve an attachment file
     * @method dav.getAttachment
     * @param  {string} doc_id The document id
     * @param  {string} attachment_id The attachment id
     */
    dav.getAttachment = function (doc_id, attachment_id) {
      var jql = priv.makeJQLikeCallback(), error = null;
      priv.ajax(
        doc_id,
        attachment_id,
        "GET"
      ).always(function (one, state, three) {
        if (state !== "success") {
          error = priv.ajaxErrorToJioError(
            one,
            "Cannot retrieve attachment",
            "Unknown"
          );
          if (one.status === 404) {
            error.reason = "Not Found";
          }
          return jql.respond(error, undefined);
        }
        return jql.respond(undefined, one);
456
      });
457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476
      return jql.to_return;
    };

    /**
     * Uploads a document file
     * @method dav.putDocument
     * @param  {object} doc The document object
     */
    dav.putDocument = function (doc) {
      var jql = priv.makeJQLikeCallback();
      priv.ajax(doc._id, undefined, "PUT", {
        "dataType": "text",
        "data": JSON.stringify(doc)
      }).always(function (one, state, three) {
        if (state !== "success") {
          return jql.respond(priv.ajaxErrorToJioError(
            one,
            "Cannot upload document",
            "Unknown"
          ), undefined);
477
        }
478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501
        jql.respond(undefined, {"ok": true, "id": doc._id});
      });
      return jql.to_return;
    };

    /**
     * Uploads an attachment file
     * @method dav.putAttachment
     * @param  {string} doc_id The document id
     * @param  {string} attachment_id The attachment id
     * @param  {string} data The attachment data
     */
    dav.putAttachment = function (doc_id, attachment_id, data) {
      var jql = priv.makeJQLikeCallback();
      priv.ajax(doc_id, attachment_id, "PUT", {
        "dataType": "text",
        "data": data
      }).always(function (one, state, three) {
        if (state !== "success") {
          return jql.respond(priv.ajaxErrorToJioError(
            one,
            "Cannot upload attachment",
            "Unknown"
          ), undefined);
502
        }
503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533
        return jql.respond(undefined, {
          "ok": true,
          "id": doc_id,
          "attachment": attachment_id
        });
      });
      return jql.to_return;
    };

    /**
     * Deletes a document file
     * @method dav.removeDocument
     * @param  {string} doc_id The document id
     */
    dav.removeDocument = function (doc_id) {
      var jql = priv.makeJQLikeCallback(), error = null;
      priv.ajax(
        doc_id,
        undefined,
        "DELETE"
      ).always(function (one, state, three) {
        if (state !== "success") {
          error = priv.ajaxErrorToJioError(
            one,
            "Cannot delete document",
            "Unknown"
          );
          if (one.status === 404) {
            error.reason = "Not Found";
          }
          return jql.respond(error, undefined);
534
        }
535 536 537 538 539 540 541 542 543 544 545 546 547 548
        jql.respond(undefined, {"ok": true, "id": doc_id});
      });
      return jql.to_return;
    };

    /**
     * Deletes an attachment file
     * @method dav.removeAttachment
     * @param  {string} doc_id The document id
     * @param  {string} attachment_id The attachment id
     */
    dav.removeAttachment = function (doc_id, attachment_id) {
      var jql = priv.makeJQLikeCallback(), error = null;
      priv.ajax(
549 550
        doc_id,
        attachment_id,
551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664
        "DELETE"
      ).always(function (one, state, three) {
        if (state !== "success") {
          error = priv.ajaxErrorToJioError(
            one,
            "Cannot delete attachment",
            "Unknown"
          );
          if (one.status === 404) {
            error.reason = "Not Found";
          }
          return jql.respond(error, undefined);
        }
        jql.respond(undefined, {"ok": true, "id": doc_id});
      });
      return jql.to_return;
    };

    /**
     * Get a list of document file
     * @method dav.allDocs
     */
    dav.allDocs = function () {
      var jql = priv.makeJQLikeCallback(), rows = [];
      priv.ajax(undefined, undefined, "PROPFIND", {
        "dataType": "xml",
        "headers": {"Depth": 1}
      }).always(function (one, state, three) {
        var response, len;
        if (state !== "success") {
          return jql.respond(priv.ajaxErrorToJioError(
            one,
            "Cannot get the document list",
            "Unknown"
          ), undefined);
        }
        response = $(one).find("D\\:response, response");
        len = response.length;
        if (len === 1) {
          return jql.respond({"total_rows": 0, "rows": []});
        }
        response.each(function (i, data) {
          var row;
          if (i > 0) { // exclude parent folder
            row = {
              "id": "",
              "key": "",
              "value": {}
            };
            $(data).find("D\\:href, href").each(function () {
              row.id = $(this).text().split('/').slice(-1)[0];
              try {
                row.id = priv.fileNameToIds(row.id);
              } catch (e) {
                if (e.name === "TypeError" && e.status === 24) {
                  return;
                }
                throw e;
              }
              if (row.id.length !== 1) {
                row = undefined;
              } else {
                row.id = row.id[0];
                row.key = row.id;
              }
            });
            if (row !== undefined) {
              rows.push(row);
            }
          }
        });
        jql.respond(undefined, {
          "total_rows": rows.length,
          "rows": rows
        });
      });
      return jql.to_return;
    };

    // JIO COMMANDS //

    // wedDav methods rfc4918 (short summary)
    // COPY     Reproduces single resources (files) and collections (directory
    //          trees). Will overwrite files (if specified by request) but will
    //          respond 209 (Conflict) if it would overwrite a tree
    // DELETE   deletes files and directory trees
    // GET      just the vanilla HTTP/1.1 behaviour
    // HEAD     ditto
    // LOCK     locks a resources
    // MKCOL    creates a directory
    // MOVE     Moves (rename or copy) a file or a directory tree. Will
    //          'overwrite' files (if specified by the request) but will respond
    //          209 (Conflict) if it would overwrite a tree.
    // OPTIONS  If WebDAV is enabled and available for the path this reports the
    //          WebDAV extension methods
    // PROPFIND Retrieves the requested file characteristics, DAV lock status
    //          and 'dead' properties for individual files, a directory and its
    //          child files, or a directory tree
    // PROPPATCHset and remove 'dead' meta-data properties
    // PUT      Update or create resource or collections
    // UNLOCK   unlocks a resource

    // Notes: all Ajax requests should be CORS (cross-domain)
    // adding custom headers triggers preflight OPTIONS request!
    // http://remysharp.com/2011/04/21/getting-cors-working/

    /**
     * Creates a new document
     * @method  post
     * @param  {object} command The JIO command
     */
    that.post = function (command) {
      var doc_id = command.getDocId() || priv.generateUuid();
      dav.getDocument(doc_id).always(function (err, response) {
665
        if (err) {
666 667 668 669 670 671 672 673 674 675 676 677 678 679 680
          if (err.status === 404) {
            // the document does not already exist
            // updating document
            var doc = command.cloneDoc();
            doc._id = doc_id;
            return dav.putDocument(doc).always(function (err, response) {
              if (err) {
                return that.retry(err);
              }
              return that.success(response);
            });
          }
          if (err.status === 24) {
            return that.error(err);
          }
681 682
          // an error occured
          return that.retry(err);
683
        }
684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739
        // the document already exists
        return that.error(priv.createError(
          405,
          "Cannot create document",
          "Document already exists"
        ));
      });
    };

    /**
     * Creates or updates a document
     * @method  put
     * @param  {object} command The JIO command
     */
    that.put = function (command) {
      dav.putDocument(command.cloneDoc()).always(function (err, response) {
        if (err) {
          // an error occured
          return that.retry(err);
        }
        // document updated
        return that.success(response);
      });
    };

    /**
     * Add an attachment to a document
     * @method  putAttachment
     * @param  {object} command The JIO command
     */
    that.putAttachment = function (command) {
      var doc = null, doc_id = command.getDocId(), attachment_id, tmp;
      attachment_id = command.getAttachmentId();
      dav.getDocument(doc_id).always(function (err, response) {
        if (err) {
          // document not found or error
          tmp = that.retry;
          if (err.status === 404 ||
              err.status === 24) {
            tmp = that.error;
          }
          return tmp(err);
        }
        doc = response;
        doc._attachments = doc._attachments || {};
        doc._attachments[attachment_id] = {
          "length": command.getAttachmentLength(),
          "digest": "md5-" + command.md5SumAttachmentData(),
          "content_type": command.getAttachmentMimeType()
        };
        // put the attachment
        dav.putAttachment(
          doc_id,
          attachment_id,
          command.getAttachmentData()
        ).always(function (err, response) {
740
          if (err) {
741
            // an error occured
742
            return that.retry(err);
743
          }
744 745 746 747 748 749 750 751
          // update the document
          dav.putDocument(doc).always(function (err, response) {
            if (err) {
              return that.retry(err);
            }
            response.attachment = attachment_id;
            return that.success(response);
          });
752
        });
753 754
      });
    };
755 756 757 758 759 760 761 762 763 764 765 766 767

    /**
     * Get a document
     * @method  get
     * @param  {object} command The JIO command
     */
    that.get = function (command) {
      dav.getDocument(command.getDocId()).always(function (err, response) {
        if (err) {
          if (err.status === 404 ||
              err.status === 24) {
            return that.error(err);
          }
768 769
          return that.retry(err);
        }
770 771 772 773 774 775 776 777 778 779 780 781 782 783
        return that.success(response);
      });
    };

    /**
     * Get an attachment
     * @method  getAttachment
     * @param  {object} command The JIO command
     */
    that.getAttachment = function (command) {
      dav.getAttachment(
        command.getDocId(),
        command.getAttachmentId()
      ).always(function (err, response) {
784 785 786
        if (err) {
          if (err.status === 404) {
            return that.error(err);
787
          }
788
          return that.retry(err);
789
        }
790
        return that.success(response);
791
      });
792 793 794 795 796 797 798 799 800 801 802 803 804
    };

    /**
     * Remove a document
     * @method remove
     * @param  {object} command The JIO command
     */
    that.remove = function (command) {
      var doc_id = command.getDocId(), count = 0, end;
      end = function () {
        count -= 1;
        if (count === 0) {
          that.success({"ok": true, "id": doc_id});
805
        }
806 807 808 809 810 811 812 813 814 815 816
      };
      dav.getDocument(doc_id).always(function (err, response) {
        var attachment_id = null;
        if (err) {
          if (err.status === 404) {
            return that.error(err);
          }
          if (err.status !== 24) { // 24 -> corrupted document
            return that.retry(err);
          }
          response = {};
817
        }
818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835
        count += 2;
        dav.removeDocument(doc_id).always(function (err, response) {
          if (err) {
            if (err.status === 404) {
              return that.error(err);
            }
            return that.retry(err);
          }
          return end();
        });
        for (attachment_id in response._attachments) {
          if (response._attachments.hasOwnProperty(attachment_id)) {
            count += 1;
            dav.removeAttachment(
              doc_id,
              attachment_id
            ).always(end);
          }
836
        }
837 838 839 840 841 842 843 844 845 846 847 848 849 850
        end();
      });
    };

    /**
     * Remove an attachment
     * @method removeAttachment
     * @param  {object} command The JIO command
     */
    that.removeAttachment = function (command) {
      var doc_id = command.getDocId(), doc, attachment_id;
      attachment_id = command.getAttachmentId();
      dav.getDocument(doc_id).always(function (err, response) {
        var still_has_attachments;
851
        if (err) {
852 853 854 855
          if (err.status === 404 ||
              err.status === 24) {
            return that.error(err);
          }
856 857
          return that.retry(err);
        }
858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890
        doc = response;
        if (typeof (doc._attachments || {})[attachment_id] !== "object") {
          return that.error(priv.createError(
            404,
            "Cannot remove attachment",
            "Not Found"
          ));
        }
        delete doc._attachments[attachment_id];
        // check if there is still attachments
        for (still_has_attachments in doc._attachments) {
          if (doc._attachments.hasOwnProperty(still_has_attachments)) {
            break;
          }
        }
        if (still_has_attachments === undefined) {
          delete doc._attachments;
        }
        doc._id = doc_id;
        dav.putDocument(doc).always(function (err, response) {
          if (err) {
            return that.retry(err);
          }
          dav.removeAttachment(
            doc_id,
            attachment_id
          ).always(function (err, response) {
            that.success({
              "ok": true,
              "id": doc_id,
              "attachment": attachment_id
            });
          });
891 892
        });
      });
Sven Franck's avatar
Sven Franck committed
893
    };
894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914

    /**
     * Gets a document list from a distant dav storage
     * Options:
     * - {boolean} include_docs Also retrieve the actual document content.
     * @method allDocs
     * @param  {object} command The JIO command
     */
    that.allDocs = function (command) {
      var count = 0, end, rows;
      end = function () {
        count -= 1;
        if (count === 0) {
          that.success(rows);
        }
      };
      dav.allDocs().always(function (err, response) {
        if (err) {
          return that.retry(err);
        }
        if (command.getOption("include_docs") === true) {
915
          count += 1;
916 917 918 919 920 921 922 923 924 925 926
          rows = response;
          rows.rows.forEach(function (row) {
            count += 1;
            dav.getDocument(
              row.id
            ).always(function (err, response) {
              if (err) {
                if (err.status === 404 || err.status === 24) {
                  return that.error(err);
                }
                return that.retry(err);
Sven Franck's avatar
Sven Franck committed
927
              }
928 929 930
              row.doc = response;
              end();
            });
Sven Franck's avatar
Sven Franck committed
931
          });
932 933 934 935 936 937 938 939 940 941
          end();
        } else {
          that.success(response);
        }
      });
    };

    priv.__init__(spec);
    return that;
  });
Sven Franck's avatar
Sven Franck committed
942

943
}));