2022-04-15 14:24:30 +00:00
|
|
|
import { without } from 'lodash'
|
2022-04-21 18:39:18 +00:00
|
|
|
import { reactive } from 'vue'
|
2022-04-15 14:24:30 +00:00
|
|
|
import { UploadFile, UploadStatus } from '@/config'
|
2022-04-24 08:50:45 +00:00
|
|
|
import { httpService } from '@/services'
|
2022-06-10 10:47:46 +00:00
|
|
|
import { albumStore, overviewStore, songStore } from '@/stores'
|
2022-04-15 14:24:30 +00:00
|
|
|
import { eventBus } from '@/utils'
|
|
|
|
|
2022-06-10 10:47:46 +00:00
|
|
|
interface UploadResult {
|
|
|
|
song: Song
|
|
|
|
album: Album
|
|
|
|
}
|
|
|
|
|
2022-04-24 08:50:45 +00:00
|
|
|
export const uploadService = {
|
2022-04-21 18:39:18 +00:00
|
|
|
state: reactive({
|
2022-04-15 14:24:30 +00:00
|
|
|
files: [] as UploadFile[]
|
2022-04-21 18:39:18 +00:00
|
|
|
}),
|
2022-04-15 14:24:30 +00:00
|
|
|
|
|
|
|
simultaneousUploads: 5,
|
|
|
|
|
2022-04-21 18:39:18 +00:00
|
|
|
queue (file: UploadFile | UploadFile[]) {
|
2022-04-15 14:24:30 +00:00
|
|
|
this.state.files = this.state.files.concat(file)
|
|
|
|
this.proceed()
|
|
|
|
},
|
|
|
|
|
2022-04-21 18:39:18 +00:00
|
|
|
remove (file: UploadFile) {
|
2022-04-15 14:24:30 +00:00
|
|
|
this.state.files = without(this.state.files, file)
|
|
|
|
this.proceed()
|
|
|
|
},
|
|
|
|
|
2022-04-21 18:39:18 +00:00
|
|
|
proceed () {
|
2022-04-15 14:24:30 +00:00
|
|
|
const remainingSlots = this.simultaneousUploads - this.getUploadingFiles().length
|
|
|
|
|
|
|
|
if (remainingSlots <= 0) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for (let i = 0; i < remainingSlots; ++i) {
|
|
|
|
const file = this.getUploadCandidate()
|
|
|
|
|
|
|
|
if (file) {
|
|
|
|
this.upload(file)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2022-04-21 18:39:18 +00:00
|
|
|
getUploadingFiles () {
|
2022-04-15 14:24:30 +00:00
|
|
|
return this.state.files.filter(file => file.status === 'Uploading')
|
|
|
|
},
|
|
|
|
|
2022-04-21 18:39:18 +00:00
|
|
|
getUploadCandidate () {
|
2022-04-15 14:24:30 +00:00
|
|
|
return this.state.files.find(file => file.status === 'Ready')
|
|
|
|
},
|
|
|
|
|
2022-04-21 18:39:18 +00:00
|
|
|
async upload (file: UploadFile) {
|
2022-04-15 14:24:30 +00:00
|
|
|
if (file.status === 'Uploading') {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
const formData = new FormData()
|
|
|
|
formData.append('file', file.file)
|
|
|
|
file.progress = 0
|
|
|
|
file.status = 'Uploading'
|
|
|
|
|
|
|
|
try {
|
2022-06-10 10:47:46 +00:00
|
|
|
const result = await httpService.post<UploadResult>('upload', formData, (progressEvent: ProgressEvent) => {
|
2022-04-15 14:24:30 +00:00
|
|
|
file.progress = progressEvent.loaded * 100 / progressEvent.total
|
|
|
|
})
|
|
|
|
|
|
|
|
file.status = 'Uploaded'
|
2022-06-10 10:47:46 +00:00
|
|
|
|
|
|
|
songStore.syncWithVault(result.song)
|
|
|
|
albumStore.syncWithVault(result.album)
|
|
|
|
overviewStore.refresh()
|
|
|
|
|
2022-04-15 14:24:30 +00:00
|
|
|
this.proceed() // upload the next file
|
2022-06-10 10:47:46 +00:00
|
|
|
|
2022-04-21 18:39:18 +00:00
|
|
|
window.setTimeout(() => this.remove(file), 1000)
|
2022-04-21 21:51:17 +00:00
|
|
|
} catch (error: any) {
|
2022-04-28 09:00:20 +00:00
|
|
|
console.error(error)
|
2022-04-21 18:39:18 +00:00
|
|
|
file.message = `Upload failed: ${error.response?.data?.message || 'Unknown error'}`
|
2022-04-15 14:24:30 +00:00
|
|
|
file.status = 'Errored'
|
|
|
|
this.proceed() // upload the next file
|
|
|
|
} finally {
|
|
|
|
this.checkUploadQueueStatus()
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2022-04-21 18:39:18 +00:00
|
|
|
retry (file: UploadFile) {
|
2022-04-15 14:24:30 +00:00
|
|
|
// simply reset the status and wait for the next process
|
|
|
|
this.resetFile(file)
|
|
|
|
this.proceed()
|
|
|
|
},
|
|
|
|
|
2022-04-21 18:39:18 +00:00
|
|
|
retryAll () {
|
2022-04-15 14:24:30 +00:00
|
|
|
this.state.files.forEach(this.resetFile)
|
|
|
|
this.proceed()
|
|
|
|
},
|
|
|
|
|
2022-04-21 18:39:18 +00:00
|
|
|
resetFile: (file: UploadFile) => {
|
2022-04-15 14:24:30 +00:00
|
|
|
file.status = 'Ready'
|
|
|
|
file.progress = 0
|
|
|
|
},
|
|
|
|
|
2022-04-21 18:39:18 +00:00
|
|
|
clear () {
|
2022-04-15 14:24:30 +00:00
|
|
|
this.state.files = []
|
|
|
|
},
|
|
|
|
|
2022-04-21 18:39:18 +00:00
|
|
|
removeFailed () {
|
2022-04-15 14:24:30 +00:00
|
|
|
this.state.files = this.state.files.filter(file => file.status !== 'Errored')
|
|
|
|
},
|
|
|
|
|
2022-04-21 18:39:18 +00:00
|
|
|
checkUploadQueueStatus () {
|
2022-04-15 14:24:30 +00:00
|
|
|
const uploadingFiles = this.state.files.filter(file => file.status === 'Uploading')
|
|
|
|
|
|
|
|
if (uploadingFiles.length === 0) {
|
|
|
|
eventBus.emit('UPLOAD_QUEUE_FINISHED')
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2022-04-21 18:39:18 +00:00
|
|
|
getFilesByStatus (status: UploadStatus) {
|
2022-04-15 14:24:30 +00:00
|
|
|
return this.state.files.filter(file => file.status === status)
|
|
|
|
}
|
|
|
|
}
|