Catch errors from createObjectURL

This commit is contained in:
Cássio Zen 2017-01-11 17:40:26 -02:00
parent e2ddaa7f2a
commit 19e5c61f05

View File

@ -8,10 +8,10 @@ export const setStore = (storeObj) => {
store = storeObj; store = storeObj;
}; };
export default function AssetProxy(value, file, uploaded = false) { export default function AssetProxy(value, fileObj, uploaded = false) {
const config = store.getState().config; const config = store.getState().config;
this.value = value; this.value = value;
this.file = file; this.fileObj = fileObj;
this.uploaded = uploaded; this.uploaded = uploaded;
this.sha = null; this.sha = null;
this.path = config.media_folder && !uploaded ? `${ config.get('media_folder') }/${ value }` : value; this.path = config.media_folder && !uploaded ? `${ config.get('media_folder') }/${ value }` : value;
@ -19,7 +19,12 @@ export default function AssetProxy(value, file, uploaded = false) {
} }
AssetProxy.prototype.toString = function () { AssetProxy.prototype.toString = function () {
return this.uploaded ? this.public_path : window.URL.createObjectURL(this.file, { oneTimeOnly: true }); if (this.uploaded) return this.public_path;
try {
return window.URL.createObjectURL(this.fileObj);
} catch (error) {
return null;
}
}; };
AssetProxy.prototype.toBase64 = function () { AssetProxy.prototype.toBase64 = function () {
@ -30,24 +35,24 @@ AssetProxy.prototype.toBase64 = function () {
resolve(binaryString.split('base64,')[1]); resolve(binaryString.split('base64,')[1]);
}; };
fr.readAsDataURL(this.file); fr.readAsDataURL(this.fileObj);
}); });
}; };
export function createAssetProxy(value, file, uploaded = false, privateUpload = false) { export function createAssetProxy(value, fileObj, uploaded = false, privateUpload = false) {
const state = store.getState(); const state = store.getState();
const integration = selectIntegration(state, null, 'assetStore'); const integration = selectIntegration(state, null, 'assetStore');
if (integration && !uploaded) { if (integration && !uploaded) {
const provider = integration && getIntegrationProvider(state.integrations, currentBackend(state.config).getToken, integration); const provider = integration && getIntegrationProvider(state.integrations, currentBackend(state.config).getToken, integration);
return provider.upload(file, privateUpload).then( return provider.upload(fileObj, privateUpload).then(
response => ( response => (
new AssetProxy(response.assetURL.replace(/^(https?):/, ''), null, true) new AssetProxy(response.assetURL.replace(/^(https?):/, ''), null, true)
), ),
error => new AssetProxy(value, file, false) error => new AssetProxy(value, fileObj, false)
); );
} else if (privateUpload) { } else if (privateUpload) {
throw new Error('The Private Upload option is only avaible for Asset Store Integration'); throw new Error('The Private Upload option is only avaible for Asset Store Integration');
} }
return Promise.resolve(new AssetProxy(value, file, uploaded)); return Promise.resolve(new AssetProxy(value, fileObj, uploaded));
} }