diff --git a/app/Http/Controllers/AlbumsController.php b/app/Http/Controllers/AlbumsController.php index 02455ae9..eed18935 100644 --- a/app/Http/Controllers/AlbumsController.php +++ b/app/Http/Controllers/AlbumsController.php @@ -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($album) && in_array($formatName, Track::$LosslessFormats)) { + App::abort(404); + } + ResourceLogItem::logItem('album', $id, ResourceLogItem::DOWNLOAD, $format['index']); $downloader = new AlbumDownloader($album, $formatName); $downloader->download(); diff --git a/app/Http/Controllers/PlaylistsController.php b/app/Http/Controllers/PlaylistsController.php index 275f08ff..be1e1d58 100644 --- a/app/Http/Controllers/PlaylistsController.php +++ b/app/Http/Controllers/PlaylistsController.php @@ -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($playlist) && in_array($formatName, Track::$LosslessFormats)) { + App::abort(404); + } + ResourceLogItem::logItem('playlist', $id, ResourceLogItem::DOWNLOAD, $format['index']); $downloader = new PlaylistDownloader($playlist, $formatName); $downloader->download(); diff --git a/app/Models/Album.php b/app/Models/Album.php index 7378ebfc..61ee75f7 100644 --- a/app/Models/Album.php +++ b/app/Models/Album.php @@ -134,6 +134,18 @@ class Album extends Model implements Searchable, Commentable, Favouritable return $this->morphMany(Activity::class, 'resource'); } + public static function hasLosslessTracks(Album $album) + { + $hasLosslessTracks = false; + foreach ($album->tracks as $track) { + if (!$track->isMasterLossy()) { + $hasLosslessTracks = true; + break; + } + } + return $hasLosslessTracks; + } + public static function mapPublicAlbumShow(Album $album) { $tracks = []; @@ -142,7 +154,12 @@ class Album extends Model implements Searchable, Commentable, Favouritable } $formats = []; + $hasLosslessTracks = Album::hasLosslessTracks($album); foreach (Track::$Formats as $name => $format) { + if (!$hasLosslessTracks && in_array($name, Track::$LosslessFormats)) { + continue; + } + $formats[] = [ 'name' => $name, 'extension' => $format['extension'], @@ -151,7 +168,7 @@ class Album extends Model implements Searchable, Commentable, Favouritable 'isCacheable' => (in_array($name, Track::$CacheableFormats) ? true : false) ]; } - + $comments = []; foreach ($album->comments as $comment) { $comments[] = Comment::mapPublic($comment); diff --git a/app/Models/Playlist.php b/app/Models/Playlist.php index eda544f5..809ad143 100644 --- a/app/Models/Playlist.php +++ b/app/Models/Playlist.php @@ -107,6 +107,18 @@ class Playlist extends Model implements Searchable, Commentable, Favouritable return !$query; } + public static function hasLosslessTracks(Playlist $playlist) + { + $hasLosslessTracks = false; + foreach ($playlist->tracks as $track) { + if (!$track->isMasterLossy()) { + $hasLosslessTracks = true; + break; + } + } + return $hasLosslessTracks; + } + public static function mapPublicPlaylistShow(Playlist $playlist) { $tracks = []; @@ -117,7 +129,12 @@ class Playlist extends Model implements Searchable, Commentable, Favouritable } $formats = []; + $hasLosslessTracks = Playlist::hasLosslessTracks($playlist); foreach (Track::$Formats as $name => $format) { + if (!$hasLosslessTracks && in_array($name, Track::$LosslessFormats)) { + continue; + } + $formats[] = [ 'name' => $name, 'extension' => $format['extension'], diff --git a/app/Models/Track.php b/app/Models/Track.php index da9ff545..528816df 100644 --- a/app/Models/Track.php +++ b/app/Models/Track.php @@ -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,16 @@ class Track extends Model implements Searchable, Commentable, Favouritable return $this->published_at != null && $this->deleted_at == null; } + public function isMasterLossy() + { + return is_null( + $this->trackFiles->where('is_master', true) + ->first(function ($key, $trackFile) { + return in_array($trackFile->format, Track::$LosslessFormats); + }) + ); + } + public function getCoverUrl($type = Image::NORMAL) { if (!$this->hasCover()) {