2024-01-04 11:35:36 +00:00
|
|
|
import { computed } from 'vue'
|
|
|
|
import { commonStore } from '@/stores'
|
2022-10-08 10:54:25 +00:00
|
|
|
import { acceptedMediaTypes } from '@/config'
|
2024-10-13 17:37:01 +00:00
|
|
|
import type { UploadFile } from '@/services'
|
|
|
|
import { uploadService } from '@/services'
|
2022-11-18 18:44:20 +00:00
|
|
|
import { getAllFileEntries, pluralize } from '@/utils'
|
2024-04-23 21:01:27 +00:00
|
|
|
import { useMessageToaster, usePolicies, useRouter } from '@/composables'
|
2022-09-12 11:11:56 +00:00
|
|
|
|
|
|
|
export const useUpload = () => {
|
2022-11-18 17:45:38 +00:00
|
|
|
const { toastSuccess, toastWarning } = useMessageToaster()
|
2022-12-02 16:17:37 +00:00
|
|
|
const { go, isCurrentScreen } = useRouter()
|
2022-10-08 10:54:25 +00:00
|
|
|
|
2024-04-04 22:20:42 +00:00
|
|
|
const { currentUserCan } = usePolicies()
|
2024-01-05 16:42:50 +00:00
|
|
|
|
2024-02-09 15:45:38 +00:00
|
|
|
const mediaPathSetUp = computed(() => {
|
|
|
|
return commonStore.state.storage_driver !== 'local' || commonStore.state.media_path_set
|
|
|
|
})
|
|
|
|
|
2024-09-05 10:39:19 +00:00
|
|
|
const allowsUpload = computed(() => currentUserCan.uploadSongs())
|
2022-09-12 11:11:56 +00:00
|
|
|
|
|
|
|
const fileEntryToFile = async (entry: FileSystemEntry) => new Promise<File>(resolve => entry.file(resolve))
|
|
|
|
|
|
|
|
const queueFilesForUpload = (files: Array<File>) => {
|
|
|
|
const uploadCandidates = files
|
2024-01-24 22:39:47 +00:00
|
|
|
.filter(({ type }) => acceptedMediaTypes.includes(type))
|
2022-09-12 11:11:56 +00:00
|
|
|
.map((file): UploadFile => ({
|
|
|
|
file,
|
|
|
|
id: `${file.name}-${file.size}`, // for simplicity, a file's identity is determined by its name and size
|
|
|
|
status: 'Ready',
|
|
|
|
name: file.name,
|
2024-10-13 17:37:01 +00:00
|
|
|
progress: 0,
|
2022-09-12 11:11:56 +00:00
|
|
|
}))
|
|
|
|
|
|
|
|
uploadService.queue(uploadCandidates)
|
|
|
|
|
|
|
|
return uploadCandidates
|
|
|
|
}
|
|
|
|
|
|
|
|
const handleDropEvent = async (event: DragEvent) => {
|
|
|
|
if (!event.dataTransfer) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
const fileEntries = await getAllFileEntries(event.dataTransfer.items)
|
2023-04-16 22:00:27 +00:00
|
|
|
const files = await Promise.all(fileEntries.map(entry => fileEntryToFile(entry)))
|
2022-09-12 11:11:56 +00:00
|
|
|
const queuedFiles = queueFilesForUpload(files)
|
|
|
|
|
|
|
|
if (queuedFiles.length) {
|
2022-11-18 17:45:38 +00:00
|
|
|
toastSuccess(`Queued ${pluralize(queuedFiles, 'file')} for upload`)
|
2022-12-02 16:17:37 +00:00
|
|
|
isCurrentScreen('Upload') || go('upload')
|
2022-09-12 11:11:56 +00:00
|
|
|
} else {
|
2022-11-18 17:45:38 +00:00
|
|
|
toastWarning('No files applicable for upload')
|
2022-09-12 11:11:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
mediaPathSetUp,
|
|
|
|
allowsUpload,
|
|
|
|
handleDropEvent,
|
2024-10-13 17:37:01 +00:00
|
|
|
queueFilesForUpload,
|
2022-09-12 11:11:56 +00:00
|
|
|
}
|
|
|
|
}
|