all files / dist/satellites/ staticFile.js

42.45% Statements 45/106
20% Branches 10/50
43.48% Functions 10/23
33.71% Lines 30/89
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 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 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 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 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 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                          18×                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
'use strict';
 
Object.defineProperty(exports, "__esModule", {
  value: true
});
 
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; Eif ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { Eif (protoProps) defineProperties(Constructor.prototype, protoProps); Iif (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
 
var _fs = require('fs');
 
var _fs2 = _interopRequireDefault(_fs);
 
var _path = require('path');
 
var _path2 = _interopRequireDefault(_path);
 
var _mime = require('mime');
 
var _mime2 = _interopRequireDefault(_mime);
 
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
 
function _classCallCheck(instance, Constructor) { Iif (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
 
/**
 * Class to manage the static files.
 */
 
var StaticFile = function () {
 
  /**
   * Create a new instance of this class.
   *
   * @param api API object reference.
   */
 
 
  /**
   * API object reference.
   *
   * @type {null}
   */
 
  function StaticFile(api) {
    _classCallCheck(this, StaticFile);
 
    this.api = null;
    this.searchLocations = [];
 
    var self = this;
 
    // save API reference object
    self.api = api;
  }
 
  /**
   * Get the public path.
   *
   * @param connection  Client connection object.
   * @param counter
   * @returns {*}
   */
 
 
  /**
   * Search locations.
   *
   * @type {Array}
   */
 
 
  _createClass(StaticFile, [{
    key: 'searchPath',
    value: function searchPath(connection) {
      var counter = arguments.length <= 1 || arguments[1] === undefined ? 0 : arguments[1];
 
      var self = this;
 
      if (self.searchLocations.length === 0 || counter >= self.searchLocations.length) {
        return null;
      } else {
        return self.searchLocations[counter];
      }
    }
 
    /**
     * Get the content of a file by the 'connection.params.file' var.
     *
     * @param connection  Client connection object.
     * @param callback    Callback function.
     * @param counter
     */
 
  }, {
    key: 'get',
    value: function get(connection, callback) {
      var counter = arguments.length <= 2 || arguments[2] === undefined ? 0 : arguments[2];
 
      var self = this;
 
      if (!connection.params.file || !self.searchPath(connection, counter)) {
        self.sendFileNotFound(connection, self.api.config.errors.fileNotProvided(connection), callback);
      } else {
        var file = null;
 
        if (!_path2.default.isAbsolute(connection.params.file)) {
          file = _path2.default.normalize(self.searchPath(connection, counter) + '/' + connection.params.file);
        } else {
          file = connection.params.file;
        }
 
        if (file.indexOf(_path2.default.normalize(self.searchPath(connection, counter))) !== 0) {
          self.get(connection, callback, counter + 1);
        } else {
          self.checkExistence(file, function (exists, truePath) {
            if (exists) {
              self.sendFile(truePath, connection, callback);
            } else {
              self.get(connection, callback, counter + 1);
            }
          });
        }
      }
    }
 
    /**
     * Send a file to the client.
     *
     * @param file
     * @param connection
     * @param callback
     */
 
  }, {
    key: 'sendFile',
    value: function sendFile(file, connection, callback) {
      var self = this;
      var lastModified = void 0;
 
      // get file information
      _fs2.default.stat(file, function (err, stats) {
        // check if is an error
        if (err) {
          // if we can't read the file respond with an error
          self.sendFileNotFound(connection, self.api.config.errors.fileReadError(String(err)), callback);
        } else {
          (function () {
            var mime = _mime2.default.lookup(file);
            var length = stats.size;
            var fileStream = _fs2.default.createReadStream(file);
            var start = new Date().getTime();
 
            lastModified = stats.mtime;
 
            // add a listener to the 'close' event
            fileStream.on('close', function () {
              var duration = new Date().getTime() - start;
              self.logRequest(file, connection, length, duration, true);
            });
 
            // add a listener to the 'error' event
            fileStream.on('error', function (err) {
              self.api.log(err);
            });
 
            // execute the callback
            callback(connection, null, fileStream, mime, length, lastModified);
          })();
        }
      });
    }
 
    /**
     * Send a file not found error to the client.
     *
     * @param connection    Client connection object.
     * @param errorMessage  Error message to send.
     * @param callback      Callback function.
     */
 
  }, {
    key: 'sendFileNotFound',
    value: function sendFileNotFound(connection, errorMessage, callback) {
      var self = this;
 
      // add error message
      connection.error = new Error(errorMessage);
 
      // load 404 error
      self.logRequest('{404: not found}', connection, null, null, false);
 
      // execute the callback function
      callback(connection, self.api.config.errors.fileNotFound(), null, 'text/html', self.api.config.errors.fileNotFound().length);
    }
 
    /**
     * Check the existence of a file.
     *
     * @param file
     * @param callback
     */
 
  }, {
    key: 'checkExistence',
    value: function checkExistence(file, callback) {
      var self = this;
 
      _fs2.default.stat(file, function (error, stats) {
        // if exists an error execute the callback
        // function and return
        if (error) {
          callback(false, file);
          return;
        }
 
        if (stats.isDirectory()) {
          var indexPath = file + '/' + self.api.config.general.directoryFileType;
          self.checkExistence(indexPath, callback);
        } else if (stats.isSymbolicLink()) {
          _fs2.default.readlink(file, function (error, truePath) {
            if (error) {
              callback(false, file);
            } else {
              truePath = _path2.default.normalize(truePath);
              self.checkExistence(truePath, callback);
            }
          });
        } else if (stats.isFile()) {
          callback(true, file);
        } else {
          callback(false, file);
        }
      });
    }
 
    /**
     * Log file requests.
     *
     * @param file
     * @param connection
     * @param length
     * @param duration
     * @param success
     */
 
  }, {
    key: 'logRequest',
    value: function logRequest(file, connection, length, duration, success) {
      var self = this;
 
      self.api.log('[ file @ ' + connection.type + ']', 'debug', {
        to: connection.remoteIP,
        file: file,
        size: length,
        duration: duration,
        success: success
      });
    }
  }]);
 
  return StaticFile;
}();
 
var _class = function () {
  function _class() {
    _classCallCheck(this, _class);
 
    this.loadPriority = 510;
  }
 
  /**
   * Satellite load priority.
   *
   * @type {number}
   */
 
 
  _createClass(_class, [{
    key: 'load',
 
 
    /**
     * Satellite load function.
     *
     * @param api   API reference object.
     * @param next  Callback function.
     */
    value: function load(api, next) {
      // put static file methods available on the API object
      api.staticFile = new StaticFile(api);
 
      // load in the explicit public paths first
      Eif (api.config.general.paths !== undefined) {
        api.staticFile.searchLocations.push(_path2.default.normalize(api.config.general.paths.public));
      }
 
      // finish satellite loading
      next();
    }
  }]);
 
  return _class;
}();
 
exports.default = _class;
//# sourceMappingURL=staticFile.js.map