2023-01-11 10:54:25 +01:00
|
|
|
import { defineStore } from 'pinia'
|
|
|
|
|
2023-09-08 10:16:34 +02:00
|
|
|
const { genUID } = playlistOperations()
|
|
|
|
|
2023-01-11 10:54:25 +01:00
|
|
|
export const useMedia = defineStore('media', {
|
|
|
|
state: () => ({
|
|
|
|
currentPath: '',
|
|
|
|
crumbs: [] as Crumb[],
|
2023-01-22 20:04:57 +01:00
|
|
|
folderTree: {} as FileFolderObject,
|
2023-01-25 08:54:30 +01:00
|
|
|
folderList: {} as FolderObject,
|
2023-01-22 20:04:57 +01:00
|
|
|
folderCrumbs: [] as Crumb[],
|
2024-04-08 17:07:03 +02:00
|
|
|
isLoading: false,
|
2023-01-11 10:54:25 +01:00
|
|
|
}),
|
|
|
|
|
|
|
|
getters: {},
|
|
|
|
actions: {
|
2023-01-22 20:04:57 +01:00
|
|
|
async getTree(path: string, foldersOnly: boolean = false) {
|
2024-04-09 15:45:17 +02:00
|
|
|
if (!foldersOnly) {
|
|
|
|
this.isLoading = true
|
|
|
|
}
|
|
|
|
|
2024-05-27 12:01:31 +02:00
|
|
|
const { $i18n } = useNuxtApp()
|
2023-05-15 14:08:40 +02:00
|
|
|
const authStore = useAuth()
|
|
|
|
const configStore = useConfig()
|
2023-07-14 13:45:48 +02:00
|
|
|
const indexStore = useIndex()
|
2024-06-12 09:17:44 +02:00
|
|
|
const channel = configStore.configChannel[configStore.configID].id
|
2023-01-11 10:54:25 +01:00
|
|
|
const crumbs: Crumb[] = []
|
|
|
|
let root = '/'
|
|
|
|
|
2023-01-11 13:41:22 +01:00
|
|
|
await fetch(`/api/file/${channel}/browse/`, {
|
2023-01-11 10:54:25 +01:00
|
|
|
method: 'POST',
|
2024-04-16 14:07:50 +02:00
|
|
|
headers: { ...configStore.contentType, ...authStore.authHeader },
|
2023-01-22 20:04:57 +01:00
|
|
|
body: JSON.stringify({ source: path, folders_only: foldersOnly }),
|
2023-01-11 10:54:25 +01:00
|
|
|
})
|
2023-07-14 13:45:48 +02:00
|
|
|
.then((response) => {
|
|
|
|
if (response.status === 200) {
|
|
|
|
return response.json()
|
|
|
|
} else {
|
2024-05-27 12:01:31 +02:00
|
|
|
indexStore.msgAlert('error', $i18n.t('media.notExists'), 3)
|
2023-07-14 13:45:48 +02:00
|
|
|
|
|
|
|
return {
|
|
|
|
source: '',
|
|
|
|
parent: '',
|
|
|
|
folders: [],
|
|
|
|
files: [],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2023-01-11 10:54:25 +01:00
|
|
|
.then((data) => {
|
2024-04-14 00:01:45 +02:00
|
|
|
const pathStr = `${data.parent}/` + data.source
|
2023-01-11 10:54:25 +01:00
|
|
|
const pathArr = pathStr.split('/')
|
|
|
|
|
|
|
|
if (path && path !== '/') {
|
|
|
|
for (const crumb of pathArr) {
|
2024-04-14 00:01:45 +02:00
|
|
|
if (crumb === data.parent) {
|
2023-01-11 10:54:25 +01:00
|
|
|
crumbs.push({ text: crumb, path: root })
|
|
|
|
} else if (crumb) {
|
|
|
|
root += crumb + '/'
|
|
|
|
crumbs.push({ text: crumb, path: root })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2024-04-14 00:01:45 +02:00
|
|
|
crumbs.push({ text: data.parent, path: '' })
|
2023-01-11 10:54:25 +01:00
|
|
|
}
|
|
|
|
|
2023-01-22 20:04:57 +01:00
|
|
|
if (foldersOnly) {
|
|
|
|
this.folderCrumbs = crumbs
|
2024-05-27 12:01:31 +02:00
|
|
|
data.parent_folders = data.parent_folders?.map((i: any) => ({ uid: genUID(), name: i })) ?? []
|
2024-04-09 15:45:17 +02:00
|
|
|
data.folders = data.folders.map((i: any) => ({ uid: genUID(), name: i }))
|
2023-01-22 20:04:57 +01:00
|
|
|
this.folderList = data
|
|
|
|
} else {
|
|
|
|
this.currentPath = path
|
|
|
|
this.crumbs = crumbs
|
2024-05-27 12:01:31 +02:00
|
|
|
data.parent_folders = data.parent_folders?.map((i: any) => ({ uid: genUID(), name: i })) ?? []
|
2024-04-09 15:45:17 +02:00
|
|
|
data.folders = data.folders.map((i: any) => ({ uid: genUID(), name: i }))
|
2023-01-22 20:04:57 +01:00
|
|
|
this.folderTree = data
|
|
|
|
}
|
2023-01-11 10:54:25 +01:00
|
|
|
})
|
2024-04-08 17:07:03 +02:00
|
|
|
|
2024-04-09 15:45:17 +02:00
|
|
|
this.isLoading = false
|
2024-04-08 17:07:03 +02:00
|
|
|
},
|
2023-01-11 10:54:25 +01:00
|
|
|
},
|
|
|
|
})
|