- md5
- aeac82323358eb31efafff51a8fb0cf5
- sha1
- 91912993d95b657081358c8ce31776967fbe5a13
- sha256
- 8ab80d1911c8988d90957b9fbb1842b19ea4ba5f38421bbef2f3ea336ccbea3d
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
import {api} from "../app/Api";
import * as types from "./mutation-types";
import translate from "../plugins/translate";
import {notifications} from "../app/Notifications";
import * as FileSaver from './../../../node_modules/file-saver/FileSaver';
// Actions are similar to mutations, the difference being that:
// - Instead of mutating the state, actions commit mutations.
// - Actions can contain arbitrary asynchronous operations.
// TODO move to utils
function updateUrlPath(path) {
if (path == null) {
path = '';
}
let url = window.location.href;
var pattern = new RegExp('\\b(path=).*?(&|$)');
if (url.search(pattern) >= 0) {
history.pushState(null, '', url.replace(pattern, '$1' + path + '$2'));
} else {
history.pushState(null, '', url + (url.indexOf('?') > 0 ? '&' : '?') + 'path=' + path);
}
}
/**
* Get contents of a directory from the api
* @param commit
* @param payload
*/
export const getContents = (context, payload) => {
// Update the url
updateUrlPath(payload);
context.commit(types.SET_IS_LOADING, true);
api.getContents(payload, 0)
.then(contents => {
context.commit(types.LOAD_CONTENTS_SUCCESS, contents);
context.commit(types.UNSELECT_ALL_BROWSER_ITEMS);
context.commit(types.SELECT_DIRECTORY, payload);
context.commit(types.SET_IS_LOADING, false);
})
.catch(error => {
// TODO error handling
context.commit(types.SET_IS_LOADING, false);
console.log("error", error);
});
}
/**
* Get the full contents of a directory
* @param context
* @param payload
*/
export const getFullContents = (context, payload) => {
context.commit(types.SET_IS_LOADING, true);
api.getContents(payload.path, 1)
.then(contents => {
context.commit(types.LOAD_FULL_CONTENTS_SUCCESS, contents.files[0]);
context.commit(types.SET_IS_LOADING, false);
})
.catch(error => {
// TODO error handling
context.commit(types.SET_IS_LOADING, false);
console.log("error", error);
});
}
/**
* Download a file
* @param context
* @param payload
*/
export const download = (context, payload) => {
api.getContents(payload.path, 0, 1)
.then(contents => {
var file = contents.files[0];
// Converte the base 64 encoded string to a blob
var byteCharacters = atob(file.content);
var byteArrays = [];
for (var offset = 0; offset < byteCharacters.length; offset += 512) {
var slice = byteCharacters.slice(offset, offset + 512);
var byteNumbers = new Array(slice.length);
for (var i = 0; i < slice.length; i++) {
byteNumbers[i] = slice.charCodeAt(i);
}
var byteArray = new Uint8Array(byteNumbers);
byteArrays.push(byteArray);
}
// Open the save as file dialog
FileSaver.saveAs(new Blob(byteArrays, {type: file.mime_type}), file.name);
})
.catch(error => {
console.log("error", error);
});
}
/**
* Toggle the selection state of an item
* @param commit
* @param payload
*/
export const toggleBrowserItemSelect = (context, payload) => {
const item = payload;
const isSelected = context.state.selectedItems.some(selected => selected.path === item.path);
if (!isSelected) {
context.commit(types.SELECT_BROWSER_ITEM, item);
} else {
context.commit(types.UNSELECT_BROWSER_ITEM, item);
}
}
/**
* Create a new folder
* @param commit
* @param payload object with the new folder name and its parent directory
*/
export const createDirectory = (context, payload) => {
context.commit(types.SET_IS_LOADING, true);
api.createDirectory(payload.name, payload.parent)
.then(folder => {
context.commit(types.CREATE_DIRECTORY_SUCCESS, folder);
context.commit(types.HIDE_CREATE_FOLDER_MODAL);
context.commit(types.SET_IS_LOADING, false);
})
.catch(error => {
// TODO error handling
context.commit(types.SET_IS_LOADING, false);
console.log("error", error);
})
}
/**
* Create a new folder
* @param commit
* @param payload object with the new folder name and its parent directory
*/
export const uploadFile = (context, payload) => {
context.commit(types.SET_IS_LOADING, true);
api.upload(payload.name, payload.parent, payload.content, payload.override || false)
.then(file => {
context.commit(types.UPLOAD_SUCCESS, file);
context.commit(types.SET_IS_LOADING, false);
})
.catch(error => {
context.commit(types.SET_IS_LOADING, false);
// Handle file exists
if (error.status === 409) {
if (notifications.ask(translate.sprintf('COM_MEDIA_FILE_EXISTS_AND_OVERRIDE', payload.name), {})) {
payload.override = true;
uploadFile(context, payload);
}
}
})
}
/**
* Delete a single item
* @param context
* @param payload object: the item to delete
*/
export const deleteItem = (context, payload) => {
context.commit(types.SET_IS_LOADING, true);
const item = payload;
api.delete(item.path)
.then(() => {
context.commit(types.DELETE_SUCCESS, item);
context.commit(types.UNSELECT_ALL_BROWSER_ITEMS);
context.commit(types.SET_IS_LOADING, false);
})
.catch(error => {
// TODO error handling
context.commit(types.SET_IS_LOADING, false);
console.log("error", error);
})
}
/**
* Rename an item
* @param context
* @param payload object: the old and the new path
*/
export const renameItem = (context, payload) => {
context.commit(types.SET_IS_LOADING, true);
api.rename(payload.path, payload.newPath)
.then((item) => {
context.commit(types.RENAME_SUCCESS, {
item: item,
oldPath: payload.path,
});
context.commit(types.HIDE_RENAME_MODAL);
context.commit(types.SET_IS_LOADING, false);
})
.catch(error => {
// TODO error handling
context.commit(types.SET_IS_LOADING, false);
console.log("error", error);
})
}
/**
* Delete the selected items
* @param context
* @param payload object
*/
export const deleteSelectedItems = (context, payload) => {
context.commit(types.SET_IS_LOADING, true);
// Get the selected items from the store
const selectedItems = context.state.selectedItems;
if (selectedItems.length > 0) {
selectedItems.forEach(item => {
api.delete(item.path)
.then(() => {
context.commit(types.DELETE_SUCCESS, item);
context.commit(types.UNSELECT_ALL_BROWSER_ITEMS);
context.commit(types.SET_IS_LOADING, false);
})
.catch(error => {
// TODO error handling
context.commit(types.SET_IS_LOADING, false);
console.log("error", error);
})
})
} else {
// TODO notify the user that he has to select at least one item
}
}
No other release has this exact content. See every version of this file.