ffplayout/frontend/stores/media.ts

84 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) {
if (!foldersOnly) {
this.isLoading = true
}
const { $i18n } = useNuxtApp()
2023-05-15 08:08:40 -04:00
const authStore = useAuth()
const configStore = useConfig()
const indexStore = useIndex()
const channel = configStore.channels[configStore.i].id
2023-01-11 04:54:25 -05:00
const crumbs: Crumb[] = []
let root = '/'
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 },
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 {
indexStore.msgAlert('error', $i18n.t('media.notExists'), 3)
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
}
if (foldersOnly) {
this.folderCrumbs = crumbs
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
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
2024-04-08 11:07:03 -04:00
},
2023-01-11 04:54:25 -05:00
},
})