'use strict';

var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');

var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);

var _createClass2 = require('babel-runtime/helpers/createClass');

var _createClass3 = _interopRequireDefault(_createClass2);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

/**
 * Created by godsong on 16/6/30.
 */

var http = require('http');
var crypto = require('crypto');
var _memoryFileMap = {};

var MemoryFile = function () {
    (0, _createClass3.default)(MemoryFile, null, [{
        key: 'get',
        value: function get(name) {
            return _memoryFileMap[name];
        }
    }, {
        key: 'dump',
        value: function dump() {
            return Object.keys(_memoryFileMap);
        }
    }]);

    function MemoryFile(fileName, content) {
        (0, _classCallCheck3.default)(this, MemoryFile);

        if (fileName.indexOf('http://') == 0) {
            this.name = fileName.slice(7);
        } else this.name = fileName;
        var md5 = crypto.createHash('md5');
        md5.update(content);
        var md5Str = md5.digest('hex');
        var key = this.name.split('?')[0] + '|' + md5Str;
        if (_memoryFileMap[this.name]) {
            return _memoryFileMap[this.name];
        } else if (_memoryFileMap[key]) {
            return _memoryFileMap[key];
        } else this.content = content;
        this.md5 = md5Str;
        _memoryFileMap[this.name] = this;
        _memoryFileMap[key] = this;
    }

    (0, _createClass3.default)(MemoryFile, [{
        key: 'getContent',
        value: function getContent() {
            return this.content;
        }
    }, {
        key: 'getUrl',
        value: function getUrl() {
            return '/source/' + this.name;
        }
    }]);
    return MemoryFile;
}();

module.exports = MemoryFile;