koel/resources/assets/js/services/mediaInfoService.ts

34 lines
1 KiB
TypeScript
Raw Normal View History

import { cache, http } from '@/services'
2022-07-16 22:42:29 +00:00
import { albumStore, artistStore, songStore } from '@/stores'
export const mediaInfoService = {
async fetchForArtist (artist: Artist) {
2022-07-25 13:44:17 +00:00
artist = artistStore.syncWithVault(artist)[0]
const cacheKey = ['artist.info', artist.id]
2022-07-25 13:25:27 +00:00
if (cache.has(cacheKey)) return cache.get<ArtistInfo>(cacheKey)
const info = await http.get<ArtistInfo | null>(`artists/${artist.id}/information`)
2022-07-25 13:44:17 +00:00
info && cache.set(cacheKey, info)
info?.image && (artist.image = info.image)
2022-07-16 22:42:29 +00:00
return info
},
async fetchForAlbum (album: Album) {
2022-07-25 13:44:17 +00:00
album = albumStore.syncWithVault(album)[0]
const cacheKey = ['album.info', album.id]
2022-07-25 13:25:27 +00:00
if (cache.has(cacheKey)) return cache.get<AlbumInfo>(cacheKey)
const info = await http.get<AlbumInfo | null>(`albums/${album.id}/information`)
2022-07-25 13:25:27 +00:00
info && cache.set(cacheKey, info)
2022-07-16 22:42:29 +00:00
if (info?.cover) {
2022-07-25 13:44:17 +00:00
album.cover = info.cover
songStore.byAlbum(album).forEach(song => (song.album_cover = info.cover!))
2022-07-16 22:42:29 +00:00
}
return info
}
}