ffplayout/stores/media.ts
2023-09-08 10:16:34 +02:00

82 lines
3.0 KiB
TypeScript

import { defineStore } from 'pinia'
import { useAuth } from '~/stores/auth'
import { useConfig } from '~/stores/config'
import { useIndex } from '~/stores/index'
const { genUID } = playlistOperations()
export const useMedia = defineStore('media', {
state: () => ({
currentPath: '',
crumbs: [] as Crumb[],
folderTree: {} as FileFolderObject,
folderList: {} as FolderObject,
folderCrumbs: [] as Crumb[],
}),
getters: {},
actions: {
async getTree(path: string, foldersOnly: boolean = false) {
const authStore = useAuth()
const configStore = useConfig()
const indexStore = useIndex()
const contentType = { 'content-type': 'application/json;charset=UTF-8' }
const channel = configStore.configGui[configStore.configID].id
const crumbs: Crumb[] = []
let root = '/'
await fetch(`/api/file/${channel}/browse/`, {
method: 'POST',
headers: { ...contentType, ...authStore.authHeader },
body: JSON.stringify({ source: path, folders_only: foldersOnly }),
})
.then((response) => {
if (response.status === 200) {
return response.json()
} else {
indexStore.alertVariant = 'alert-danger'
indexStore.alertMsg = `Storage not exist!`
indexStore.showAlert = true
return {
source: '',
parent: '',
folders: [],
files: [],
}
}
})
.then((data) => {
const pathStr = 'Home/' + data.source
const pathArr = pathStr.split('/')
if (path && path !== '/') {
for (const crumb of pathArr) {
if (crumb === 'Home') {
crumbs.push({ text: crumb, path: root })
} else if (crumb) {
root += crumb + '/'
crumbs.push({ text: crumb, path: root })
}
}
} else {
crumbs.push({ text: 'Home', path: '' })
}
if (foldersOnly) {
this.folderCrumbs = crumbs
data.folders = data.folders.map((i: any) => ({uid: genUID(), name: i}))
this.folderList = data
} else {
this.currentPath = path
this.crumbs = crumbs
data.folders = data.folders.map((i: any) => ({uid: genUID(), name: i}))
this.folderTree = data
}
})
}
},
})