From 629b9913ceb950a9f20fc095fce22b173ea4b28c Mon Sep 17 00:00:00 2001 From: Kelvin Zhang Date: Wed, 28 Oct 2015 17:55:21 +0000 Subject: [PATCH] Add cache handling for controllers --- .../Controllers/Api/Web/AlbumsController.php | 50 +++++++++++++++++++ .../Controllers/Api/Web/TracksController.php | 37 ++++++++++++++ 2 files changed, 87 insertions(+) diff --git a/app/Http/Controllers/Api/Web/AlbumsController.php b/app/Http/Controllers/Api/Web/AlbumsController.php index 068d2d6c..fb9bee96 100644 --- a/app/Http/Controllers/Api/Web/AlbumsController.php +++ b/app/Http/Controllers/Api/Web/AlbumsController.php @@ -20,16 +20,19 @@ namespace Poniverse\Ponyfm\Http\Controllers\Api\Web; +use Illuminate\Database\Eloquent\ModelNotFoundException; use Poniverse\Ponyfm\Album; use Poniverse\Ponyfm\Commands\CreateAlbumCommand; use Poniverse\Ponyfm\Commands\DeleteAlbumCommand; use Poniverse\Ponyfm\Commands\EditAlbumCommand; use Poniverse\Ponyfm\Http\Controllers\ApiControllerBase; use Poniverse\Ponyfm\Image; +use Poniverse\Ponyfm\Jobs\EncodeTrackFile; use Poniverse\Ponyfm\ResourceLogItem; use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Input; use Illuminate\Support\Facades\Response; +use Poniverse\Ponyfm\Track; class AlbumsController extends ApiControllerBase { @@ -83,6 +86,53 @@ class AlbumsController extends ApiControllerBase ], 200); } + public function getCachedAlbum($id, $format) + { + // Validation + try { + $album = Album::findOrFail($id); + } catch (ModelNotFoundException $e) { + return $this->notFound('Album not found!'); + } + + if (!array_key_exists($format, Track::$CacheableFormats)) { + return $this->notFound('Format not found!'); + } + + $tracks = $album->tracks; + + $trackCount = 0; + $cachedCount = 0; + + foreach($tracks as $track) { + if ($track->is_downloadable == false) { + continue; + } else { + $trackCount++; + } + + try { + $trackFile = $track->trackFiles()->where('format', $format)->firstOrFail(); + } catch (ModelNotFoundException $e) { + return $this->notFound('Track file for track ID ' . $track->id . ' not found!'); + } + + if ($trackFile->expiration != null) { + $cachedCount++; + } elseif ($trackFile->in_progress != true) { + $this->dispatch(new EncodeTrackFile($trackFile, true)); + } + } + + if ($trackCount === $cachedCount) { + $url = $album->getDownloadUrl($format); + } else { + $url = null; + } + + return Response::json(['url' => $url], 200); + } + public function getIndex() { $page = 1; diff --git a/app/Http/Controllers/Api/Web/TracksController.php b/app/Http/Controllers/Api/Web/TracksController.php index 95580a79..77ba94de 100644 --- a/app/Http/Controllers/Api/Web/TracksController.php +++ b/app/Http/Controllers/Api/Web/TracksController.php @@ -20,10 +20,13 @@ namespace Poniverse\Ponyfm\Http\Controllers\Api\Web; +use Illuminate\Database\Eloquent\ModelNotFoundException; +use Illuminate\Support\Facades\File; use Poniverse\Ponyfm\Commands\DeleteTrackCommand; use Poniverse\Ponyfm\Commands\EditTrackCommand; use Poniverse\Ponyfm\Commands\UploadTrackCommand; use Poniverse\Ponyfm\Http\Controllers\ApiControllerBase; +use Poniverse\Ponyfm\Jobs\EncodeTrackFile; use Poniverse\Ponyfm\ResourceLogItem; use Poniverse\Ponyfm\Track; use Cover; @@ -70,6 +73,40 @@ class TracksController extends ApiControllerBase return Response::json(['track' => $returned_track], 200); } + public function getCachedTrack($id, $format) + { + // Validation + try { + $track = Track::findOrFail($id); + } catch (ModelNotFoundException $e) { + return $this->notFound('Track not found!'); + } + + if (!$track->canView(Auth::user())) + return $this->notFound('Track not found!'); + + if (!in_array($format, array_keys(Track::$CacheableFormats))) { + return $this->notFound('Format not found!'); + } + + try { + $trackFile = $track->trackFiles()->where('format', $format)->firstOrFail(); + } catch (ModelNotFoundException $e) { + return $this->notFound('Track file not found!'); + } + + // Return URL or begin encoding + if ($trackFile->expiration != null && File::exists($trackFile->getFile())) { + $url = $track->getUrlFor($format); + } elseif ($trackFile->in_progress === true) { + $url = null; + } else { + $this->dispatch(new EncodeTrackFile($trackFile, true)); + $url = null; + } + + } + public function getIndex() { $page = 1;