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