filebrowser/frontend/src/store/modules/upload.js

110 lines
2.7 KiB
JavaScript
Raw Normal View History

2021-03-21 11:51:58 +00:00
import Vue from "vue";
import { files as api } from "@/api";
import throttle from "lodash.throttle";
import buttons from "@/utils/buttons";
2020-07-09 15:16:04 +00:00
const UPLOADS_LIMIT = 5;
2020-07-08 14:12:33 +00:00
const state = {
id: 0,
size: 0,
2020-07-09 15:16:04 +00:00
progress: [],
queue: [],
2021-03-21 11:51:58 +00:00
uploads: {},
};
2020-07-08 14:12:33 +00:00
const mutations = {
setProgress(state, { id, loaded }) {
2021-03-21 11:51:58 +00:00
Vue.set(state.progress, id, loaded);
2020-07-08 14:12:33 +00:00
},
reset: (state) => {
2021-03-21 11:51:58 +00:00
state.id = 0;
state.size = 0;
state.progress = [];
2020-07-09 15:16:04 +00:00
},
addJob: (state, item) => {
2021-03-21 11:51:58 +00:00
state.queue.push(item);
state.size += item.file.size;
state.id++;
2020-07-09 15:16:04 +00:00
},
moveJob(state) {
2021-03-21 11:51:58 +00:00
const item = state.queue[0];
state.queue.shift();
Vue.set(state.uploads, item.id, item);
2020-07-09 15:16:04 +00:00
},
removeJob(state, id) {
2021-03-21 11:51:58 +00:00
delete state.uploads[id];
},
};
2020-07-09 15:16:04 +00:00
2020-07-13 18:09:01 +00:00
const beforeUnload = (event) => {
2021-03-21 11:51:58 +00:00
event.preventDefault();
event.returnValue = "";
};
2020-07-13 18:09:01 +00:00
2020-07-09 15:16:04 +00:00
const actions = {
upload: (context, item) => {
let uploadsCount = Object.keys(context.state.uploads).length;
2021-03-21 11:51:58 +00:00
let isQueueEmpty = context.state.queue.length == 0;
let isUploadsEmpty = uploadsCount == 0;
2020-07-09 15:16:04 +00:00
if (isQueueEmpty && isUploadsEmpty) {
2021-03-21 11:51:58 +00:00
window.addEventListener("beforeunload", beforeUnload);
buttons.loading("upload");
2020-07-09 15:16:04 +00:00
}
2021-03-21 11:51:58 +00:00
context.commit("addJob", item);
context.dispatch("processUploads");
2020-07-09 15:16:04 +00:00
},
finishUpload: (context, item) => {
2021-03-21 11:51:58 +00:00
context.commit("setProgress", { id: item.id, loaded: item.file.size });
context.commit("removeJob", item.id);
context.dispatch("processUploads");
2020-07-09 15:16:04 +00:00
},
processUploads: async (context) => {
let uploadsCount = Object.keys(context.state.uploads).length;
2021-03-21 11:51:58 +00:00
let isBellowLimit = uploadsCount < UPLOADS_LIMIT;
let isQueueEmpty = context.state.queue.length == 0;
let isUploadsEmpty = uploadsCount == 0;
2020-07-09 15:16:04 +00:00
2021-03-21 11:51:58 +00:00
let isFinished = isQueueEmpty && isUploadsEmpty;
let canProcess = isBellowLimit && !isQueueEmpty;
2020-07-09 15:16:04 +00:00
if (isFinished) {
2021-03-21 11:51:58 +00:00
window.removeEventListener("beforeunload", beforeUnload);
buttons.success("upload");
context.commit("reset");
context.commit("setReload", true, { root: true });
2020-07-09 15:16:04 +00:00
}
if (canProcess) {
const item = context.state.queue[0];
2021-03-21 11:51:58 +00:00
context.commit("moveJob");
2020-07-09 15:16:04 +00:00
if (item.file.isDir) {
2021-03-21 11:51:58 +00:00
await api.post(item.path).catch(Vue.prototype.$showError);
2020-07-09 15:16:04 +00:00
} else {
let onUpload = throttle(
2021-03-21 11:51:58 +00:00
(event) =>
context.commit("setProgress", {
id: item.id,
loaded: event.loaded,
}),
100,
{ leading: true, trailing: false }
);
await api
.post(item.path, item.file, item.overwrite, onUpload)
.catch(Vue.prototype.$showError);
2020-07-09 15:16:04 +00:00
}
2021-03-21 11:51:58 +00:00
context.dispatch("finishUpload", item);
2020-07-09 15:16:04 +00:00
}
2021-03-21 11:51:58 +00:00
},
};
2020-07-08 14:12:33 +00:00
2021-03-21 11:51:58 +00:00
export default { state, mutations, actions, namespaced: true };