From adbb894873846ebb6a67aecbc82e5d19d23bfc30 Mon Sep 17 00:00:00 2001 From: Peter Deltchev Date: Tue, 26 May 2015 15:41:55 -0700 Subject: [PATCH] T125: Properly 404 on a nonexistent TrackFile. --- app/models/Entities/TrackFile.php | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/app/models/Entities/TrackFile.php b/app/models/Entities/TrackFile.php index 9730f793..a84dc40f 100644 --- a/app/models/Entities/TrackFile.php +++ b/app/models/Entities/TrackFile.php @@ -12,6 +12,15 @@ class TrackFile extends \Eloquent { return $this->belongsTo('Entities\Track'); } + /** + * Look up and return a TrackFile by track ID and an extension. + * + * If the track does not have a TrackFile in the given extension's format, a 404 exception is thrown. + * + * @param int $trackId + * @param string $extension + * @return TrackFile + */ public static function findOrFailByExtension($trackId, $extension) { // find the extension's format $requestedFormatName = null; @@ -25,11 +34,17 @@ class TrackFile extends \Eloquent { App::abort(404); } - return static:: + $trackFile = static:: with('track') ->where('track_id', $trackId) ->where('format', $requestedFormatName) ->first(); + + if ($trackFile === null) { + App::abort(404); + } else { + return $trackFile; + } } public function getFormatAttribute($value) {