2022-04-20 09:37:22 +00:00
|
|
|
import { favoriteStore, playlistStore } from '@/stores'
|
2022-04-24 08:50:45 +00:00
|
|
|
import { authService } from '.'
|
2022-04-20 09:37:22 +00:00
|
|
|
import { alerts, arrayify } from '@/utils'
|
2022-04-15 14:24:30 +00:00
|
|
|
|
2022-04-24 08:50:45 +00:00
|
|
|
export const downloadService = {
|
2022-04-15 14:24:30 +00:00
|
|
|
fromSongs (songs: Song | Song[]): void {
|
2022-04-20 09:37:22 +00:00
|
|
|
const query = arrayify(songs).reduce((q, song) => `songs[]=${song.id}&${q}`, '')
|
2022-04-15 14:24:30 +00:00
|
|
|
this.trigger(`songs?${query}`)
|
|
|
|
},
|
|
|
|
|
|
|
|
fromAlbum (album: Album): void {
|
|
|
|
this.trigger(`album/${album.id}`)
|
|
|
|
},
|
|
|
|
|
|
|
|
fromArtist (artist: Artist): void {
|
|
|
|
this.trigger(`artist/${artist.id}`)
|
|
|
|
},
|
|
|
|
|
|
|
|
fromPlaylist (playlist: Playlist): void {
|
|
|
|
if (playlistStore.getSongs(playlist).length) {
|
|
|
|
this.trigger(`playlist/${playlist.id}`)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
fromFavorites (): void {
|
|
|
|
if (favoriteStore.all.length) {
|
|
|
|
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-04-24 08:50:45 +00:00
|
|
|
const url = `${window.BASE_URL}download/${uri}${sep}api_token=${authService.getToken()}`
|
2022-04-15 14:24:30 +00:00
|
|
|
|
|
|
|
if (KOEL_ENV === 'app') {
|
|
|
|
require('electron').ipcRenderer.send('DOWNLOAD', url)
|
|
|
|
alerts.success('Download started!')
|
|
|
|
} else {
|
|
|
|
const iframe = document.createElement('iframe')
|
|
|
|
iframe.style.display = 'none'
|
|
|
|
iframe.setAttribute('src', url)
|
|
|
|
document.body.appendChild(iframe)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|