mirror of
https://github.com/koel/koel
synced 2024-11-25 05:30:20 +00:00
40 lines
925 B
TypeScript
40 lines
925 B
TypeScript
import { secondsToHis } from '@/utils'
|
|
import { httpService } from '..'
|
|
|
|
export const albumInfoService = {
|
|
async fetch (album: Album): Promise<Album> {
|
|
if (!album.info) {
|
|
const info = await httpService.get<AlbumInfo|null>(`album/${album.id}/info`)
|
|
|
|
if (info) {
|
|
this.merge(album, info)
|
|
}
|
|
}
|
|
|
|
return album
|
|
},
|
|
|
|
/**
|
|
* Merge the (fetched) info into an album.
|
|
*/
|
|
merge: (album: Album, info: AlbumInfo): void => {
|
|
// Convert the duration into i:s
|
|
if (info.tracks) {
|
|
info.tracks.forEach(track => {
|
|
track.fmtLength = secondsToHis(track.length)
|
|
})
|
|
}
|
|
|
|
// If the album cover is not in a nice form, discard.
|
|
if (typeof info.image !== 'string') {
|
|
info.image = null
|
|
}
|
|
|
|
// Set the album cover on the client side to the retrieved image from server.
|
|
if (info.image) {
|
|
album.cover = info.image
|
|
}
|
|
|
|
album.info = info
|
|
}
|
|
}
|