mirror of
https://github.com/Poniverse/Pony.fm.git
synced 2025-02-07 06:16:43 +01:00
T125: Refactored track downloads with TrackFile::findOrFailByExtension().
This commit is contained in:
parent
9ade037820
commit
3a9a659257
2 changed files with 24 additions and 24 deletions
|
@ -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);
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue