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

42 lines
1.1 KiB
TypeScript
Raw Normal View History

2022-06-10 12:47:46 +02:00
import { favoriteStore } from '@/stores'
2022-05-14 20:49:45 +02:00
import { authService } from '@/services'
import { arrayify } from '@/utils'
2022-04-15 16:24:30 +02:00
2022-04-24 11:50:45 +03:00
export const downloadService = {
2024-05-19 13:49:42 +08:00
fromPlayables (playables: MaybeArray<Playable>) {
const query = arrayify(playables).reduce((q, playable) => `songs[]=${playable.id}&${q}`, '')
2022-04-15 16:24:30 +02:00
this.trigger(`songs?${query}`)
},
2022-05-13 19:58:38 +02:00
fromAlbum (album: Album) {
2022-04-15 16:24:30 +02:00
this.trigger(`album/${album.id}`)
},
2022-05-13 19:58:38 +02:00
fromArtist (artist: Artist) {
2022-04-15 16:24:30 +02:00
this.trigger(`artist/${artist.id}`)
},
2022-05-13 19:58:38 +02:00
fromPlaylist (playlist: Playlist) {
2022-06-10 12:47:46 +02:00
this.trigger(`playlist/${playlist.id}`)
2022-04-15 16:24:30 +02:00
},
2022-05-13 19:58:38 +02:00
fromFavorites () {
if (favoriteStore.state.playables.length) {
2022-04-15 16:24:30 +02:00
this.trigger('favorites')
}
},
/**
* Build a download link using a segment and trigger it.
*
* @param {string} uri The uri segment, corresponding to the song(s),
* artist, playlist, or album.
*/
trigger: (uri: string) => {
const sep = uri.includes('?') ? '&' : '?'
2022-11-16 18:57:38 +01:00
const url = `${window.BASE_URL}download/${uri}${sep}t=${authService.getAudioToken()}`
2022-04-15 16:24:30 +02:00
2024-05-19 13:49:42 +08:00
open(url)
2022-04-15 16:24:30 +02:00
}
}