p5.js-web-editor/client/modules/IDE/actions/files.js

115 lines
3.1 KiB
JavaScript
Raw Normal View History

2016-06-22 19:58:23 +00:00
import * as ActionTypes from '../../../constants';
import axios from 'axios';
2016-07-20 04:51:27 +00:00
import blobUtil from 'blob-util';
2016-07-21 18:18:38 +00:00
import xhr from 'xhr';
import fileType from 'file-type';
const ROOT_URL = location.href.indexOf('localhost') > 0 ? 'http://localhost:8000/api' : '/api';
2016-06-22 19:58:23 +00:00
function appendToFilename(filename, string) {
const dotIndex = filename.lastIndexOf('.');
if (dotIndex === -1) return filename + string;
return filename.substring(0, dotIndex) + string + filename.substring(dotIndex);
}
function createUniqueName(name, files) {
let testName = name;
let index = 1;
let existingName = files.find((file) => name === file.name);
while (existingName) {
testName = appendToFilename(name, `-${index}`);
index++;
existingName = files.find((file) => testName === file.name); // eslint-disable-line
}
return testName;
}
export function updateFileContent(name, content) {
2016-06-23 22:29:55 +00:00
return {
type: ActionTypes.UPDATE_FILE_CONTENT,
2016-06-23 22:29:55 +00:00
name,
content
};
}
2016-07-20 04:51:27 +00:00
export function getBlobUrl(file) {
return (dispatch) => {
2016-07-21 18:18:38 +00:00
xhr({
uri: file.url,
responseType: 'arraybuffer',
useXDR: true
}, (err, body, res) => {
if (err) throw err;
const typeOfFile = fileType(new Uint8Array(res));
blobUtil.arrayBufferToBlob(res, typeOfFile.mime)
.then(blobUtil.createObjectURL)
.then(objectURL => {
dispatch({
type: ActionTypes.SET_BLOB_URL,
name: file.name,
blobURL: objectURL
});
2016-07-20 04:51:27 +00:00
});
});
2016-07-21 18:18:38 +00:00
// blobUtil.imgSrcToBlob(file.url, undefined, { crossOrigin: 'Anonymous' })
// .then(blobUtil.createObjectURL)
// .then(objectURL => {
// dispatch({
// type: ActionTypes.SET_BLOB_URL,
// name: file.name,
// blobURL: objectURL
// });
// });
2016-07-20 04:51:27 +00:00
};
}
export function createFile(formProps) {
return (dispatch, getState) => {
const state = getState();
if (state.project.id) {
const postParams = {
name: createUniqueName(formProps.name, state.files),
url: formProps.url
};
axios.post(`${ROOT_URL}/projects/${state.project.id}/files`, postParams, { withCredentials: true })
.then(response => {
2016-07-20 04:51:27 +00:00
if (response.data.url) {
getBlobUrl(response.data)(dispatch);
}
dispatch({
type: ActionTypes.CREATE_FILE,
...response.data
});
dispatch({
type: ActionTypes.HIDE_MODAL
});
})
.catch(response => dispatch({
type: ActionTypes.ERROR,
error: response.data
}));
} else {
let maxFileId = 0;
state.files.forEach(file => {
if (parseInt(file.id, 10) > maxFileId) {
maxFileId = parseInt(file.id, 10);
}
});
2016-07-20 04:51:27 +00:00
if (formProps.url) {
getBlobUrl(formProps)(dispatch);
}
dispatch({
type: ActionTypes.CREATE_FILE,
name: createUniqueName(formProps.name, state.files),
2016-07-20 19:33:37 +00:00
id: `${maxFileId + 1}`,
url: formProps.url
});
dispatch({
type: ActionTypes.HIDE_MODAL
});
}
};
}