-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
Copy pathAssetProxy.js
60 lines (53 loc) · 2.07 KB
/
AssetProxy.js
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
import { resolvePath } from 'Lib/pathHelper';
import { currentBackend } from "Backends/backend";
import { getIntegrationProvider } from 'Integrations';
import { selectIntegration } from 'Reducers';
let store;
export const setStore = (storeObj) => {
store = storeObj;
};
export default function AssetProxy(value, fileObj, uploaded = false, asset) {
const config = store.getState().config;
this.value = value;
this.fileObj = fileObj;
this.uploaded = uploaded;
this.sha = null;
this.path = config.get('media_folder') && !uploaded ? resolvePath(value, config.get('media_folder')) : value;
this.public_path = !uploaded ? resolvePath(value, config.get('public_folder')) : value;
this.asset = asset;
}
AssetProxy.prototype.toString = function () {
// Use the deployed image path if we do not have a locally cached copy.
if (this.uploaded && !this.fileObj) return this.public_path;
try {
return window.URL.createObjectURL(this.fileObj);
} catch (error) {
return null;
}
};
AssetProxy.prototype.toBase64 = function () {
return new Promise((resolve, reject) => {
const fr = new FileReader();
fr.onload = (readerEvt) => {
const binaryString = readerEvt.target.result;
resolve(binaryString.split('base64,')[1]);
};
fr.readAsDataURL(this.fileObj);
});
};
export function createAssetProxy(value, fileObj, uploaded = false, privateUpload = false) {
const state = store.getState();
const integration = selectIntegration(state, null, 'assetStore');
if (integration && !uploaded) {
const provider = integration && getIntegrationProvider(state.integrations, currentBackend(state.config).getToken, integration);
return provider.upload(fileObj, privateUpload).then(
response => (
new AssetProxy(response.asset.url.replace(/^(https?):/, ''), null, true, response.asset)
),
error => new AssetProxy(value, fileObj, false)
);
} else if (privateUpload) {
throw new Error('The Private Upload option is only avaible for Asset Store Integration');
}
return Promise.resolve(new AssetProxy(value, fileObj, uploaded));
}