ffplayout/stores/media.ts

82 lines
3.1 KiB
TypeScript
Raw Normal View History

2023-01-11 04:54:25 -05:00
import { defineStore } from 'pinia'
const { genUID } = playlistOperations()
2023-01-11 04:54:25 -05:00
export const useMedia = defineStore('media', {
state: () => ({
currentPath: '',
crumbs: [] as Crumb[],
folderTree: {} as FileFolderObject,
folderList: {} as FolderObject,
folderCrumbs: [] as Crumb[],
2024-04-08 11:07:03 -04:00
isLoading: false,
2023-01-11 04:54:25 -05:00
}),
getters: {},
actions: {
async getTree(path: string, foldersOnly: boolean = false) {
2024-04-08 11:07:03 -04:00
this.isLoading = true
2023-05-15 08:08:40 -04:00
const authStore = useAuth()
const configStore = useConfig()
const indexStore = useIndex()
2023-01-11 04:54:25 -05:00
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/`, {
2023-01-11 04:54:25 -05:00
method: 'POST',
headers: { ...contentType, ...authStore.authHeader },
body: JSON.stringify({ source: path, folders_only: foldersOnly }),
2023-01-11 04:54:25 -05:00
})
.then((response) => {
if (response.status === 200) {
return response.json()
} else {
2024-04-04 17:28:25 -04:00
indexStore.msgAlert('alert-error', 'Storage not exist!', 3)
return {
source: '',
parent: '',
folders: [],
files: [],
}
}
})
2023-01-11 04:54:25 -05:00
.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
2024-04-07 17:27:52 -04:00
data.parent_folders = data.parent_folders.map((i: any) => ({uid: genUID(), name: i}))
data.folders = data.folders.map((i: any) => ({uid: genUID(), name: i}))
this.folderList = data
} else {
this.currentPath = path
this.crumbs = crumbs
2024-04-07 17:27:52 -04:00
data.parent_folders = data.parent_folders.map((i: any) => ({uid: genUID(), name: i}))
data.folders = data.folders.map((i: any) => ({uid: genUID(), name: i}))
this.folderTree = data
}
2023-01-11 04:54:25 -05:00
})
2024-04-08 11:07:03 -04:00
this.isLoading = false
},
2023-01-11 04:54:25 -05:00
},
})