#7: Implement CR changes

This commit is contained in:
Kelvin 2016-07-11 11:25:45 +01:00
parent 05c66ca5dd
commit 2c3ef557af
7 changed files with 36 additions and 35 deletions

View file

@ -81,7 +81,7 @@ class AlbumsController extends Controller
App::abort(404); App::abort(404);
} }
if (!Album::hasLosslessTracks($album) && in_array($formatName, Track::$LosslessFormats)) { if (!$album->hasLosslessTracksOnly() && in_array($formatName, Track::$LosslessFormats)) {
App::abort(404); App::abort(404);
} }

View file

@ -82,7 +82,7 @@ class PlaylistsController extends Controller
App::abort(404); App::abort(404);
} }
if (!Playlist::hasLosslessTracks($playlist) && in_array($formatName, Track::$LosslessFormats)) { if (!$this->hasLosslessTracksOnly() && in_array($formatName, Track::$LosslessFormats)) {
App::abort(404); App::abort(404);
} }

View file

@ -134,18 +134,6 @@ class Album extends Model implements Searchable, Commentable, Favouritable
return $this->morphMany(Activity::class, 'resource'); 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) public static function mapPublicAlbumShow(Album $album)
{ {
$tracks = []; $tracks = [];
@ -154,7 +142,7 @@ class Album extends Model implements Searchable, Commentable, Favouritable
} }
$formats = []; $formats = [];
$hasLosslessTracks = Album::hasLosslessTracks($album); $hasLosslessTracks = $album->hasLosslessTracksOnly();
foreach (Track::$Formats as $name => $format) { foreach (Track::$Formats as $name => $format) {
if (!$hasLosslessTracks && in_array($name, Track::$LosslessFormats)) { if (!$hasLosslessTracks && in_array($name, Track::$LosslessFormats)) {
continue; continue;

View file

@ -107,18 +107,6 @@ class Playlist extends Model implements Searchable, Commentable, Favouritable
return !$query; 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) public static function mapPublicPlaylistShow(Playlist $playlist)
{ {
$tracks = []; $tracks = [];
@ -129,7 +117,7 @@ class Playlist extends Model implements Searchable, Commentable, Favouritable
} }
$formats = []; $formats = [];
$hasLosslessTracks = Playlist::hasLosslessTracks($playlist); $hasLosslessTracks = $playlist->hasLosslessTracksOnly();
foreach (Track::$Formats as $name => $format) { foreach (Track::$Formats as $name => $format) {
if (!$hasLosslessTracks && in_array($name, Track::$LosslessFormats)) { if (!$hasLosslessTracks && in_array($name, Track::$LosslessFormats)) {
continue; continue;

View file

@ -633,14 +633,15 @@ class Track extends Model implements Searchable, Commentable, Favouritable
return $this->published_at != null && $this->deleted_at == null; return $this->published_at != null && $this->deleted_at == null;
} }
public function isMasterLossy()
protected function getMasterTrackFile() : TrackFile
{ {
return is_null( return $this->trackFiles->where('is_master', true)->first();
$this->trackFiles->where('is_master', true) }
->first(function ($key, $trackFile) {
return in_array($trackFile->format, Track::$LosslessFormats); public function isMasterLossy() : bool
}) {
); return $this->getMasterTrackFile()->isLossy();
} }
public function getCoverUrl($type = Image::NORMAL) public function getCoverUrl($type = Image::NORMAL)

View file

@ -185,4 +185,9 @@ class TrackFile extends Model
return $this->filesize; return $this->filesize;
} }
public function isLossy() : bool
{
return !in_array($this->format, Track::$LosslessFormats);
}
} }

View file

@ -24,6 +24,7 @@ namespace Poniverse\Ponyfm\Traits;
use File; use File;
use Illuminate\Database\Eloquent\Collection; use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\Relation; use Illuminate\Database\Eloquent\Relations\Relation;
use Poniverse\Ponyfm\Jobs\EncodeTrackFile; use Poniverse\Ponyfm\Jobs\EncodeTrackFile;
use Poniverse\Ponyfm\Models\TrackFile; use Poniverse\Ponyfm\Models\TrackFile;
@ -123,4 +124,22 @@ trait TrackCollection
} }
])->where('format', $format)->get(); ])->where('format', $format)->get();
} }
/**
* 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 = false;
foreach ($this->tracks as $track) {
if (!$track->isMasterLossy()) {
$hasLosslessTracksOnly = true;
break;
}
}
return $hasLosslessTracksOnly;
}
} }