var Class = require('../utils/Class'); var CONST = require('./const'); var Events = require('./events'); var GetFastValue = require('../utils/object/GetFastValue'); var GetURL = require('./GetURL'); var MergeXHRSettings = require('./MergeXHRSettings'); var XHRLoader = require('./XHRLoader'); var XHRSettings = require('./XHRSettings'); var File = new Class({ initialize: function File(loader, fileConfig){ this.loader = loader; this.cache = GetFastValue(fileConfig, 'cache', false ); this.type = GetFastValue(fileConfig, 'type', false ); this.key = GetFastValue(fileConfig, 'key', false ); var loadKey = this.key; if (loader.prefix && loader.prefix !== '') { this.key = loader.prefix + loadKey; } if (!this.type || !this.key) { throw new Error('Invalid Loader.' + this.type + ' key') } var url = GetFastValue(fileConfig, 'url'); if (url === undefined) { url = loader.path + loadKey + '.' + GetFastValue(fileConfig, 'extension', ''); } else if (typeof url === 'string' && !url.match(/^(?:blob:|data:|http:\/\/|https:\/\/|\/\/)/)) { url = loader.path + url; } _AN_Write_url('url', this, false , url); _AN_Write_src('src', this, false , ''); this.xhrSettings = XHRSettings(GetFastValue(fileConfig, 'responseType', undefined)); if (GetFastValue(fileConfig, 'xhrSettings', false )) { this.xhrSettings = MergeXHRSettings(this.xhrSettings, GetFastValue(fileConfig, 'xhrSettings', { } )); } this.xhrLoader = null ; this.state = (typeof (_AN_Read_url('url', this)) === 'function')? CONST.FILE_POPULATED: CONST.FILE_PENDING; this.bytesTotal = 0; this.bytesLoaded = -1; this.percentComplete = -1; this.crossOrigin = undefined; this.data = undefined; this.config = GetFastValue(fileConfig, 'config', { } ); this.multiFile; this.linkFile; } , setLink: function (fileB){ this.linkFile = fileB; fileB.linkFile = this; } , resetXHR: function (){ if (this.xhrLoader) { this.xhrLoader.onload = undefined; this.xhrLoader.onerror = undefined; this.xhrLoader.onprogress = undefined; } } , load: function (){ if (this.state === CONST.FILE_POPULATED) { this.loader.nextFile(this, true ); } else { this.state = CONST.FILE_LOADING; _AN_Write_src('src', this, false , GetURL(this, _AN_Read_baseurl('baseURL', this.loader))); if (_AN_Read_src('src', this).indexOf('data:') === 0) { console.warn('Local data URIs are not supported: ' + this.key); } else { this.xhrLoader = XHRLoader(this, this.loader.xhr); } } } , onLoad: function (xhr, event){ var localFileOk = ((xhr.responseURL && xhr.responseURL.indexOf('file://') === 0 && _AN_Read_target('target', event).status === 0)); var success = !(_AN_Read_target('target', event) && _AN_Read_target('target', event).status !== 200) || localFileOk; if (xhr.readyState === 4 && xhr.status >= 400 && xhr.status <= 599) { success = false ; } this.state = CONST.FILE_LOADED; this.resetXHR(); this.loader.nextFile(this, success); } , onError: function (){ this.resetXHR(); this.loader.nextFile(this, false ); } , onProgress: function (event){ if (event.lengthComputable) { this.bytesLoaded = event.loaded; this.bytesTotal = event.total; this.percentComplete = Math.min((this.bytesLoaded / this.bytesTotal), 1); this.loader.emit(Events.FILE_PROGRESS, this, this.percentComplete); } } , onProcess: function (){ this.state = CONST.FILE_PROCESSING; this.onProcessComplete(); } , onProcessComplete: function (){ this.state = CONST.FILE_COMPLETE; if (this.multiFile) { this.multiFile.onFileComplete(this); } this.loader.fileProcessComplete(this); } , onProcessError: function (){ this.state = CONST.FILE_ERRORED; if (this.multiFile) { this.multiFile.onFileFailed(this); } this.loader.fileProcessComplete(this); } , hasCacheConflict: function (){ return (this.cache && this.cache.exists(this.key)); } , addToCache: function (){ if (this.cache) { this.cache.add(this.key, this.data); } this.pendingDestroy(); } , pendingDestroy: function (data){ if (data === undefined) { data = this.data; } var key = this.key; var type = this.type; this.loader.emit(Events.FILE_COMPLETE, key, type, data); this.loader.emit(Events.FILE_KEY_COMPLETE + type + '-' + key, key, type, data); this.loader.flagForRemoval(this); } , destroy: function (){ this.loader = null ; this.cache = null ; this.xhrSettings = null ; this.multiFile = null ; this.linkFile = null ; this.data = null ; } } ); File.createObjectURL = function (image, blob, defaultType){ if (typeof URL === 'function') { _AN_Write_src('src', image, false , URL.createObjectURL(blob)); } else { var reader = new FileReader(); reader.onload = function (){ image.removeAttribute('crossOrigin'); _AN_Write_src('src', image, false , 'data:' + (blob.type || defaultType) + ';base64,' + reader.result.split(',')[1]); } ; reader.onerror = image.onerror; reader.readAsDataURL(blob); } } ; File.revokeObjectURL = function (image){ if (typeof URL === 'function') { URL.revokeObjectURL(_AN_Read_src('src', image)); } } ; module.exports = File;