New Backend changes
This commit is contained in:
142
composables/useFiles.ts
Normal file
142
composables/useFiles.ts
Normal file
@@ -0,0 +1,142 @@
|
||||
|
||||
export const useFiles = () => {
|
||||
const supabase = useSupabaseClient()
|
||||
const toast = useToast()
|
||||
|
||||
const auth = useAuthStore()
|
||||
|
||||
let bucket = "filesdev"
|
||||
|
||||
const uploadFiles = async (fileData, files,tags, upsert) => {
|
||||
const uploadSingleFile = async (file) => {
|
||||
//Create File Entry to Get ID for Folder
|
||||
|
||||
const formData = new FormData()
|
||||
|
||||
formData.append("file", file)
|
||||
formData.append("meta", JSON.stringify(fileData))
|
||||
|
||||
const {fileReturn} = await useNuxtApp().$api("/api/files/upload",{
|
||||
method: "POST",
|
||||
body: formData
|
||||
})
|
||||
}
|
||||
|
||||
if(files.length === 1) {
|
||||
await uploadSingleFile(files[0])
|
||||
} else if( files.length > 1) {
|
||||
|
||||
for(let i = 0; i < files.length; i++){
|
||||
await uploadSingleFile(files[i])
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
const selectDocuments = async (sortColumn = null, folder = null) => {
|
||||
let data = []
|
||||
data = await useEntities("files").select("*, incominginvoice(*), project(*), vendor(*), customer(*), contract(*), plant(*), createddocument(*), vehicle(*), product(*), profile(*), check(*), inventoryitem(*)")
|
||||
|
||||
|
||||
|
||||
const res = await useNuxtApp().$api("/api/files/presigned",{
|
||||
method: "POST",
|
||||
body: {
|
||||
ids: data.map(i => i.id)
|
||||
}
|
||||
})
|
||||
|
||||
console.log(res)
|
||||
|
||||
return res.files
|
||||
}
|
||||
|
||||
const selectSomeDocuments = async (documentIds, sortColumn = null, folder = null) => {
|
||||
|
||||
if(documentIds.length === 0) return []
|
||||
const res = await useNuxtApp().$api("/api/files/presigned",{
|
||||
method: "POST",
|
||||
body: {
|
||||
ids: documentIds
|
||||
}
|
||||
})
|
||||
|
||||
console.log(res)
|
||||
|
||||
return res.files
|
||||
|
||||
|
||||
}
|
||||
|
||||
const selectDocument = async (id) => {
|
||||
const {data,error} = await supabase
|
||||
.from("files")
|
||||
.select('*')
|
||||
.eq("id",id)
|
||||
.single()
|
||||
|
||||
const {data: supabaseData,error:supabaseError} = await supabase.storage.from(bucket).createSignedUrl(data.path,3600)
|
||||
|
||||
return {
|
||||
...data,
|
||||
url: supabaseData.signedUrl
|
||||
}
|
||||
/*
|
||||
if(data.length > 0){
|
||||
let paths = []
|
||||
data.forEach(doc => {
|
||||
paths.push(doc.path)
|
||||
})
|
||||
|
||||
const {data: supabaseData,error} = await supabase.storage.from(bucket).createSignedUrls(paths,3600)
|
||||
|
||||
data = data.map((doc,index) => {
|
||||
|
||||
return {
|
||||
...doc,
|
||||
url: supabaseData[index].signedUrl
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
//console.log(data)
|
||||
|
||||
return data[0]*/
|
||||
}
|
||||
|
||||
const downloadFile = async (id?: string, ids?: string[]) => {
|
||||
const url = id ? `/api/files/download/${id}` : `/api/files/download`
|
||||
const body = ids ? { ids } : undefined
|
||||
|
||||
const res:any = await useNuxtApp().$api.raw(url, {
|
||||
method: "POST",
|
||||
body,
|
||||
responseType: "blob", // wichtig!
|
||||
})
|
||||
|
||||
// Dateiname bestimmen
|
||||
let filename = "download"
|
||||
|
||||
if (id) {
|
||||
// Einzeldatei → nimm den letzten Teil des Pfads aus Content-Disposition
|
||||
const contentDisposition = res.headers?.get("content-disposition")
|
||||
if (contentDisposition) {
|
||||
const match = contentDisposition.match(/filename="?([^"]+)"?/)
|
||||
if (match) filename = match[1]
|
||||
}
|
||||
} else {
|
||||
filename = "dateien.zip"
|
||||
}
|
||||
|
||||
// Direkt speichern
|
||||
const blob = res._data as Blob
|
||||
const link = document.createElement("a")
|
||||
link.href = URL.createObjectURL(blob)
|
||||
link.download = filename
|
||||
link.click()
|
||||
URL.revokeObjectURL(link.href)
|
||||
}
|
||||
|
||||
|
||||
return {uploadFiles, selectDocuments, selectSomeDocuments, selectDocument, downloadFile}
|
||||
}
|
||||
Reference in New Issue
Block a user