From 3e4b50d7b145ba3261760b1af6fc3ea539a5a36d Mon Sep 17 00:00:00 2001 From: Jonas Mueller Date: Sat, 22 Feb 2020 09:11:03 +0100 Subject: [PATCH 1/3] Add Download link for album --- app/View/Songs/album.ctp | 1 + app/webroot/js/player-nav.js | 31 +++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/app/View/Songs/album.ctp b/app/View/Songs/album.ctp index ec238a5..5211746 100644 --- a/app/View/Songs/album.ctp +++ b/app/View/Songs/album.ctp @@ -12,6 +12,7 @@
  • +
  • diff --git a/app/webroot/js/player-nav.js b/app/webroot/js/player-nav.js index f16ed41..03d8fb8 100644 --- a/app/webroot/js/player-nav.js +++ b/app/webroot/js/player-nav.js @@ -31,6 +31,7 @@ var playAlbum = '.action-play-album'; var playTitleNext = '.action-play-next'; var playBandNext = '.action-artist-play-next'; var playAlbumNext = '.action-album-play-next'; +var downloadAlbum = '.action-download-album'; var playPlaylistNext = '.action-playlist-play-next'; var playTitleAfter = '.action-add-to-up-next'; var playBandAfter = '.action-add-artist-to-up-next'; @@ -237,6 +238,14 @@ function init() { player.addAll(songsManager.getAlbumSongs(band, album)); }); + $('#content').on('click', downloadAlbum, function(e) { + e.preventDefault(); + var band = $(this).parents('[data-band]').attr('data-band'); + var album = $(this).parents('[data-album]').attr('data-album'); + var songs = songsManager.getAlbumSongs(band, album); + downloadAll(songs.map(song => baseurl + song.url)); + }); + $('#content').on('click', shuffleAlbum, function(e) { e.preventDefault(); var band = $(this).parents('[data-band]').attr('data-band'); @@ -525,6 +534,28 @@ function toggleQueueList() { } } +function sleep(ms) { + return new Promise(resolve => setTimeout(resolve, ms)); +} + +async function downloadAll(urls) { + var link = document.createElement('a'); + + link.setAttribute('download', null); + link.style.display = 'none'; + link.classList.add("no-ajax"); + + document.body.appendChild(link); + + for (var i = 0; i < urls.length; i++) { + link.setAttribute('href', urls[i]); + link.click(); + await sleep(100); + } + + document.body.removeChild(link); +} + var test = []; for (var i = 0; i < 100; i++) { test.push({title:"element : " + (i+1)}); From e8704cc779702fc081143e5f7bb534d0024bc46a Mon Sep 17 00:00:00 2001 From: Jonas Mueller Date: Sat, 22 Feb 2020 12:06:44 +0100 Subject: [PATCH 2/3] Allow download of playlists --- app/Controller/SongsController.php | 7 ++++++- app/View/Playlists/index.ctp | 3 ++- app/webroot/js/SongsManager.js | 14 +++++++++++++- app/webroot/js/player-nav.js | 8 ++++++++ 4 files changed, 29 insertions(+), 3 deletions(-) diff --git a/app/Controller/SongsController.php b/app/Controller/SongsController.php index 1f6b6e8..bcf02f5 100755 --- a/app/Controller/SongsController.php +++ b/app/Controller/SongsController.php @@ -249,11 +249,16 @@ public function sync() { $this->viewClass = 'Json'; $this->SortComponent = $this->Components->load('Sort'); - $songs = $this->Song->find("all", array('fields' => array('id', 'album', 'artist', 'band', 'cover', 'title', 'disc', 'track_number', 'playtime'), 'order' => 'title')); + $songs = $this->Song->find("all", array( + 'fields' => array('id', 'album', 'artist', 'band', 'cover', 'title', 'disc', 'track_number', 'playtime'), + 'contain' => 'PlaylistMembership.playlist_id', + 'order' => 'title') + ); $songs = $this->SortComponent->sortByBand($songs); foreach ($songs as $k => &$song) { $song['Song']['url'] = $this->request->base . '/songs/download/' . $song['Song']['id']; $song['Song']['cover'] = $this->request->base.'/'.IMAGES_URL.(empty($song['Song']['cover']) ? "no-cover.png" : THUMBNAILS_DIR.'/'.$song['Song']['cover']); + $song['Song']['playlists'] = array_map(function ($e) { return intval($e['playlist_id']); }, $song['PlaylistMembership']); } $songs = Hash::extract($songs, '{n}.Song'); diff --git a/app/View/Playlists/index.ctp b/app/View/Playlists/index.ctp index 4910f16..cc089cf 100644 --- a/app/View/Playlists/index.ctp +++ b/app/View/Playlists/index.ctp @@ -88,6 +88,7 @@ @@ -179,4 +180,4 @@ - \ No newline at end of file + diff --git a/app/webroot/js/SongsManager.js b/app/webroot/js/SongsManager.js index 8f3e467..da9ca40 100644 --- a/app/webroot/js/SongsManager.js +++ b/app/webroot/js/SongsManager.js @@ -105,6 +105,18 @@ function SongsManager(baseurl, version) { return playlist; }; + this.getPlaylistSongs = function(playlist) { + var playlistSongs = []; + for(var i = 0; i < songs.length; i++) { + for(var j = 0; j < songs[i].playlists.length; j++) { + if(songs[i].playlists[j] == playlist) { + playlistSongs.push(songs[i]); + } + } + } + return playlistSongs; + }; + this.getBandSongs = function(band) { var bands = []; for(var i = 0; i < songs.length; i++) { @@ -159,4 +171,4 @@ function SongsManager(baseurl, version) { this.setPlaylist = function(songs) { playlist = songs; }; -} \ No newline at end of file +} diff --git a/app/webroot/js/player-nav.js b/app/webroot/js/player-nav.js index 03d8fb8..f23f55a 100644 --- a/app/webroot/js/player-nav.js +++ b/app/webroot/js/player-nav.js @@ -32,6 +32,7 @@ var playTitleNext = '.action-play-next'; var playBandNext = '.action-artist-play-next'; var playAlbumNext = '.action-album-play-next'; var downloadAlbum = '.action-download-album'; +var downloadPlaylist = '.action-download-playlist'; var playPlaylistNext = '.action-playlist-play-next'; var playTitleAfter = '.action-add-to-up-next'; var playBandAfter = '.action-add-artist-to-up-next'; @@ -246,6 +247,13 @@ function init() { downloadAll(songs.map(song => baseurl + song.url)); }); + $('#content').on('click', downloadPlaylist, function(e) { + e.preventDefault(); + var playlist = $(this).parents('[data-playlist]').attr('data-playlist'); + var songs = songsManager.getPlaylistSongs(playlist); + downloadAll(songs.map(song => baseurl + song.url)); + }); + $('#content').on('click', shuffleAlbum, function(e) { e.preventDefault(); var band = $(this).parents('[data-band]').attr('data-band'); From a7ae8513186961801c1f2b5bb616bf5a214636e9 Mon Sep 17 00:00:00 2001 From: Jonas Mueller Date: Sat, 22 Feb 2020 13:16:41 +0100 Subject: [PATCH 3/3] Increase timeout for download to 500ms --- app/webroot/js/player-nav.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/webroot/js/player-nav.js b/app/webroot/js/player-nav.js index f23f55a..8dab7c1 100644 --- a/app/webroot/js/player-nav.js +++ b/app/webroot/js/player-nav.js @@ -558,7 +558,7 @@ async function downloadAll(urls) { for (var i = 0; i < urls.length; i++) { link.setAttribute('href', urls[i]); link.click(); - await sleep(100); + await sleep(500); } document.body.removeChild(link);