mirror of
https://github.com/Poniverse/Pony.fm.git
synced 2024-11-22 04:58:01 +01:00
Add cache handling for controllers
This commit is contained in:
parent
a03c44aadb
commit
629b9913ce
2 changed files with 87 additions and 0 deletions
|
@ -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;
|
||||
|
|
|
@ -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;
|
||||
|
|
Loading…
Reference in a new issue