#4: Fixed playlists that have a mix of lossy and lossless master files, added a bunch of typehinting, and fixed the reversal of a migration.

This commit is contained in:
Peter Deltchev 2015-11-09 20:08:37 -08:00
parent 214880ec2c
commit 3456e6b499
6 changed files with 87 additions and 26 deletions

View file

@ -26,10 +26,10 @@ use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\ModelNotFoundException; use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Database\Eloquent\SoftDeletes; use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Foundation\Bus\DispatchesJobs; use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Support\Facades\Auth; use Auth;
use Illuminate\Support\Facades\Cache; use Cache;
use Illuminate\Support\Facades\File; use File;
use Illuminate\Support\Facades\URL; use URL;
use Poniverse\Ponyfm\Jobs\EncodeTrackFile; use Poniverse\Ponyfm\Jobs\EncodeTrackFile;
use Poniverse\Ponyfm\Traits\SlugTrait; use Poniverse\Ponyfm\Traits\SlugTrait;
@ -90,7 +90,7 @@ class Album extends Model
return $this->hasMany('Poniverse\Ponyfm\Comment')->orderBy('created_at', 'desc'); return $this->hasMany('Poniverse\Ponyfm\Comment')->orderBy('created_at', 'desc');
} }
public static function mapPublicAlbumShow($album) public static function mapPublicAlbumShow(Album $album)
{ {
$tracks = []; $tracks = [];
foreach ($album->tracks as $track) { foreach ($album->tracks as $track) {
@ -136,7 +136,7 @@ class Album extends Model
return $data; return $data;
} }
public static function mapPublicAlbumSummary($album) public static function mapPublicAlbumSummary(Album $album)
{ {
$userData = [ $userData = [
'stats' => [ 'stats' => [
@ -211,10 +211,18 @@ class Album extends Model
return Cache::remember($this->getCacheKey('filesize-' . $format), 1440, function () use ($tracks, $format) { return Cache::remember($this->getCacheKey('filesize-' . $format), 1440, function () use ($tracks, $format) {
$size = 0; $size = 0;
foreach ($tracks as $track) { foreach ($tracks as $track) {
/** @var $track Track */
// Ensure that only downloadable tracks are added onto the file size // Ensure that only downloadable tracks are added onto the file size
if ($track->is_downloadable == 1) { 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; $index = 1;
foreach ($tracks as $track) { foreach ($tracks as $track) {
/** @var $track Track */
$track->track_number = $index; $track->track_number = $index;
$index++; $index++;
$track->updateTags(); $track->updateTags();
@ -287,6 +297,9 @@ class Album extends Model
$cachedCount = 0; $cachedCount = 0;
foreach ($this->tracks as $track) { foreach ($this->tracks as $track) {
/** @var $track Track */
if ($track->is_downloadable == false) { if ($track->is_downloadable == false) {
continue; continue;
} }
@ -308,6 +321,8 @@ class Album extends Model
public function encodeCacheableTrackFiles($format) public function encodeCacheableTrackFiles($format)
{ {
foreach ($this->tracks as $track) { foreach ($this->tracks as $track) {
/** @var $track Track */
if ($track->is_downloadable == false) { if ($track->is_downloadable == false) {
continue; continue;
} }
@ -363,7 +378,9 @@ class Album extends Model
continue; continue;
} }
/** @var $track Track */
$track = Track::find($trackId); $track = Track::find($trackId);
if ($track->album_id != null && $track->album_id != $this->id) { if ($track->album_id != null && $track->album_id != $this->id) {
$albumsToFix[] = $track->album; $albumsToFix[] = $track->album;
} }
@ -378,6 +395,8 @@ class Album extends Model
} }
foreach ($tracksToRemove as $track) { foreach ($tracksToRemove as $track) {
/** @var $track Track */
$track->album_id = null; $track->album_id = null;
$track->track_number = null; $track->track_number = null;
$track->updateTags(); $track->updateTags();
@ -385,6 +404,8 @@ class Album extends Model
} }
foreach ($albumsToFix as $album) { foreach ($albumsToFix as $album) {
/** @var $album Album */
$album->updateTrackNumbers(); $album->updateTrackNumbers();
} }

14
app/Exceptions.php Normal file
View file

@ -0,0 +1,14 @@
<?php namespace Poniverse\Ponyfm;
use Illuminate\Database\Eloquent\ModelNotFoundException;
/**
* Class TrackFileNotFoundException
* @package Poniverse\Ponyfm
*
* This exception is used to indicate that the requested `TrackFile` object
* does not exist. This is useful when dealing with albums or playlists that
* contain tracks for which no lossless master is available (and thus, lossless
* `TrackFiles` don't exist for).
*/
class TrackFileNotFoundException extends ModelNotFoundException {}

View file

@ -26,9 +26,9 @@ use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\ModelNotFoundException; use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Database\Eloquent\SoftDeletes; use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Foundation\Bus\DispatchesJobs; use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Support\Facades\Auth; use Auth;
use Illuminate\Support\Facades\Cache; use Cache;
use Illuminate\Support\Facades\URL; use URL;
use Poniverse\Ponyfm\Jobs\EncodeTrackFile; use Poniverse\Ponyfm\Jobs\EncodeTrackFile;
use Poniverse\Ponyfm\Traits\SlugTrait; use Poniverse\Ponyfm\Traits\SlugTrait;
@ -59,10 +59,12 @@ class Playlist extends Model
return !$query; return !$query;
} }
public static function mapPublicPlaylistShow($playlist) public static function mapPublicPlaylistShow(Playlist $playlist)
{ {
$tracks = []; $tracks = [];
foreach ($playlist->tracks as $track) { foreach ($playlist->tracks as $track) {
/** @var $track Track */
$tracks[] = Track::mapPublicTrackSummary($track); $tracks[] = Track::mapPublicTrackSummary($track);
} }
@ -95,7 +97,7 @@ class Playlist extends Model
return $data; return $data;
} }
public static function mapPublicPlaylistSummary($playlist) public static function mapPublicPlaylistSummary(Playlist $playlist)
{ {
$userData = [ $userData = [
'stats' => [ 'stats' => [
@ -222,6 +224,8 @@ class Playlist extends Model
$cachedCount = 0; $cachedCount = 0;
foreach ($this->tracks as $track) { foreach ($this->tracks as $track) {
/** @var $track Track */
if ($track->is_downloadable == false) { if ($track->is_downloadable == false) {
continue; continue;
} }
@ -239,6 +243,8 @@ class Playlist extends Model
public function encodeCacheableTrackFiles($format) public function encodeCacheableTrackFiles($format)
{ {
foreach ($this->tracks as $track) { foreach ($this->tracks as $track) {
/** @var $track Track */
if ($track->is_downloadable == false) { if ($track->is_downloadable == false) {
continue; continue;
} }
@ -265,9 +271,16 @@ class Playlist extends Model
return Cache::remember($this->getCacheKey('filesize-' . $format), 1440, function () use ($tracks, $format) { return Cache::remember($this->getCacheKey('filesize-' . $format), 1440, function () use ($tracks, $format) {
$size = 0; $size = 0;
foreach ($tracks as $track) { foreach ($tracks as $track) {
/** @var $track Track */
// Ensure that only downloadable tracks are added onto the file size // Ensure that only downloadable tracks are added onto the file size
if ($track->is_downloadable == 1) { 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
}
} }
} }

View file

@ -24,6 +24,7 @@ use Auth;
use Cache; use Cache;
use Config; use Config;
use DB; use DB;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Poniverse\Ponyfm\Traits\SlugTrait; use Poniverse\Ponyfm\Traits\SlugTrait;
use Exception; use Exception;
use External; use External;
@ -214,7 +215,7 @@ class Track extends Model
return $processed; return $processed;
} }
public static function mapPublicTrackShow($track) public static function mapPublicTrackShow(Track $track)
{ {
$returnValue = self::mapPublicTrackSummary($track); $returnValue = self::mapPublicTrackSummary($track);
$returnValue['description'] = $track->description; $returnValue['description'] = $track->description;
@ -261,7 +262,7 @@ class Track extends Model
return $returnValue; return $returnValue;
} }
public static function mapPublicTrackSummary($track) public static function mapPublicTrackSummary(Track $track)
{ {
$userData = [ $userData = [
'stats' => [ 'stats' => [
@ -333,7 +334,7 @@ class Track extends Model
]; ];
} }
public static function mapPrivateTrackShow($track) public static function mapPrivateTrackShow(Track $track)
{ {
$showSongs = []; $showSongs = [];
foreach ($track->showSongs as $showSong) { foreach ($track->showSongs as $showSong) {
@ -354,7 +355,7 @@ class Track extends Model
return $returnValue; return $returnValue;
} }
public static function mapPrivateTrackSummary($track) public static function mapPrivateTrackSummary(Track $track)
{ {
return [ return [
'id' => $track->id, 'id' => $track->id,
@ -438,9 +439,22 @@ class Track extends Model
$this->updateHash(); $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) 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) public function canView($user)

View file

@ -22,10 +22,9 @@ namespace Poniverse\Ponyfm;
use Helpers; use Helpers;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\App; use App;
use Illuminate\Support\Facades\Cache; use File;
use Illuminate\Support\Facades\File; use URL;
use Illuminate\Support\Facades\URL;
class TrackFile extends Model class TrackFile extends Model
{ {
@ -85,9 +84,9 @@ class TrackFile extends Model
return URL::to('/t' . $this->track_id . '/dl.' . $this->extension); 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() public function getFormat()

View file

@ -46,8 +46,8 @@ class UpdateTrackFilesWithCache extends Migration
{ {
Schema::table('track_files', function (Blueprint $table) { Schema::table('track_files', function (Blueprint $table) {
$table->dropColumn('is_cacheable'); $table->dropColumn('is_cacheable');
$table->dropColumn('expiration'); $table->dropColumn('expires_at');
$table->dropColumn('in_progress'); $table->dropColumn('is_in_progress');
}); });
} }
} }