chunk upload and multipart upload beta.1

This commit is contained in:
carodej
2020-07-29 12:29:30 +02:00
parent bf8db1be52
commit 18761eb5b3
18 changed files with 307 additions and 243 deletions
+53 -79
View File
@@ -73,28 +73,31 @@ const Helpers = {
this.$store.dispatch('createFolder', folderName)
}
Vue.prototype.$uploadFiles = async function (files) {
// Prevent submit empty files
if (files && files.length == 0) return
Vue.prototype.$handleUploading = async function (files, parent_id) {
let fileCount = files ? files.length : 0
let fileCountSucceed = 1
let fileBuffer = []
// Append the file list to fileBuffer array
Array.prototype.push.apply(fileBuffer, files);
let fileSucceed = 0
// Update files count in progressbar
store.commit('UPDATE_FILE_COUNT_PROGRESS', {
current: fileCountSucceed,
total: fileCount
current: fileSucceed,
total: files.length
})
// Reset upload progress to 0
store.commit('UPLOADING_FILE_PROGRESS', 0)
// Get parent id
const rootFolder = this.$store.getters.currentFolder
? this.$store.getters.currentFolder.unique_id
: 0
let parentFolder = this.$store.getters.currentFolder ? this.$store.getters.currentFolder.unique_id : 0
let rootFolder = parent_id ? parent_id : parentFolder
for (var i = files.length - 1; i >= 0; i--) {
let file = files[i],
// Upload files
do {
let file = fileBuffer.shift(),
chunks = []
// Calculate ceils
@@ -108,21 +111,23 @@ const Helpers = {
));
}
// Set form Data
let formData = new FormData()
let uploadedSize = 0
// Set Data
let formData = new FormData(),
uploadedSize = 0,
isNotGeneralError = true
do {
let isLast = chunks.length === 1
let chunk = chunks.shift()
let attempts = 0
let isLast = chunks.length === 1,
chunk = chunks.shift(),
attempts = 0,
filename = Array(16).fill(0).map(x => Math.random().toString(36).charAt(2)).join('') + '-' + file.name + '.part'
// Set form data
formData.set('is_last', isLast);
formData.set('file', chunk, `${file.name}.part`);
formData.set('file', chunk, filename);
formData.set('parent_id', rootFolder)
formData.set('is_last', isLast);
// Upload data
// Upload chunks
do {
await store.dispatch('uploadFiles', {
form: formData,
@@ -130,35 +135,39 @@ const Helpers = {
totalUploadedSize: uploadedSize
}).then(() => {
uploadedSize = uploadedSize + chunk.size
}).catch(() => {
}).catch((error) => {
// Count attempts
attempts++
if (attempts === 3) {
events.$emit('alert:open', {
title: this.$t('popup_error.title'),
message: this.$t('popup_error.message'),
})
}
})
} while (attempts !== 0 && attempts !== 3)
// Break uploading proccess
if (error.response.status === 500)
isNotGeneralError = false
} while (chunks.length !== 0)
// Show Error
if (attempts === 3)
this.$isSomethingWrong()
})
} while (isNotGeneralError && attempts !== 0 && attempts !== 3)
} while (isNotGeneralError && chunks.length !== 0)
fileSucceed++
// Progress file log
store.commit('UPDATE_FILE_COUNT_PROGRESS', {
current: fileCountSucceed,
total: fileCount
current: fileSucceed,
total: files.length
})
// Uploading finished
if (fileCount === fileCountSucceed) {
store.commit('UPDATE_FILE_COUNT_PROGRESS', undefined)
} else {
// Add uploaded file
fileCountSucceed++
}
}
} while (fileBuffer.length !== 0)
store.commit('UPDATE_FILE_COUNT_PROGRESS', undefined)
}
Vue.prototype.$uploadFiles = async function (files) {
this.$handleUploading(files, undefined)
}
Vue.prototype.$uploadExternalFiles = async function (event, parent_id) {
@@ -167,44 +176,9 @@ const Helpers = {
if (event.dataTransfer.items.length == 0) return
// Get files
const files = [...event.dataTransfer.items].map(item => item.getAsFile());
let files = [...event.dataTransfer.items].map(item => item.getAsFile());
let fileCountSucceed = 1
store.commit('UPDATE_FILE_COUNT_PROGRESS', {
current: fileCountSucceed,
total: files.length
})
for (var i = files.length - 1; i >= 0; i--) {
let formData = new FormData()
// Append data
formData.append('file', files[i])
// Append form data
formData.append('parent_id', parent_id)
// Upload data
await store.dispatch('uploadFiles', formData).then(() => {
// Progress file log
store.commit('UPDATE_FILE_COUNT_PROGRESS', {
current: fileCountSucceed,
total: files.length
})
// Progress file log
store.commit('INCREASE_FOLDER_ITEM', parent_id)
// Uploading finished
if (files.length === fileCountSucceed) {
store.commit('UPDATE_FILE_COUNT_PROGRESS', undefined)
} else {
// Add uploaded file
fileCountSucceed++
}
})
}
this.$handleUploading(files, parent_id)
}
Vue.prototype.$downloadFile = function (url, filename) {