2016-06-22 19:58:23 +00:00
|
|
|
import * as ActionTypes from '../../../constants';
|
2016-07-13 22:53:56 +00:00
|
|
|
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';
|
2016-08-24 18:22:10 +00:00
|
|
|
import objectID from 'bson-objectid';
|
2016-07-13 22:53:56 +00:00
|
|
|
|
|
|
|
const ROOT_URL = location.href.indexOf('localhost') > 0 ? 'http://localhost:8000/api' : '/api';
|
2016-06-22 19:58:23 +00:00
|
|
|
|
2016-07-21 02:52:09 +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;
|
|
|
|
}
|
|
|
|
|
2016-07-07 17:50:52 +00:00
|
|
|
export function updateFileContent(name, content) {
|
2016-06-23 22:29:55 +00:00
|
|
|
return {
|
2016-07-07 17:50:52 +00:00
|
|
|
type: ActionTypes.UPDATE_FILE_CONTENT,
|
2016-06-23 22:29:55 +00:00
|
|
|
name,
|
|
|
|
content
|
|
|
|
};
|
|
|
|
}
|
2016-07-13 20:13:28 +00:00
|
|
|
|
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
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-07-13 22:53:56 +00:00
|
|
|
export function createFile(formProps) {
|
|
|
|
return (dispatch, getState) => {
|
|
|
|
const state = getState();
|
2016-08-24 18:22:10 +00:00
|
|
|
const rootFile = state.files.filter(file => file.name === 'root')[0];
|
2016-07-13 22:53:56 +00:00
|
|
|
if (state.project.id) {
|
|
|
|
const postParams = {
|
2016-07-21 02:52:09 +00:00
|
|
|
name: createUniqueName(formProps.name, state.files),
|
2016-08-25 15:25:22 +00:00
|
|
|
url: formProps.url,
|
2016-08-25 21:32:27 +00:00
|
|
|
content: formProps.content || '',
|
2016-08-23 23:40:47 +00:00
|
|
|
// TODO pass parent id to API, once there are folders
|
2016-08-24 18:22:10 +00:00
|
|
|
parentId: rootFile.id
|
2016-07-13 22:53:56 +00:00
|
|
|
};
|
|
|
|
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);
|
|
|
|
}
|
2016-07-13 22:53:56 +00:00
|
|
|
dispatch({
|
|
|
|
type: ActionTypes.CREATE_FILE,
|
2016-08-24 21:29:44 +00:00
|
|
|
...response.data,
|
|
|
|
parentId: rootFile.id
|
2016-07-13 22:53:56 +00:00
|
|
|
});
|
2016-07-20 02:29:54 +00:00
|
|
|
dispatch({
|
|
|
|
type: ActionTypes.HIDE_MODAL
|
|
|
|
});
|
2016-07-13 22:53:56 +00:00
|
|
|
})
|
|
|
|
.catch(response => dispatch({
|
|
|
|
type: ActionTypes.ERROR,
|
|
|
|
error: response.data
|
|
|
|
}));
|
|
|
|
} else {
|
2016-07-20 04:51:27 +00:00
|
|
|
if (formProps.url) {
|
|
|
|
getBlobUrl(formProps)(dispatch);
|
|
|
|
}
|
2016-08-24 18:22:10 +00:00
|
|
|
const id = objectID().toHexString();
|
2016-07-13 22:53:56 +00:00
|
|
|
dispatch({
|
|
|
|
type: ActionTypes.CREATE_FILE,
|
2016-07-21 02:52:09 +00:00
|
|
|
name: createUniqueName(formProps.name, state.files),
|
2016-08-24 18:22:10 +00:00
|
|
|
id,
|
|
|
|
_id: id,
|
2016-08-25 04:18:28 +00:00
|
|
|
url: formProps.url,
|
2016-08-25 21:32:27 +00:00
|
|
|
content: formProps.content || '',
|
2016-08-23 23:40:47 +00:00
|
|
|
// TODO pass parent id from File Tree
|
2016-08-24 18:22:10 +00:00
|
|
|
parentId: rootFile.id
|
2016-07-13 22:53:56 +00:00
|
|
|
});
|
|
|
|
dispatch({
|
|
|
|
type: ActionTypes.HIDE_MODAL
|
|
|
|
});
|
|
|
|
}
|
2016-07-13 20:13:28 +00:00
|
|
|
};
|
|
|
|
}
|
2016-08-03 19:11:59 +00:00
|
|
|
|
2016-08-30 03:23:10 +00:00
|
|
|
export function createFolder(formProps) {
|
|
|
|
return (dispatch, getState) => {
|
|
|
|
const state = getState();
|
|
|
|
const rootFile = state.files.filter(file => file.name === 'root')[0];
|
|
|
|
if (state.project.id) {
|
|
|
|
const postParams = {
|
|
|
|
name: createUniqueName(formProps.name, state.files),
|
|
|
|
content: '',
|
|
|
|
parentId: rootFile.id,
|
|
|
|
fileType: 'folder'
|
|
|
|
};
|
|
|
|
axios.post(`${ROOT_URL}/projects/${state.project.id}/files`, postParams, { withCredentials: true })
|
|
|
|
.then(response => {
|
|
|
|
dispatch({
|
|
|
|
type: ActionTypes.CREATE_FILE,
|
|
|
|
...response.data,
|
|
|
|
parentId: rootFile.id
|
|
|
|
});
|
|
|
|
dispatch({
|
|
|
|
type: ActionTypes.CLOSE_NEW_FOLDER_MODAL
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.catch(response => dispatch({
|
|
|
|
type: ActionTypes.ERROR,
|
|
|
|
error: response.data
|
|
|
|
}));
|
|
|
|
} else {
|
|
|
|
const id = objectID().toHexString();
|
|
|
|
dispatch({
|
|
|
|
type: ActionTypes.CREATE_FILE,
|
|
|
|
name: createUniqueName(formProps.name, state.files),
|
|
|
|
id,
|
|
|
|
_id: id,
|
|
|
|
content: '',
|
|
|
|
// TODO pass parent id from File Tree
|
|
|
|
parentId: rootFile.id,
|
|
|
|
fileType: 'folder'
|
|
|
|
});
|
|
|
|
dispatch({
|
|
|
|
type: ActionTypes.CLOSE_NEW_FOLDER_MODAL
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-08-03 19:11:59 +00:00
|
|
|
export function showFileOptions(fileId) {
|
|
|
|
return {
|
|
|
|
type: ActionTypes.SHOW_FILE_OPTIONS,
|
|
|
|
id: fileId
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function hideFileOptions(fileId) {
|
|
|
|
return {
|
|
|
|
type: ActionTypes.HIDE_FILE_OPTIONS,
|
|
|
|
id: fileId
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-08-03 21:10:03 +00:00
|
|
|
export function showEditFileName(id) {
|
|
|
|
return {
|
|
|
|
type: ActionTypes.SHOW_EDIT_FILE_NAME,
|
|
|
|
id
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function hideEditFileName(id) {
|
|
|
|
return {
|
|
|
|
type: ActionTypes.HIDE_EDIT_FILE_NAME,
|
|
|
|
id
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-08-03 19:11:59 +00:00
|
|
|
export function updateFileName(id, name) {
|
|
|
|
return {
|
|
|
|
type: ActionTypes.UPDATE_FILE_NAME,
|
|
|
|
id,
|
|
|
|
name
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-08-24 20:06:28 +00:00
|
|
|
export function deleteFile(id, parentId) {
|
2016-08-24 21:59:15 +00:00
|
|
|
return (dispatch, getState) => {
|
|
|
|
const state = getState();
|
2016-08-24 22:52:08 +00:00
|
|
|
if (state.project.id) {
|
|
|
|
const deleteConfig = {
|
|
|
|
params: {
|
2016-08-24 21:59:15 +00:00
|
|
|
parentId
|
2016-08-24 22:52:08 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
axios.delete(`${ROOT_URL}/projects/${state.project.id}/files/${id}`, deleteConfig, { withCredentials: true })
|
|
|
|
.then(() => {
|
|
|
|
dispatch({
|
|
|
|
type: ActionTypes.DELETE_FILE,
|
|
|
|
id,
|
|
|
|
parentId
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.catch(response => {
|
|
|
|
dispatch({
|
|
|
|
type: ActionTypes.ERROR,
|
|
|
|
error: response.data
|
|
|
|
});
|
2016-08-24 21:59:15 +00:00
|
|
|
});
|
2016-08-24 22:52:08 +00:00
|
|
|
} else {
|
|
|
|
dispatch({
|
|
|
|
type: ActionTypes.DELETE_FILE,
|
|
|
|
id,
|
|
|
|
parentId
|
|
|
|
});
|
|
|
|
}
|
2016-08-03 19:11:59 +00:00
|
|
|
};
|
|
|
|
}
|