feat: upload progress based on total size (#993)

This commit is contained in:
Ramires Viana 2020-06-19 04:46:33 -03:00 committed by GitHub
parent 241201657c
commit cd454bae51
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 55 additions and 18 deletions

View File

@ -8802,6 +8802,11 @@
"integrity": "sha1-vMbEmkKihA7Zl/Mj6tpezRguC/4=", "integrity": "sha1-vMbEmkKihA7Zl/Mj6tpezRguC/4=",
"dev": true "dev": true
}, },
"lodash.throttle": {
"version": "4.1.1",
"resolved": "https://registry.npmjs.org/lodash.throttle/-/lodash.throttle-4.1.1.tgz",
"integrity": "sha1-wj6RtxAkKscMN/HhzaknTMOb8vQ="
},
"lodash.transform": { "lodash.transform": {
"version": "4.6.0", "version": "4.6.0",
"resolved": "https://registry.npmjs.org/lodash.transform/-/lodash.transform-4.6.0.tgz", "resolved": "https://registry.npmjs.org/lodash.transform/-/lodash.transform-4.6.0.tgz",

View File

@ -13,6 +13,7 @@
"clipboard": "^2.0.4", "clipboard": "^2.0.4",
"js-base64": "^2.5.1", "js-base64": "^2.5.1",
"lodash.clonedeep": "^4.5.0", "lodash.clonedeep": "^4.5.0",
"lodash.throttle": "^4.1.1",
"material-design-icons": "^3.0.1", "material-design-icons": "^3.0.1",
"moment": "^2.24.0", "moment": "^2.24.0",
"normalize.css": "^8.0.1", "normalize.css": "^8.0.1",

View File

@ -89,6 +89,7 @@
<script> <script>
import { mapState, mapMutations } from 'vuex' import { mapState, mapMutations } from 'vuex'
import throttle from 'lodash.throttle'
import Item from './ListingItem' import Item from './ListingItem'
import css from '@/utils/css' import css from '@/utils/css'
import { users, files as api } from '@/api' import { users, files as api } from '@/api'
@ -100,7 +101,13 @@ export default {
components: { Item }, components: { Item },
data: function () { data: function () {
return { return {
show: 50 show: 50,
uploading: {
id: 0,
count: 0,
size: 0,
progress: []
}
} }
}, },
computed: { computed: {
@ -450,20 +457,24 @@ export default {
} }
}) })
}, },
handleFiles (files, base, overwrite = false) { setProgress: throttle(function() {
buttons.loading('upload') if (this.uploading.count == 0) {
let promises = [] return
let progress = new Array(files.length).fill(0)
let onupload = (id) => (event) => {
progress[id] = (event.loaded / event.total) * 100
let sum = 0
for (let i = 0; i < progress.length; i++) {
sum += progress[i]
} }
this.$store.commit('setProgress', Math.ceil(sum / progress.length)) let sum = this.uploading.progress.reduce((acc, val) => acc + val)
this.$store.commit('setProgress', Math.ceil(sum / this.uploading.size * 100))
}, 100, {leading: false, trailing: true}),
handleFiles (files, base, overwrite = false) {
if (this.uploading.count == 0) {
buttons.loading('upload')
}
let promises = []
let onupload = (id) => (event) => {
this.uploading.progress[id] = event.loaded
this.setProgress()
} }
for (let i = 0; i < files.length; i++) { for (let i = 0; i < files.length; i++) {
@ -472,30 +483,50 @@ export default {
if (!file.isDir) { if (!file.isDir) {
let filename = (file.fullPath !== undefined) ? file.fullPath : file.name let filename = (file.fullPath !== undefined) ? file.fullPath : file.name
let filenameEncoded = url.encodeRFC5987ValueChars(filename) let filenameEncoded = url.encodeRFC5987ValueChars(filename)
promises.push(api.post(this.$route.path + base + filenameEncoded, file, overwrite, onupload(i)))
let id = this.uploading.id
this.uploading.size += file.size
this.uploading.id++
this.uploading.count++
let promise = api.post(this.$route.path + base + filenameEncoded, file, overwrite, throttle(onupload(id), 100)).finally(() => {
this.uploading.count--
})
promises.push(promise)
} else { } else {
let uri = this.$route.path + base; let uri = this.$route.path + base
let folders = file.path.split("/"); let folders = file.path.split("/")
for (let i = 0; i < folders.length; i++) { for (let i = 0; i < folders.length; i++) {
let folder = folders[i]; let folder = folders[i]
let folderEncoded = encodeURIComponent(folder); let folderEncoded = encodeURIComponent(folder)
uri += folderEncoded + "/" uri += folderEncoded + "/"
} }
api.post(uri); api.post(uri)
} }
} }
let finish = () => { let finish = () => {
if (this.uploading.count > 0) {
return
}
buttons.success('upload') buttons.success('upload')
this.$store.commit('setProgress', 0) this.$store.commit('setProgress', 0)
this.$store.commit('setReload', true)
this.uploading.id = 0
this.uploading.sizes = []
this.uploading.progress = []
} }
Promise.all(promises) Promise.all(promises)
.then(() => { .then(() => {
finish() finish()
this.$store.commit('setReload', true)
}) })
.catch(error => { .catch(error => {
finish() finish()