Code coverage report for lib/deps/docs/preprocessAttachments.js

Statements: 19.67% (12 / 61)      Branches: 0% (0 / 30)      Functions: 0% (0 / 9)      Lines: 19.67% (12 / 61)      Ignored: none     

All files » lib/deps/docs/ » preprocessAttachments.js
1 2 3 4 5 6 7 8 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 50 51 52 53 54 55 56 57 58 59 60 61 62 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 100 101 102 103 104 105 106 107    1 1 1 1 1 1   1               1                   1                                                                                                     1                               1                       1
'use strict';
 
var base64 = require('../binary/base64');
var arrayBuffToBinString = require('../binary/arrayBufferToBinaryString');
var readAsArrayBuffer = require('../binary/readAsArrayBuffer');
var binStringToBlobOrBuffer = require('../binary/binaryStringToBlobOrBuffer');
var errors = require('../errors');
var md5 = require('../md5');
 
function preprocessAttachments(docInfos, blobType, callback) {
 
  if (!docInfos.length) {
    return callback();
  }
 
  var docv = 0;
 
  function parseBase64(data) {
    try {
      return base64.atob(data);
    } catch (e) {
      var err = errors.error(errors.BAD_ARG,
        'Attachments need to be base64 encoded');
      return {error: err};
    }
  }
 
  function preprocessAttachment(att, callback) {
    if (att.stub) {
      return callback();
    }
    if (typeof att.data === 'string') {
      // input is a base64 string
 
      var asBinary = parseBase64(att.data);
      if (asBinary.error) {
        return callback(asBinary.error);
      }
 
      att.length = asBinary.length;
      if (blobType === 'blob') {
        att.data = binStringToBlobOrBuffer(asBinary, att.content_type);
      } else if (blobType === 'base64') {
        att.data = base64.btoa(asBinary);
      } else { // binary
        att.data = asBinary;
      }
      md5(asBinary).then(function (result) {
        att.digest = 'md5-' + result;
        callback();
      });
    } else { // input is a blob
      readAsArrayBuffer(att.data, function (buff) {
        if (blobType === 'binary') {
          att.data = arrayBuffToBinString(buff);
        } else if (blobType === 'base64') {
          att.data = base64.btoa(arrayBuffToBinString(buff));
        }
        md5(buff).then(function (result) {
          att.digest = 'md5-' + result;
          att.length = buff.byteLength;
          callback();
        });
      });
    }
  }
 
  var overallErr;
 
  docInfos.forEach(function (docInfo) {
    var attachments = docInfo.data && docInfo.data._attachments ?
      Object.keys(docInfo.data._attachments) : [];
    var recv = 0;
 
    if (!attachments.length) {
      return done();
    }
 
    function processedAttachment(err) {
      overallErr = err;
      recv++;
      if (recv === attachments.length) {
        done();
      }
    }
 
    for (var key in docInfo.data._attachments) {
      if (docInfo.data._attachments.hasOwnProperty(key)) {
        preprocessAttachment(docInfo.data._attachments[key],
          processedAttachment);
      }
    }
  });
 
  function done() {
    docv++;
    if (docInfos.length === docv) {
      if (overallErr) {
        callback(overallErr);
      } else {
        callback();
      }
    }
  }
}
 
module.exports = preprocessAttachments;