diff --git a/app/Album.php b/app/Album.php index 9c5d2bc3..26858b76 100644 --- a/app/Album.php +++ b/app/Album.php @@ -26,10 +26,10 @@ use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\ModelNotFoundException; use Illuminate\Database\Eloquent\SoftDeletes; use Illuminate\Foundation\Bus\DispatchesJobs; -use Illuminate\Support\Facades\Auth; -use Illuminate\Support\Facades\Cache; -use Illuminate\Support\Facades\File; -use Illuminate\Support\Facades\URL; +use Auth; +use Cache; +use File; +use URL; use Poniverse\Ponyfm\Jobs\EncodeTrackFile; use Poniverse\Ponyfm\Traits\SlugTrait; @@ -90,7 +90,7 @@ class Album extends Model return $this->hasMany('Poniverse\Ponyfm\Comment')->orderBy('created_at', 'desc'); } - public static function mapPublicAlbumShow($album) + public static function mapPublicAlbumShow(Album $album) { $tracks = []; foreach ($album->tracks as $track) { @@ -136,7 +136,7 @@ class Album extends Model return $data; } - public static function mapPublicAlbumSummary($album) + public static function mapPublicAlbumSummary(Album $album) { $userData = [ 'stats' => [ @@ -211,10 +211,18 @@ class Album extends Model 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) { - $size += $track->getFilesize($format); + try { + $size += $track->getFilesize($format); + + } catch (TrackFileNotFoundException $e) { + // do nothing - this track won't be included in the download + } } } @@ -260,6 +268,8 @@ class Album extends Model $index = 1; foreach ($tracks as $track) { + /** @var $track Track */ + $track->track_number = $index; $index++; $track->updateTags(); @@ -287,6 +297,9 @@ class Album extends Model $cachedCount = 0; foreach ($this->tracks as $track) { + /** @var $track Track */ + + if ($track->is_downloadable == false) { continue; } @@ -308,6 +321,8 @@ class Album extends Model public function encodeCacheableTrackFiles($format) { foreach ($this->tracks as $track) { + /** @var $track Track */ + if ($track->is_downloadable == false) { continue; } @@ -363,7 +378,9 @@ class Album extends Model continue; } + /** @var $track Track */ $track = Track::find($trackId); + if ($track->album_id != null && $track->album_id != $this->id) { $albumsToFix[] = $track->album; } @@ -378,6 +395,8 @@ class Album extends Model } foreach ($tracksToRemove as $track) { + /** @var $track Track */ + $track->album_id = null; $track->track_number = null; $track->updateTags(); @@ -385,6 +404,8 @@ class Album extends Model } foreach ($albumsToFix as $album) { + /** @var $album Album */ + $album->updateTrackNumbers(); } diff --git a/app/Exceptions.php b/app/Exceptions.php new file mode 100644 index 00000000..94afa01b --- /dev/null +++ b/app/Exceptions.php @@ -0,0 +1,14 @@ +tracks as $track) { + /** @var $track Track */ + $tracks[] = Track::mapPublicTrackSummary($track); } @@ -95,7 +97,7 @@ class Playlist extends Model return $data; } - public static function mapPublicPlaylistSummary($playlist) + public static function mapPublicPlaylistSummary(Playlist $playlist) { $userData = [ 'stats' => [ @@ -222,6 +224,8 @@ class Playlist extends Model $cachedCount = 0; foreach ($this->tracks as $track) { + /** @var $track Track */ + if ($track->is_downloadable == false) { continue; } @@ -239,6 +243,8 @@ class Playlist extends Model public function encodeCacheableTrackFiles($format) { foreach ($this->tracks as $track) { + /** @var $track Track */ + if ($track->is_downloadable == false) { continue; } @@ -265,9 +271,16 @@ class Playlist extends Model 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) { - $size += $track->getFilesize($format); + try { + $size += $track->getFilesize($format); + + } catch (TrackFileNotFoundException $e) { + // do nothing - this track won't be included in the download + } } } diff --git a/app/Track.php b/app/Track.php index 7afa25b5..09604f20 100644 --- a/app/Track.php +++ b/app/Track.php @@ -24,6 +24,7 @@ use Auth; use Cache; use Config; use DB; +use Illuminate\Database\Eloquent\ModelNotFoundException; use Poniverse\Ponyfm\Traits\SlugTrait; use Exception; use External; @@ -214,7 +215,7 @@ class Track extends Model return $processed; } - public static function mapPublicTrackShow($track) + public static function mapPublicTrackShow(Track $track) { $returnValue = self::mapPublicTrackSummary($track); $returnValue['description'] = $track->description; @@ -261,7 +262,7 @@ class Track extends Model return $returnValue; } - public static function mapPublicTrackSummary($track) + public static function mapPublicTrackSummary(Track $track) { $userData = [ 'stats' => [ @@ -333,7 +334,7 @@ class Track extends Model ]; } - public static function mapPrivateTrackShow($track) + public static function mapPrivateTrackShow(Track $track) { $showSongs = []; foreach ($track->showSongs as $showSong) { @@ -354,7 +355,7 @@ class Track extends Model return $returnValue; } - public static function mapPrivateTrackSummary($track) + public static function mapPrivateTrackSummary(Track $track) { return [ 'id' => $track->id, @@ -438,9 +439,22 @@ class Track extends Model $this->updateHash(); } + /** + * Returns the size of this track's file in the given format. + * + * @param $formatName + * @return int filesize in bytes + * @throws TrackFileNotFoundException + */ public function getFilesize($formatName) { - return $this->trackFiles()->where('format', $formatName)->first()->filesize; + $trackFile = $this->trackFiles()->where('format', $formatName)->first(); + + if ($trackFile) { + return (int) $trackFile->filesize; + } else { + throw new TrackFileNotFoundException(); + } } public function canView($user) diff --git a/app/TrackFile.php b/app/TrackFile.php index d7a35855..46c67190 100644 --- a/app/TrackFile.php +++ b/app/TrackFile.php @@ -22,10 +22,9 @@ namespace Poniverse\Ponyfm; use Helpers; use Illuminate\Database\Eloquent\Model; -use Illuminate\Support\Facades\App; -use Illuminate\Support\Facades\Cache; -use Illuminate\Support\Facades\File; -use Illuminate\Support\Facades\URL; +use App; +use File; +use URL; class TrackFile extends Model { @@ -85,9 +84,9 @@ class TrackFile extends Model return URL::to('/t' . $this->track_id . '/dl.' . $this->extension); } - public function getSizeAttribute($value) + public function getSizeAttribute() { - return Helpers::formatBytes($this->getFilesize($this->getFile())); + return Helpers::formatBytes($this->getFilesize()); } public function getFormat() diff --git a/database/migrations/2015_10_26_192855_update_track_files_with_cache.php b/database/migrations/2015_10_26_192855_update_track_files_with_cache.php index 5e8c1b0d..5352f319 100644 --- a/database/migrations/2015_10_26_192855_update_track_files_with_cache.php +++ b/database/migrations/2015_10_26_192855_update_track_files_with_cache.php @@ -46,8 +46,8 @@ class UpdateTrackFilesWithCache extends Migration { Schema::table('track_files', function (Blueprint $table) { $table->dropColumn('is_cacheable'); - $table->dropColumn('expiration'); - $table->dropColumn('in_progress'); + $table->dropColumn('expires_at'); + $table->dropColumn('is_in_progress'); }); } }