Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Download links for album, playlist #383

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion app/Controller/SongsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down
3 changes: 2 additions & 1 deletion app/View/Playlists/index.ctp
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@
<ul class="dropdown-menu" role="menu" aria-labelledby="dropdownAlbum">
<li role="presentation"><a role="menuitem" tabindex="-1" href="#" class="action-playlist-play-next"><?php echo __('Play Next'); ?></a></li>
<li role="presentation"><a role="menuitem" tabindex="-1" href="#" class="action-add-playlist-to-up-next"><?php echo __('Add to Up Next'); ?></a></li>
<li role="presentation"><a role="menuitem" tabindex="-1" href="#" class="action-download-playlist"><?php echo __('Download'); ?></a></li>
</ul>
</span>
</small>
Expand Down Expand Up @@ -179,4 +180,4 @@
</div>
</div>
<?php endforeach; ?>
<?php endif; ?>
<?php endif; ?>
1 change: 1 addition & 0 deletions app/View/Songs/album.ctp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<li role="presentation"><a role="menuitem" tabindex="-1" href="#" class="action-album-play-next"><?php echo __('Play Next'); ?></a></li>
<li role="presentation"><a role="menuitem" tabindex="-1" href="#" class="action-add-album-to-up-next"><?php echo __('Add to Up Next'); ?></a></li>
<li role="presentation"><a role="menuitem" tabindex="-1" href="#" data-toggle="modal" data-type="album" data-target="#add-to" ><?php echo __('Add to...'); ?></a></li>
<li role="presentation"><a role="menuitem" tabindex="-1" href="#" class="action-download-album"><?php echo __('Download'); ?></a></li>
</ul>
</span>
</small>
Expand Down
14 changes: 13 additions & 1 deletion app/webroot/js/SongsManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -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++) {
Expand Down Expand Up @@ -159,4 +171,4 @@ function SongsManager(baseurl, version) {
this.setPlaylist = function(songs) {
playlist = songs;
};
}
}
39 changes: 39 additions & 0 deletions app/webroot/js/player-nav.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ 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 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';
Expand Down Expand Up @@ -237,6 +239,21 @@ 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', 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');
Expand Down Expand Up @@ -525,6 +542,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(500);
}

document.body.removeChild(link);
}

var test = [];
for (var i = 0; i < 100; i++) {
test.push({title:"element : " + (i+1)});
Expand Down