load blob url when uploading file
This commit is contained in:
parent
91f7cc47a0
commit
cc231cbacd
2 changed files with 23 additions and 10 deletions
|
@ -1,5 +1,6 @@
|
||||||
import * as ActionTypes from '../../../constants';
|
import * as ActionTypes from '../../../constants';
|
||||||
import axios from 'axios';
|
import axios from 'axios';
|
||||||
|
import blobUtil from 'blob-util';
|
||||||
|
|
||||||
const ROOT_URL = location.href.indexOf('localhost') > 0 ? 'http://localhost:8000/api' : '/api';
|
const ROOT_URL = location.href.indexOf('localhost') > 0 ? 'http://localhost:8000/api' : '/api';
|
||||||
|
|
||||||
|
@ -11,6 +12,20 @@ export function updateFileContent(name, content) {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function getBlobUrl(file) {
|
||||||
|
return (dispatch) => {
|
||||||
|
blobUtil.imgSrcToBlob(file.url, undefined, { crossOrigin: 'Anonymous' })
|
||||||
|
.then(blobUtil.createObjectURL)
|
||||||
|
.then(objectURL => {
|
||||||
|
dispatch({
|
||||||
|
type: ActionTypes.SET_BLOB_URL,
|
||||||
|
name: file.name,
|
||||||
|
blobURL: objectURL
|
||||||
|
});
|
||||||
|
});
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
export function createFile(formProps) {
|
export function createFile(formProps) {
|
||||||
return (dispatch, getState) => {
|
return (dispatch, getState) => {
|
||||||
const state = getState();
|
const state = getState();
|
||||||
|
@ -21,6 +36,9 @@ export function createFile(formProps) {
|
||||||
};
|
};
|
||||||
axios.post(`${ROOT_URL}/projects/${state.project.id}/files`, postParams, { withCredentials: true })
|
axios.post(`${ROOT_URL}/projects/${state.project.id}/files`, postParams, { withCredentials: true })
|
||||||
.then(response => {
|
.then(response => {
|
||||||
|
if (response.data.url) {
|
||||||
|
getBlobUrl(response.data)(dispatch);
|
||||||
|
}
|
||||||
dispatch({
|
dispatch({
|
||||||
type: ActionTypes.CREATE_FILE,
|
type: ActionTypes.CREATE_FILE,
|
||||||
...response.data
|
...response.data
|
||||||
|
@ -40,6 +58,9 @@ export function createFile(formProps) {
|
||||||
maxFileId = parseInt(file.id, 10);
|
maxFileId = parseInt(file.id, 10);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
if (formProps.url) {
|
||||||
|
getBlobUrl(formProps)(dispatch);
|
||||||
|
}
|
||||||
dispatch({
|
dispatch({
|
||||||
type: ActionTypes.CREATE_FILE,
|
type: ActionTypes.CREATE_FILE,
|
||||||
name: formProps.name,
|
name: formProps.name,
|
||||||
|
|
|
@ -3,7 +3,7 @@ import { browserHistory } from 'react-router';
|
||||||
import axios from 'axios';
|
import axios from 'axios';
|
||||||
import JSZip from 'jszip';
|
import JSZip from 'jszip';
|
||||||
import { saveAs } from 'file-saver';
|
import { saveAs } from 'file-saver';
|
||||||
import blobUtil from 'blob-util';
|
import { getBlobUrl } from './files';
|
||||||
|
|
||||||
const ROOT_URL = location.href.indexOf('localhost') > 0 ? 'http://localhost:8000/api' : '/api';
|
const ROOT_URL = location.href.indexOf('localhost') > 0 ? 'http://localhost:8000/api' : '/api';
|
||||||
|
|
||||||
|
@ -12,15 +12,7 @@ export function getProjectBlobUrls() {
|
||||||
const state = getState();
|
const state = getState();
|
||||||
state.files.forEach(file => {
|
state.files.forEach(file => {
|
||||||
if (file.url) {
|
if (file.url) {
|
||||||
blobUtil.imgSrcToBlob(file.url, undefined, { crossOrigin: 'Anonymous' })
|
getBlobUrl(file)(dispatch);
|
||||||
.then(blobUtil.createObjectURL)
|
|
||||||
.then(objectURL => {
|
|
||||||
dispatch({
|
|
||||||
type: ActionTypes.SET_BLOB_URL,
|
|
||||||
name: file.name,
|
|
||||||
blobURL: objectURL
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in a new issue