refactor: upload file

This commit is contained in:
puri-ph4tt 2023-11-28 09:22:44 +07:00 committed by Methapon2001
parent b10679b53c
commit 74c5115e95
No known key found for this signature in database
GPG key ID: 849924FEF46BD132
2 changed files with 71 additions and 10 deletions

View file

@ -238,15 +238,56 @@ export const useTreeDataStore = defineStore('changeCabinet', () => {
await getFolder(pathname.join('/') + '/')
}
async function uploadFile(
pathname: string,
file: File,
metadata: {
title: string
description: string
keyword: string[]
category: string[]
}
) {
loader.show()
const pathArray: string[] = pathname.split('/').filter(Boolean)
if (pathArray.length <= 2) return
let requestPath = 'cabinet'
if (pathArray.length >= 1) requestPath += `/${pathArray[0]}`
if (pathArray.length >= 2) requestPath += `/drawer/${pathArray[1]}`
if (pathArray.length >= 3) requestPath += `/folder/${pathArray[2]}`
if (pathArray.length >= 4) requestPath += `/subfolder/${pathArray[3]}`
requestPath += '/file'
const formData = new FormData()
formData.append('file', file)
formData.append('title', metadata.title)
formData.append('description', metadata.description)
formData.append('keyword', metadata.keyword.join(','))
formData.append('category', metadata.category.join(','))
await axiosClient.post(`${apiEndpoint}${requestPath}`, formData)
if (currentDept.value === 0) await getCabinet()
else await getFolder(currentPath.value)
return loader.hide()
}
return {
data,
currentFolder,
currentFile,
currentDept,
listDataFolder,
currentPath,
listDataFile,
listDataFolder,
getCabinet,
getFolder,
uploadFile,
gotoParent,
createFolder,
deleteFolder,