#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\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();
}

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\SoftDeletes;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\URL;
use Auth;
use Cache;
use URL;
use Poniverse\Ponyfm\Jobs\EncodeTrackFile;
use Poniverse\Ponyfm\Traits\SlugTrait;
@ -59,10 +59,12 @@ class Playlist extends Model
return !$query;
}
public static function mapPublicPlaylistShow($playlist)
public static function mapPublicPlaylistShow(Playlist $playlist)
{
$tracks = [];
foreach ($playlist->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
}
}
}

View file

@ -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)

View file

@ -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()

View file

@ -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');
});
}
}