feat: implement abort upload functionality (#2673)

This commit is contained in:
M A E R Y O 2023-08-28 06:59:49 +09:00 committed by GitHub
parent 95fec7f694
commit a404fb043d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 55 additions and 6 deletions

View File

@ -6,6 +6,7 @@ import { fetchURL } from "./utils";
const RETRY_BASE_DELAY = 1000; const RETRY_BASE_DELAY = 1000;
const RETRY_MAX_DELAY = 20000; const RETRY_MAX_DELAY = 20000;
const CURRENT_UPLOAD_LIST = {};
export async function upload( export async function upload(
filePath, filePath,
@ -34,6 +35,7 @@ export async function upload(
"X-Auth": store.state.jwt, "X-Auth": store.state.jwt,
}, },
onError: function (error) { onError: function (error) {
delete CURRENT_UPLOAD_LIST[filePath];
reject("Upload failed: " + error); reject("Upload failed: " + error);
}, },
onProgress: function (bytesUploaded) { onProgress: function (bytesUploaded) {
@ -44,10 +46,12 @@ export async function upload(
} }
}, },
onSuccess: function () { onSuccess: function () {
delete CURRENT_UPLOAD_LIST[filePath];
resolve(); resolve();
}, },
}); });
upload.start(); upload.start();
CURRENT_UPLOAD_LIST[filePath] = upload;
}); });
} }
@ -88,3 +92,21 @@ export async function useTus(content) {
function isTusSupported() { function isTusSupported() {
return tus.isSupported === true; return tus.isSupported === true;
} }
export function abortUpload(filePath) {
const upload = CURRENT_UPLOAD_LIST[filePath];
if (upload) {
upload.abort();
delete CURRENT_UPLOAD_LIST[filePath];
}
}
export function abortAllUploads() {
for (let filePath in CURRENT_UPLOAD_LIST) {
const upload = CURRENT_UPLOAD_LIST[filePath];
if (upload) {
upload.abort();
delete CURRENT_UPLOAD_LIST[filePath];
}
}
}

View File

@ -7,7 +7,14 @@
<div class="card floating"> <div class="card floating">
<div class="card-title"> <div class="card-title">
<h2>{{ $t("prompts.uploadFiles", { files: filesInUploadCount }) }}</h2> <h2>{{ $t("prompts.uploadFiles", { files: filesInUploadCount }) }}</h2>
<button
class="action"
@click="abortAll"
aria-label="Abort upload"
title="Abort upload"
>
<i class="material-icons">{{ "cancel" }}</i>
</button>
<button <button
class="action" class="action"
@click="toggle" @click="toggle"
@ -42,7 +49,9 @@
</template> </template>
<script> <script>
import { mapGetters } from "vuex"; import { mapGetters, mapMutations } from "vuex";
import { abortAllUploads } from "@/api/tus";
import buttons from "@/utils/buttons";
export default { export default {
name: "uploadFiles", name: "uploadFiles",
@ -53,11 +62,21 @@ export default {
}, },
computed: { computed: {
...mapGetters(["filesInUpload", "filesInUploadCount"]), ...mapGetters(["filesInUpload", "filesInUploadCount"]),
...mapMutations(['resetUpload']),
}, },
methods: { methods: {
toggle: function () { toggle: function () {
this.open = !this.open; this.open = !this.open;
}, },
abortAll() {
if (confirm(this.$t('upload.abortUpload'))) {
abortAllUploads();
buttons.done('upload');
this.open = false;
this.$store.commit('resetUpload');
this.$store.commit("setReload", true);
}
},
}, },
}; };
</script> </script>

View File

@ -45,6 +45,9 @@
"downloadFolder": "Download Folder", "downloadFolder": "Download Folder",
"downloadSelected": "Download Selected" "downloadSelected": "Download Selected"
}, },
"upload": {
"abortUpload": "Are you sure you want to abort?"
},
"errors": { "errors": {
"forbidden": "You don't have permissions to access this.", "forbidden": "You don't have permissions to access this.",
"internal": "Something really went wrong.", "internal": "Something really went wrong.",

View File

@ -4,7 +4,7 @@ const getters = {
isListing: (state, getters) => getters.isFiles && state.req.isDir, isListing: (state, getters) => getters.isFiles && state.req.isDir,
selectedCount: (state) => state.selected.length, selectedCount: (state) => state.selected.length,
progress: (state) => { progress: (state) => {
if (state.upload.progress.length == 0) { if (state.upload.progress.length === 0) {
return 0; return 0;
} }
@ -14,9 +14,7 @@ const getters = {
return Math.ceil((sum / totalSize) * 100); return Math.ceil((sum / totalSize) * 100);
}, },
filesInUploadCount: (state) => { filesInUploadCount: (state) => {
let total = return Object.keys(state.upload.uploads).length + state.upload.queue.length;
Object.keys(state.upload.uploads).length + state.upload.queue.length;
return total;
}, },
filesInUpload: (state) => { filesInUpload: (state) => {
let files = []; let files = [];

View File

@ -97,6 +97,13 @@ const mutations = {
state.clipboard.key = ""; state.clipboard.key = "";
state.clipboard.items = []; state.clipboard.items = [];
}, },
resetUpload(state) {
state.upload.uploads = {};
state.upload.queue = [];
state.upload.progress = [];
state.upload.sizes = [];
state.upload.id = 0;
},
}; };
export default mutations; export default mutations;