Pony.fm/app/Http/Controllers/TracksController.php

222 lines
6.4 KiB
PHP
Raw Permalink Normal View History

2015-08-31 16:30:02 +02:00
<?php
/**
* Pony.fm - A community for pony fan music.
2018-04-21 06:25:36 +02:00
* Copyright (C) 2015 Feld0
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
2021-02-14 03:34:58 +01:00
namespace App\Http\Controllers;
2015-09-06 19:21:11 +02:00
2016-11-30 22:42:45 +01:00
use Illuminate\Http\Request;
2021-02-14 03:34:58 +01:00
use App\Models\ResourceLogItem;
use App\Models\Track;
use App\Models\TrackFile;
2015-09-06 19:21:11 +02:00
use Auth;
use Config;
2016-06-12 13:49:24 +02:00
use App;
2015-09-06 19:21:11 +02:00
use Redirect;
use Response;
use View;
2015-08-31 16:30:02 +02:00
class TracksController extends Controller
{
public function getIndex()
{
return View::make('tracks.index');
}
public function getEmbed($id)
{
$track = Track
::whereId($id)
->published()
->userDetails()
->with(
'user',
'user.avatar',
'genre'
)->first();
if (!$track || !$track->canView(Auth::user())) {
App::abort(404);
}
$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]);
}
2016-11-30 22:42:45 +01:00
public function getOembed(Request $request)
{
2017-11-28 08:00:11 +01:00
if (!$request->filled('url')) {
2016-11-30 22:42:45 +01:00
App::abort(404);
}
$parsedUrl = parse_url($request->input('url'));
2016-11-30 23:08:40 +01:00
$id = explode('-', explode('/', $parsedUrl['path'])[2])[0];
2016-11-30 22:42:45 +01:00
$track = Track
::whereId($id)
->published()
->userDetails()
->first();
if (!$track || !$track->canView(Auth::user())) {
App::abort(404);
}
$output = [
'version' => '1.0',
'type' => 'rich',
'provider_name' => 'Pony.fm',
'provider_url' => 'https://pony.fm',
'width' => 480,
'height' => 130,
'title' => $track->title,
'author_name' => $track->user->display_name,
'author_url' => $track->user->url,
'html' => '<iframe src="'.action('TracksController@getEmbed', ['id' => $track->id]).'" width="100%" height="150" allowTransparency="true" frameborder="0" seamless allowfullscreen></iframe>'
];
return Response::json($output);
}
2015-08-31 16:30:02 +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', ['track' => $track]);
2015-08-31 16:30:02 +02:00
}
public function getEdit($id, $slug)
{
return $this->getTrack($id, $slug);
}
2015-08-31 16:30:02 +02:00
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]);
}
public function getStream($id, $extension)
{
$track = Track::find($id);
if (!$track || !$track->canView(Auth::user())) {
App::abort(404);
}
$trackFile = TrackFile::findOrFailByExtension($track->id, $extension);
$response = Response::make('', 200);
$filename = $trackFile->getFile();
2016-10-01 19:20:49 +02:00
if (!file_exists($filename)) {
App::abort(418);
}
ResourceLogItem::logItem('track', $id, ResourceLogItem::PLAY, $trackFile->getFormat()['index']);
2015-08-31 16:30:02 +02:00
if (Config::get('app.sendfile')) {
$response->header('X-Sendfile', $filename);
} else {
$response->header('X-Accel-Redirect', $filename);
}
$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']);
return $response;
}
public function getDownload($id, $extension)
{
$track = Track::find($id);
if (!$track || !$track->canView(Auth::user())) {
App::abort(404);
}
$trackFile = TrackFile::findOrFailByExtension($track->id, $extension);
ResourceLogItem::logItem('track', $id, ResourceLogItem::DOWNLOAD, $trackFile->getFormat()['index']);
$response = Response::make('', 200);
$filename = $trackFile->getFile();
if (Config::get('app.sendfile')) {
$response->header('X-Sendfile', $filename);
Laravel 5.2 Update (#106) * Adopt PSR-2 coding style The Laravel framework adopts the PSR-2 coding style in version 5.1. Laravel apps *should* adopt this coding style as well. Read the [PSR-2 coding style guide][1] for more details and check out [PHPCS][2] to use as a code formatting tool. [1]: https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md [2]: https://github.com/squizlabs/PHP_CodeSniffer * Adopt PHP short array syntax Laravel 5 adopted the short array syntax which became available in PHP 5.4. * Remove SelfHandling from Jobs Jobs are self handling by default in Laravel 5.2. * Add new exceptions to `$dontReport` property * Shift core files * Shift Middleware Laravel 5.2 adjusts the `Guard` object used within middleware. In addition, new `can` and `throttles` middleware were added. * Shift Input to Request facade Laravel 5.2 no longer registers the `Input` facade by default. Laravel now prefers using the `Request` facade or the `$request` object within *Controllers* instead. Review the [HTTP Requests][1] documentation for more details. [1]: https://laravel.com/docs/5.2/requests * Shift configuration Laravel 5.2 introduces the `env` app configuration option and removes the `pretend` mail configuration option. In addition, a few of the default `providers` and `aliases` bindings were removed. * Shift Laravel dependencies * Shift cleanup * Updated composer.lock * Updated Middleware to 5.2 * Config update for Laravel 5.2 * [Laravel 5.2] Updated validation strings * Updated auth config * Updated to use middleware groups * Added laravel 5.2 sessions migration
2016-09-30 00:26:31 +02:00
$response->header(
'Content-Disposition',
'attachment; filename="'.$trackFile->getDownloadFilename().'"'
);
2015-08-31 16:30:02 +02:00
} else {
$response->header('X-Accel-Redirect', $filename);
Laravel 5.2 Update (#106) * Adopt PSR-2 coding style The Laravel framework adopts the PSR-2 coding style in version 5.1. Laravel apps *should* adopt this coding style as well. Read the [PSR-2 coding style guide][1] for more details and check out [PHPCS][2] to use as a code formatting tool. [1]: https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md [2]: https://github.com/squizlabs/PHP_CodeSniffer * Adopt PHP short array syntax Laravel 5 adopted the short array syntax which became available in PHP 5.4. * Remove SelfHandling from Jobs Jobs are self handling by default in Laravel 5.2. * Add new exceptions to `$dontReport` property * Shift core files * Shift Middleware Laravel 5.2 adjusts the `Guard` object used within middleware. In addition, new `can` and `throttles` middleware were added. * Shift Input to Request facade Laravel 5.2 no longer registers the `Input` facade by default. Laravel now prefers using the `Request` facade or the `$request` object within *Controllers* instead. Review the [HTTP Requests][1] documentation for more details. [1]: https://laravel.com/docs/5.2/requests * Shift configuration Laravel 5.2 introduces the `env` app configuration option and removes the `pretend` mail configuration option. In addition, a few of the default `providers` and `aliases` bindings were removed. * Shift Laravel dependencies * Shift cleanup * Updated composer.lock * Updated Middleware to 5.2 * Config update for Laravel 5.2 * [Laravel 5.2] Updated validation strings * Updated auth config * Updated to use middleware groups * Added laravel 5.2 sessions migration
2016-09-30 00:26:31 +02:00
$response->header(
'Content-Disposition',
'attachment; filename="'.$trackFile->getDownloadFilename().'"'
);
2015-08-31 16:30:02 +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']);
return $response;
}
}