Code coverage report for node-mhtml/lib/part.js

Statements: 96.67% (29 / 30)      Branches: 85% (17 / 20)      Functions: 100% (6 / 6)      Lines: 96.67% (29 / 30)     

All files » node-mhtml/lib/ » part.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 791   1 1   1   1 62 62 62     1       468       468   468 167     301 292           26             54 50       4     4       54         49     49         20 11 11 11     20     49      
'use strict';
 
var mimelib = require('mimelib');
var cheerio = require('cheerio');
 
module.exports = Part;
 
function Part() {
  this._meta = {};
  this.basePath = '';
  this.content = '';
}
 
Part.prototype = {
 
  meta: function (key, value) {
 
    Iif (!key && !value) {
      return this._meta;
    }
 
    key = key.toLowerCase();
 
    if (value) {
      return this._meta[key] = value;
    }
 
    if (this._meta.hasOwnProperty(key)) {
      return this._meta[key];
    }
  },
 
  decoder: {
    base64: function (encoded) {
      return new Buffer(encoded, 'base64');
    },
    'quoted-printable': mimelib.decodeQuotedPrintable
  },
 
  filePath: function () {
 
    if (this.meta('content-location')) {
      return this.meta('content-location').replace(/\\/g, '/').replace(this.basePath, '');
    }
 
    // TODO: use content-id if present?
    var ext = (mimelib.contentTypesReversed.hasOwnProperty(this.meta('content-type')))
      ? '.' + mimelib.contentTypesReversed[this.meta('content-type')] : '';
 
    return Math.floor(Math.random() * 5000000000) + ext;
  },
 
  isLocal: function () {
    return Boolean(!this.meta('content-location') || this.meta('content-location').match(/^file:/));
  },
 
  decoded: function (cb) {
 
    var $, decoded = this.decoder[this.meta('content-transfer-encoding')](this.content);
 
    // convert all file:// links to be relative to the exported folder
    if (typeof decoded === 'string' && this.basePath) {
 
      // strip out any <base> tags as they are generally useless once the mhtml
      // is exported.
      // FIXME: this code is mostly for Excel files and is quite hacky, improve
      if (this.meta('content-type') === 'text/html') {
        $ = cheerio.load(decoded);
        $('head').find('base').remove();
        decoded = $.html();
      }
 
      decoded = decoded.replace(new RegExp(this.basePath, 'g'), '');
    }
 
    cb(null, decoded);
  }
};