Pony.fm/app/controllers/TracksController.php

153 lines
3.8 KiB
PHP
Raw Normal View History

2013-07-25 23:33:04 +02:00
<?php
2013-08-19 05:39:29 +02:00
use Entities\ResourceLogItem;
2013-07-27 02:15:07 +02:00
use Entities\Track;
use Entities\TrackFile;
2013-07-27 02:15:07 +02:00
use Illuminate\Support\Facades\App;
2013-07-25 23:33:04 +02:00
class TracksController extends Controller {
public function getIndex() {
return View::make('tracks.index');
}
2013-08-31 05:16:16 +02:00
public function getEmbed($id) {
2013-09-01 04:20:48 +02:00
$track = Track
::whereId($id)
->published()
->userDetails()
->with(
'user',
'user.avatar',
'genre'
)->first();
2013-08-31 05:16:16 +02:00
if (!$track || !$track->canView(Auth::user()))
App::abort(404);
2013-09-01 04:20:48 +02:00
$userData = [
'stats' => [
'views' => 0,
'plays' => 0,
'downloads' => 0
],
'is_favourited' => false
];
if ($track->users->count()) {
$userRow = $track->users[0];
$userData = [
'stats' => [
'views' => $userRow->view_count,
'plays' => $userRow->play_count,
'downloads' => $userRow->download_count,
],
'is_favourited' => $userRow->is_favourited
];
}
return View::make('tracks.embed', ['track' => $track, 'user' => $userData]);
2013-08-31 05:16:16 +02:00
}
public function getTrack($id, $slug) {
$track = Track::find($id);
if (!$track || !$track->canView(Auth::user()))
App::abort(404);
if ($track->slug != $slug)
return Redirect::action('TracksController@getTrack', [$id, $track->slug]);
return View::make('tracks.show');
}
public function getShortlink($id) {
$track = Track::find($id);
if (!$track || !$track->canView(Auth::user()))
App::abort(404);
return Redirect::action('TracksController@getTrack', [$id, $track->slug]);
}
2013-08-01 10:57:08 +02:00
2013-09-02 00:51:51 +02:00
public function getStream($id, $extension) {
2013-08-01 10:57:08 +02:00
$track = Track::find($id);
if (!$track || !$track->canView(Auth::user()))
App::abort(404);
$trackFile = null;
2013-09-02 00:51:51 +02:00
foreach ($track->trackFiles as $file) {
if ($file->extension === $extension) {
$trackFile = $file;
2013-09-02 00:51:51 +02:00
break;
}
}
if ($trackFile == null)
2013-09-02 00:51:51 +02:00
App::abort(404);
ResourceLogItem::logItem('track', $id, ResourceLogItem::PLAY, $trackFile->getFormat()['index']);
2013-08-01 10:57:08 +02:00
$response = Response::make('', 200);
$filename = $trackFile->getFile();
2013-09-01 10:07:03 +02:00
2013-09-01 10:07:59 +02:00
if (Config::get('app.sendfile')) {
2013-09-01 21:28:29 +02:00
$response->header('X-Sendfile', $filename);
2013-09-01 10:07:03 +02:00
} else {
2013-09-01 21:28:29 +02:00
$response->header('X-Accel-Redirect', $filename);
2013-09-01 10:07:03 +02:00
}
2013-09-01 21:28:29 +02:00
$time = gmdate(filemtime($filename));
if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) && $time == $_SERVER['HTTP_IF_MODIFIED_SINCE']) {
header('HTTP/1.0 304 Not Modified');
exit();
}
$response->header('Last-Modified', $time);
$response->header('Content-Type', $trackFile->getFormat()['mime_type']);
2013-08-19 05:39:29 +02:00
return $response;
}
public function getDownload($id, $extension) {
$track = Track::find($id);
if (!$track || !$track->canView(Auth::user()))
App::abort(404);
$trackFile = null;
2013-08-19 05:39:29 +02:00
foreach ($track->trackFiles as $file) {
if ($file->extension === $extension) {
$trackFile = $file;
2013-08-19 05:39:29 +02:00
break;
}
}
if ($trackFile == null)
2013-08-19 05:39:29 +02:00
App::abort(404);
ResourceLogItem::logItem('track', $id, ResourceLogItem::DOWNLOAD, $trackFile->getFormat()['index']);
2013-08-19 05:39:29 +02:00
$response = Response::make('', 200);
$filename = $trackFile->getFile();
2013-09-01 10:07:03 +02:00
2013-09-01 10:07:59 +02:00
if (Config::get('app.sendfile')) {
2013-09-01 21:28:29 +02:00
$response->header('X-Sendfile', $filename);
$response->header('Content-Disposition', 'attachment; filename="' . $trackFile->getDownloadFilename() . '"');
2013-09-01 10:07:03 +02:00
} else {
2013-09-01 21:28:29 +02:00
$response->header('X-Accel-Redirect', $filename);
$response->header('Content-Disposition', 'attachment; filename="' . $trackFile->getDownloadFilename() . '"');
2013-09-01 10:07:03 +02:00
}
2013-09-01 10:07:59 +02:00
2013-09-01 21:28:29 +02:00
$time = gmdate(filemtime($filename));
if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) && $time == $_SERVER['HTTP_IF_MODIFIED_SINCE']) {
header('HTTP/1.0 304 Not Modified');
exit();
}
$response->header('Last-Modified', $time);
$response->header('Content-Type', $trackFile->getFormat()['mime_type']);
2013-08-01 10:57:08 +02:00
return $response;
}
2013-07-25 23:33:04 +02:00
}