T125: Refactored track downloads with TrackFile::findOrFailByExtension().

This commit is contained in:
Peter Deltchev 2015-05-25 22:45:31 -07:00
parent 9ade037820
commit 3a9a659257
2 changed files with 24 additions and 24 deletions

View file

@ -72,18 +72,7 @@
if (!$track || !$track->canView(Auth::user()))
App::abort(404);
$trackFile = null;
foreach ($track->trackFiles as $file) {
if ($file->extension === $extension) {
$trackFile = $file;
break;
}
}
if ($trackFile == null)
App::abort(404);
$trackFile = TrackFile::findOrFailByExtension($track->id, $extension);
ResourceLogItem::logItem('track', $id, ResourceLogItem::PLAY, $trackFile->getFormat()['index']);
$response = Response::make('', 200);
@ -113,18 +102,7 @@
if (!$track || !$track->canView(Auth::user()))
App::abort(404);
$trackFile = null;
foreach ($track->trackFiles as $file) {
if ($file->extension === $extension) {
$trackFile = $file;
break;
}
}
if ($trackFile == null)
App::abort(404);
$trackFile = TrackFile::findOrFailByExtension($track->id, $extension);
ResourceLogItem::logItem('track', $id, ResourceLogItem::DOWNLOAD, $trackFile->getFormat()['index']);
$response = Response::make('', 200);

View file

@ -1,6 +1,8 @@
<?php namespace Entities;
use Entities\Track;
use Helpers;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\URL;
use Illuminate\Support\Facades\Cache;
@ -10,6 +12,26 @@ class TrackFile extends \Eloquent {
return $this->belongsTo('Entities\Track');
}
public static function findOrFailByExtension($trackId, $extension) {
// find the extension's format
$requestedFormatName = null;
foreach (Track::$Formats as $name => $format) {
if ($extension === $format[ 'extension' ]) {
$requestedFormatName = $name;
break;
}
}
if ($requestedFormatName === null) {
App::abort(404);
}
return static::
with('track')
->where('track_id', $trackId)
->where('format', $requestedFormatName)
->first();
}
public function getFormatAttribute($value) {
return $value;
}