feeles-ide
Version:
The hackable and serializable IDE to make learning material
157 lines (137 loc) • 4.65 kB
JavaScript
import _Promise from 'babel-runtime/core-js/promise';
import _regeneratorRuntime from 'babel-runtime/regenerator';
import _JSON$stringify from 'babel-runtime/core-js/json/stringify';
import _asyncToGenerator from 'babel-runtime/helpers/asyncToGenerator';
import _Object$getPrototypeOf from 'babel-runtime/core-js/object/get-prototype-of';
import _extends from 'babel-runtime/helpers/extends';
import _classCallCheck from 'babel-runtime/helpers/classCallCheck';
import _createClass from 'babel-runtime/helpers/createClass';
import _possibleConstructorReturn from 'babel-runtime/helpers/possibleConstructorReturn';
import _inherits from 'babel-runtime/helpers/inherits';
import md5 from 'md5';
import _File from './_File';
import { encode, decode } from './sanitizeHTML';
var BinaryFile = function (_File2) {
_inherits(BinaryFile, _File2);
function BinaryFile(props) {
_classCallCheck(this, BinaryFile);
if (props.composed) {
var bin = atob(decode(props.composed));
var byteArray = new Uint8Array(bin.length);
for (var i = bin.length - 1; i >= 0; i--) {
byteArray[i] = bin.charCodeAt(i);
}
var blob = new Blob([byteArray.buffer], { type: props.type });
var hash = md5(byteArray);
props = _extends({}, props, { blob: blob, hash: hash });
}
if (props.blob && !props.blobURL) {
var blobURL = URL.createObjectURL(props.blob);
props = _extends({}, props, { blobURL: blobURL });
}
return _possibleConstructorReturn(this, (BinaryFile.__proto__ || _Object$getPrototypeOf(BinaryFile)).call(this, props));
}
_createClass(BinaryFile, [{
key: 'set',
value: function set(change) {
if (!change.blob && this.hash) {
change.hash = this.hash;
}
var seed = _extends({}, this.serialize(), change);
seed.key = this.key;
seed.lastModified = Date.now();
return new BinaryFile(seed);
}
}, {
key: 'compose',
value: function () {
var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
var serialized, credits, dataURL, base64;
return _regeneratorRuntime.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
serialized = this.serialize();
delete serialized.blob;
if (this.sign && this.sign === this.credit) {
credits = this.credits.concat(_extends({}, this.sign, {
timestamp: Date.now(),
hash: this.hash
}));
serialized.credits = _JSON$stringify(credits);
} else {
serialized.credits = _JSON$stringify(this.credits);
}
_context.next = 5;
return this.toDataURL();
case 5:
dataURL = _context.sent;
base64 = dataURL.substr(dataURL.indexOf(',') + 1);
serialized.composed = encode(base64);
return _context.abrupt('return', serialized);
case 9:
case 'end':
return _context.stop();
}
}
}, _callee, this);
}));
function compose() {
return _ref.apply(this, arguments);
}
return compose;
}()
/**
* @param file File|Blob
* @return Promise gives BinaryFile
*/
}, {
key: 'blob',
get: function get() {
return this.props.blob;
}
}, {
key: 'blobURL',
get: function get() {
return this.props.blobURL;
}
}, {
key: 'hash',
get: function get() {
return this.props.hash;
}
}], [{
key: 'load',
value: function load(file) {
return new _Promise(function (resolve) {
// get hash of TypedArray from binary
var reader = new FileReader();
reader.onload = function (e) {
var typedArray = new Uint8Array(e.target.result);
var hash = md5(typedArray);
resolve(hash);
};
reader.readAsArrayBuffer(file);
}).then(function (hash) {
return new BinaryFile({
type: file.type,
name: file.name || BinaryFile.defaultProps.name,
blob: file,
hash: hash,
lastModified: file.lastModified
});
});
}
}]);
return BinaryFile;
}(_File);
BinaryFile.defaultProps = {
name: '.BinaryFile',
blob: null,
sign: null
};
BinaryFile.defaultOptions = {
isTrashed: false
};
BinaryFile.visible = _File.visible.concat('blob');
export default BinaryFile;