mirror of
https://github.com/Poniverse/Pony.fm.git
synced 2024-11-22 04:58:01 +01:00
Merge pull request #94 from Poniverse/feature/lossy_download_tweaks
Add tweaks to lossy downloads, closes #7
This commit is contained in:
commit
5f60afbe8f
14 changed files with 200 additions and 70 deletions
|
@ -21,6 +21,7 @@
|
|||
namespace Poniverse\Ponyfm;
|
||||
|
||||
use Poniverse\Ponyfm\Models\Album;
|
||||
use Poniverse\Ponyfm\Models\Track;
|
||||
use ZipStream;
|
||||
|
||||
class AlbumDownloader
|
||||
|
@ -43,6 +44,11 @@ class AlbumDownloader
|
|||
|
||||
public function download()
|
||||
{
|
||||
// Check whether the format is lossless yet not all master files are lossless
|
||||
$isLosslessFormatWithLossyTracks = in_array($this->_format, Track::$LosslessFormats)
|
||||
&& !$this->_album->hasLosslessTracksOnly()
|
||||
&& $this->_album->hasLosslessTracks();
|
||||
|
||||
$zip = new ZipStream($this->_album->user->display_name.' - '.$this->_album->title.'.zip');
|
||||
$zip->setComment(
|
||||
'Album: '.$this->_album->title."\r\n".
|
||||
|
@ -69,8 +75,15 @@ class AlbumDownloader
|
|||
continue;
|
||||
}
|
||||
|
||||
$zip->addLargeFile($track->getFileFor($this->_format),
|
||||
$directory.$track->getDownloadFilenameFor($this->_format));
|
||||
if ($isLosslessFormatWithLossyTracks && $track->isMasterLossy()) {
|
||||
$masterFormatName = $track->getMasterFormatName();
|
||||
$zip->addLargeFile($track->getFileFor($masterFormatName),
|
||||
$directory . $track->getDownloadFilenameFor($masterFormatName));
|
||||
} else {
|
||||
$zip->addLargeFile($track->getFileFor($this->_format),
|
||||
$directory . $track->getDownloadFilenameFor($this->_format));
|
||||
}
|
||||
|
||||
$notes .=
|
||||
$track->track_number.'. '.$track->title."\r\n".
|
||||
$track->description."\r\n".
|
||||
|
|
|
@ -61,7 +61,7 @@ class AlbumsController extends Controller
|
|||
|
||||
public function getDownload($id, $extension)
|
||||
{
|
||||
$album = Album::with('tracks', 'user')->find($id);
|
||||
$album = Album::with('tracks', 'tracks.trackFiles', 'user')->find($id);
|
||||
if (!$album) {
|
||||
App::abort(404);
|
||||
}
|
||||
|
@ -81,6 +81,10 @@ class AlbumsController extends Controller
|
|||
App::abort(404);
|
||||
}
|
||||
|
||||
if (!$album->hasLosslessTracks() && in_array($formatName, Track::$LosslessFormats)) {
|
||||
App::abort(404);
|
||||
}
|
||||
|
||||
ResourceLogItem::logItem('album', $id, ResourceLogItem::DOWNLOAD, $format['index']);
|
||||
$downloader = new AlbumDownloader($album, $formatName);
|
||||
$downloader->download();
|
||||
|
|
|
@ -62,7 +62,7 @@ class PlaylistsController extends Controller
|
|||
|
||||
public function getDownload($id, $extension)
|
||||
{
|
||||
$playlist = Playlist::with('tracks', 'user', 'tracks.album')->find($id);
|
||||
$playlist = Playlist::with('tracks', 'tracks.trackFiles', 'user', 'tracks.album')->find($id);
|
||||
if (!$playlist || (!$playlist->is_public && !Auth::check()) || (!$playlist->is_public && ($playlist->user_id !== Auth::user()->id))) {
|
||||
App::abort(404);
|
||||
}
|
||||
|
@ -82,6 +82,10 @@ class PlaylistsController extends Controller
|
|||
App::abort(404);
|
||||
}
|
||||
|
||||
if (!$playlist->hasLosslessTracks() && in_array($formatName, Track::$LosslessFormats)) {
|
||||
App::abort(404);
|
||||
}
|
||||
|
||||
ResourceLogItem::logItem('playlist', $id, ResourceLogItem::DOWNLOAD, $format['index']);
|
||||
$downloader = new PlaylistDownloader($playlist, $formatName);
|
||||
$downloader->download();
|
||||
|
|
|
@ -144,12 +144,17 @@ class Album extends Model implements Searchable, Commentable, Favouritable
|
|||
|
||||
$formats = [];
|
||||
foreach (Track::$Formats as $name => $format) {
|
||||
if (in_array($name, Track::$LosslessFormats) && !$album->hasLosslessTracksOnly() && !$album->hasLosslessTracks()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$formats[] = [
|
||||
'name' => $name,
|
||||
'extension' => $format['extension'],
|
||||
'url' => $album->getDownloadUrl($name),
|
||||
'size' => Helpers::formatBytes($album->getFilesize($name)),
|
||||
'isCacheable' => (in_array($name, Track::$CacheableFormats) ? true : false)
|
||||
'isCacheable' => (in_array($name, Track::$CacheableFormats) ? true : false),
|
||||
'isMixedLosslessness' => (in_array($name, Track::$LosslessFormats) && !$album->hasLosslessTracksOnly() && $album->hasLosslessTracks())
|
||||
];
|
||||
}
|
||||
|
||||
|
@ -249,34 +254,6 @@ class Album extends Model implements Searchable, Commentable, Favouritable
|
|||
return action('AlbumsController@getDownload', ['id' => $this->id, 'extension' => Track::$Formats[$format]['extension']]);
|
||||
}
|
||||
|
||||
public function getFilesize($format)
|
||||
{
|
||||
$tracks = $this->tracks;
|
||||
if (!count($tracks)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return Cache::remember($this->getCacheKey('filesize-'.$format), 1440, function() use ($tracks, $format) {
|
||||
$size = 0;
|
||||
|
||||
foreach ($tracks as $track) {
|
||||
/** @var $track Track */
|
||||
|
||||
// Ensure that only downloadable tracks are added onto the file size
|
||||
if ($track->is_downloadable == 1) {
|
||||
try {
|
||||
$size += $track->getFilesize($format);
|
||||
|
||||
} catch (TrackFileNotFoundException $e) {
|
||||
// do nothing - this track won't be included in the download
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $size;
|
||||
});
|
||||
}
|
||||
|
||||
public function getCoverUrl($type = Image::NORMAL)
|
||||
{
|
||||
if (!$this->hasCover()) {
|
||||
|
|
|
@ -119,12 +119,17 @@ class Playlist extends Model implements Searchable, Commentable, Favouritable
|
|||
|
||||
$formats = [];
|
||||
foreach (Track::$Formats as $name => $format) {
|
||||
if (in_array($name, Track::$LosslessFormats) && !$playlist->hasLosslessTracksOnly() && !$playlist->hasLosslessTracks()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$formats[] = [
|
||||
'name' => $name,
|
||||
'extension' => $format['extension'],
|
||||
'url' => $playlist->getDownloadUrl($name),
|
||||
'size' => Helpers::formatBytes($playlist->getFilesize($name)),
|
||||
'isCacheable' => (in_array($name, Track::$CacheableFormats) ? true : false)
|
||||
'isCacheable' => (in_array($name, Track::$CacheableFormats) ? true : false),
|
||||
'isMixedLosslessness' => (in_array($name, Track::$LosslessFormats) && !$playlist->hasLosslessTracksOnly() && $playlist->hasLosslessTracks())
|
||||
];
|
||||
}
|
||||
|
||||
|
@ -268,33 +273,6 @@ class Playlist extends Model implements Searchable, Commentable, Favouritable
|
|||
return action('PlaylistsController@getDownload', ['id' => $this->id, 'format' => Track::$Formats[$format]['extension']]);
|
||||
}
|
||||
|
||||
public function getFilesize($format)
|
||||
{
|
||||
$tracks = $this->tracks;
|
||||
if (!count($tracks)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return Cache::remember($this->getCacheKey('filesize-'.$format), 1440, function() use ($tracks, $format) {
|
||||
$size = 0;
|
||||
foreach ($tracks as $track) {
|
||||
/** @var $track Track */
|
||||
|
||||
// Ensure that only downloadable tracks are added onto the file size
|
||||
if ($track->is_downloadable == 1) {
|
||||
try {
|
||||
$size += $track->getFilesize($format);
|
||||
|
||||
} catch (TrackFileNotFoundException $e) {
|
||||
// do nothing - this track won't be included in the download
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $size;
|
||||
});
|
||||
}
|
||||
|
||||
public function getCoverUrl($type = Image::NORMAL)
|
||||
{
|
||||
if ($this->tracks->count() == 0) {
|
||||
|
|
|
@ -205,6 +205,11 @@ class Track extends Model implements Searchable, Commentable, Favouritable
|
|||
'AAC'
|
||||
];
|
||||
|
||||
public static $LosslessFormats = [
|
||||
'FLAC',
|
||||
'ALAC'
|
||||
];
|
||||
|
||||
public static function summary()
|
||||
{
|
||||
return self::select('tracks.id', 'title', 'user_id', 'slug', 'is_vocal', 'is_explicit', 'created_at',
|
||||
|
@ -628,6 +633,22 @@ class Track extends Model implements Searchable, Commentable, Favouritable
|
|||
return $this->published_at != null && $this->deleted_at == null;
|
||||
}
|
||||
|
||||
|
||||
protected function getMasterTrackFile() : TrackFile
|
||||
{
|
||||
return $this->trackFiles->where('is_master', true)->first();
|
||||
}
|
||||
|
||||
public function getMasterFormatName() : string
|
||||
{
|
||||
return $this->getMasterTrackFile()->format;
|
||||
}
|
||||
|
||||
public function isMasterLossy() : bool
|
||||
{
|
||||
return $this->getMasterTrackFile()->isLossy();
|
||||
}
|
||||
|
||||
public function getCoverUrl($type = Image::NORMAL)
|
||||
{
|
||||
if (!$this->hasCover()) {
|
||||
|
|
|
@ -185,4 +185,9 @@ class TrackFile extends Model
|
|||
|
||||
return $this->filesize;
|
||||
}
|
||||
|
||||
public function isLossy() : bool
|
||||
{
|
||||
return !in_array($this->format, Track::$LosslessFormats);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -21,6 +21,7 @@
|
|||
namespace Poniverse\Ponyfm;
|
||||
|
||||
use Poniverse\Ponyfm\Models\Playlist;
|
||||
use Poniverse\Ponyfm\Models\Track;
|
||||
use ZipStream;
|
||||
|
||||
class PlaylistDownloader
|
||||
|
@ -43,6 +44,11 @@ class PlaylistDownloader
|
|||
|
||||
public function download()
|
||||
{
|
||||
// Check whether the format is lossless yet not all master files are lossless
|
||||
$isLosslessFormatWithLossyTracks = in_array($this->_format, Track::$LosslessFormats)
|
||||
&& !$this->_playlist->hasLosslessTracksOnly()
|
||||
&& $this->_playlist->hasLosslessTracks();
|
||||
|
||||
$zip = new ZipStream($this->_playlist->user->display_name.' - '.$this->_playlist->title.'.zip');
|
||||
$zip->setComment(
|
||||
'Playlist: '.$this->_playlist->title."\r\n".
|
||||
|
@ -69,8 +75,15 @@ class PlaylistDownloader
|
|||
continue;
|
||||
}
|
||||
|
||||
$trackTarget = $track->downloadDirectory.'/'.$track->getDownloadFilenameFor($this->_format);
|
||||
$zip->addLargeFile($track->getFileFor($this->_format), $trackTarget);
|
||||
if ($isLosslessFormatWithLossyTracks && $track->isMasterLossy()) {
|
||||
$masterFormatName = $track->getMasterFormatName();
|
||||
$trackTarget = $track->downloadDirectory . '/' . $track->getDownloadFilenameFor($masterFormatName);
|
||||
$zip->addLargeFile($track->getFileFor($masterFormatName), $trackTarget);
|
||||
} else {
|
||||
$trackTarget = $track->downloadDirectory . '/' . $track->getDownloadFilenameFor($this->_format);
|
||||
$zip->addLargeFile($track->getFileFor($this->_format), $trackTarget);
|
||||
}
|
||||
|
||||
$notes .=
|
||||
$index.'. '.$track->title."\r\n".
|
||||
$track->description."\r\n".
|
||||
|
|
|
@ -24,8 +24,11 @@ namespace Poniverse\Ponyfm\Traits;
|
|||
|
||||
use File;
|
||||
use Illuminate\Database\Eloquent\Collection;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\Relation;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Poniverse\Ponyfm\Jobs\EncodeTrackFile;
|
||||
use Poniverse\Ponyfm\Models\Track;
|
||||
use Poniverse\Ponyfm\Models\TrackFile;
|
||||
|
||||
|
||||
|
@ -123,4 +126,86 @@ trait TrackCollection
|
|||
}
|
||||
])->where('format', $format)->get();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a boolean based on whether at least one (@link TrackFile)
|
||||
* for this (@link TrackCollection)'s tracks has a lossless master file.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function hasLosslessTracks() : bool
|
||||
{
|
||||
$hasLosslessTracks = false;
|
||||
foreach ($this->tracks as $track) {
|
||||
if (!$track->isMasterLossy()) {
|
||||
$hasLosslessTracks = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return $hasLosslessTracks;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a boolean based on whether all (@link TrackFile)s
|
||||
* for this (@link TrackCollection)'s tracks have lossless master files.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function hasLosslessTracksOnly() : bool
|
||||
{
|
||||
$hasLosslessTracksOnly = true;
|
||||
foreach ($this->tracks as $track) {
|
||||
if ($track->isMasterLossy()) {
|
||||
$hasLosslessTracksOnly = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return $hasLosslessTracksOnly;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the filesize in bytes for a (@link Album) or (@link Playlist) based on a format.
|
||||
*
|
||||
* @param $format
|
||||
* @return int
|
||||
*/
|
||||
public function getFilesize($format) : int
|
||||
{
|
||||
$tracks = $this->tracks;
|
||||
if (!count($tracks)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return Cache::remember($this->getCacheKey('filesize-'.$format), 1440, function() use ($tracks, $format) {
|
||||
$size = 0;
|
||||
|
||||
// Check whether the format is lossless yet not all master files are lossless
|
||||
$isLosslessFormatWithLossyTracks = in_array($format, Track::$LosslessFormats)
|
||||
&& !$this->hasLosslessTracksOnly()
|
||||
&& $this->hasLosslessTracks();
|
||||
|
||||
foreach ($tracks as $track) {
|
||||
/** @var $track Track */
|
||||
|
||||
// Ensure that only downloadable tracks are added onto the file size
|
||||
if (!$track->is_downloadable) {
|
||||
continue;
|
||||
}
|
||||
|
||||
try {
|
||||
// Get the file size corresponding to the losslessness of the track master file and format specified
|
||||
if ($isLosslessFormatWithLossyTracks && $track->isMasterLossy()) {
|
||||
$size += $track->getFilesize($track->getMasterFormatName());
|
||||
} else {
|
||||
$size += $track->getFilesize($format);
|
||||
}
|
||||
|
||||
} catch (TrackFileNotFoundException $e) {
|
||||
// do nothing - this track won't be included in the download
|
||||
}
|
||||
}
|
||||
|
||||
return $size;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
@ -6,11 +6,11 @@
|
|||
</a>
|
||||
<ul class="dropdown-menu" ng-show="::album.is_downloadable == 1">
|
||||
<li ng-repeat="format in ::album.formats" ng-hide="isInProgress">
|
||||
<a target="_blank" ng-if="::!format.isCacheable" ng-href="{{::format.url}}">
|
||||
<a target="_blank" ng-if="::!format.isCacheable" ng-click="checkMixedLosslessness(format);" ng-href="{{::format.url}}">
|
||||
<span>{{::format.name}}</span>
|
||||
<small>({{::format.size}})</small>
|
||||
</a>
|
||||
<a ng-if="::format.isCacheable" ng-click="getCachedAlbum(album.id, format.name);" href="">
|
||||
<a ng-if="::format.isCacheable" ng-click="getCachedAlbum(album.id, format);" href="">
|
||||
<span>{{::format.name}}</span>
|
||||
<small>({{::format.size}})</small>
|
||||
</a>
|
||||
|
|
|
@ -0,0 +1,16 @@
|
|||
<div class="modal" tabindex="-1" role="dialog">
|
||||
<div class="modal-dialog">
|
||||
<div class="modal-content">
|
||||
<div class="modal-header" ng-show="title">
|
||||
<button type="button" class="close" ng-click="$hide()">×</button>
|
||||
<h4 class="modal-title">Information</h4>
|
||||
</div>
|
||||
<div class="modal-body">
|
||||
<p>Your download contains some tracks which are in lossy formats instead of your chosen lossless format ({{::format.name}}. This is because they were originally uploaded in a lossy format (e.g., MP3). As a result, these tracks have been replaced by their lossy files.</p>
|
||||
</div>
|
||||
<div class="modal-footer">
|
||||
<button type="button" class="btn btn-primary" ng-click="$hide()">OK</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
|
@ -6,11 +6,11 @@
|
|||
</a>
|
||||
<ul class="dropdown-menu">
|
||||
<li ng-repeat="format in ::playlist.formats" ng-hide="isInProgress">
|
||||
<a target="_blank" ng-if="::!format.isCacheable" ng-href="{{::format.url}}">
|
||||
<a target="_blank" ng-if="::!format.isCacheable" ng-click="checkMixedLosslessness(format);" ng-href="{{::format.url}}">
|
||||
<span>{{::format.name}}</span>
|
||||
<small>({{::format.size}})</small>
|
||||
</a>
|
||||
<a ng-if="::format.isCacheable" ng-click="getCachedPlaylist(playlist.id, format.name);" href="">
|
||||
<a ng-if="::format.isCacheable" ng-click="getCachedPlaylist(playlist.id, format);" href="">
|
||||
<span>{{::format.name}}</span>
|
||||
<small>({{::format.size}})</small>
|
||||
</a>
|
||||
|
|
|
@ -43,10 +43,16 @@ module.exports = angular.module('ponyfm').controller "album", [
|
|||
playlists.refreshOwned().done (lists) ->
|
||||
$scope.playlists.push list for list in lists
|
||||
|
||||
$scope.checkMixedLosslessness = (format) ->
|
||||
if format.isMixedLosslessness == true
|
||||
$scope.format = format
|
||||
$modal({scope: $scope, templateUrl: 'templates/partials/collection-mixed-losslessness-dialog.html', show: true})
|
||||
|
||||
|
||||
$scope.getCachedAlbum = (id, format) ->
|
||||
$scope.isInProgress = true
|
||||
|
||||
cachedAlbum.download('albums', id, format).then (response) ->
|
||||
cachedAlbum.download('albums', id, format.name).then (response) ->
|
||||
$scope.albumUrl = response
|
||||
if $scope.albumUrl == 'error'
|
||||
$scope.isInProgress = false
|
||||
|
@ -59,4 +65,5 @@ module.exports = angular.module('ponyfm').controller "album", [
|
|||
else
|
||||
$scope.isInProgress = false
|
||||
$window.open $scope.albumUrl
|
||||
$scope.checkMixedLosslessness(format)
|
||||
]
|
||||
|
|
|
@ -37,10 +37,16 @@ module.exports = angular.module('ponyfm').controller 'playlist', [
|
|||
controller: ['$scope', ($scope) -> $scope.playlist = playlist; $scope.close = () -> dialog.close()],
|
||||
show: true
|
||||
|
||||
$scope.checkMixedLosslessness = (format) ->
|
||||
if format.isMixedLosslessness == true
|
||||
$scope.format = format
|
||||
$modal({scope: $scope, templateUrl: 'templates/partials/collection-mixed-losslessness-dialog.html', show: true})
|
||||
|
||||
|
||||
$scope.getCachedPlaylist = (id, format) ->
|
||||
$scope.isInProgress = true
|
||||
|
||||
cachedPlaylist.download('playlists', id, format).then (response) ->
|
||||
cachedPlaylist.download('playlists', id, format.name).then (response) ->
|
||||
$scope.playlistUrl = response
|
||||
if $scope.playlistUrl == 'error'
|
||||
$scope.isInProgress = false
|
||||
|
@ -53,4 +59,5 @@ module.exports = angular.module('ponyfm').controller 'playlist', [
|
|||
else
|
||||
$scope.isInProgress = false
|
||||
$window.open $scope.playlistUrl
|
||||
$scope.checkMixedLosslessness(format)
|
||||
]
|
||||
|
|
Loading…
Reference in a new issue