Pony.fm/app/controllers/TracksController.php

143 lines
3.6 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 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
public function getStream($id) {
$track = Track::find($id);
if (!$track || !$track->canView(Auth::user()))
App::abort(404);
$format = Track::$Formats['MP3'];
2013-08-19 05:39:29 +02:00
ResourceLogItem::logItem('track', $id, ResourceLogItem::PLAY, $format['index']);
2013-08-01 10:57:08 +02:00
$response = Response::make('', 200);
2013-09-01 21:28:29 +02:00
$filename = $track->getFileFor('MP3');
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);
2013-08-19 05:39:29 +02:00
$response->header('Content-Type', $format['mime_type']);
return $response;
}
public function getDownload($id, $extension) {
$track = Track::find($id);
if (!$track || !$track->canView(Auth::user()))
App::abort(404);
$format = null;
$formatName = null;
foreach (Track::$Formats as $name => $item) {
if ($item['extension'] == $extension) {
$format = $item;
$formatName = $name;
break;
}
}
if ($format == null)
App::abort(404);
ResourceLogItem::logItem('track', $id, ResourceLogItem::DOWNLOAD, $format['index']);
$response = Response::make('', 200);
2013-09-01 21:28:29 +02:00
$filename = $track->getFileFor($format);
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 21:24:48 +02:00
$response->header('Content-Disposition', 'attachment; filename="' . $track->getDownloadFilenameFor($formatName) . '"');
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 21:24:48 +02:00
$response->header('Content-Disposition', 'attachment; filename=' . $track->getDownloadFilenameFor($formatName));
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);
2013-08-01 10:57:08 +02:00
$response->header('Content-Type', $format['mime_type']);
return $response;
}
2013-07-25 23:33:04 +02:00
}