2015-08-31 16:30:02 +02:00
|
|
|
<?php
|
|
|
|
|
2015-10-25 06:17:45 +01:00
|
|
|
/**
|
|
|
|
* Pony.fm - A community for pony fan music.
|
2021-02-14 03:39:15 +01:00
|
|
|
* Copyright (C) 2015 Feld0.
|
2015-10-25 06:17:45 +01:00
|
|
|
*
|
|
|
|
* 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
|
|
|
|
2021-02-14 03:34:58 +01:00
|
|
|
use App\Models\ResourceLogItem;
|
|
|
|
use App\Models\Track;
|
|
|
|
use App\Models\TrackFile;
|
2021-02-14 20:46:41 +01:00
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use Illuminate\Support\Facades\App;
|
2021-02-14 20:45:51 +01:00
|
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
|
use Illuminate\Support\Facades\Config;
|
|
|
|
use Illuminate\Support\Facades\Redirect;
|
|
|
|
use Illuminate\Support\Facades\Response;
|
|
|
|
use Illuminate\Support\Facades\View;
|
2015-08-31 16:30:02 +02:00
|
|
|
|
|
|
|
class TracksController extends Controller
|
|
|
|
{
|
|
|
|
public function getIndex()
|
|
|
|
{
|
2021-02-14 20:38:28 +01:00
|
|
|
return view('tracks.index');
|
2015-08-31 16:30:02 +02:00
|
|
|
}
|
|
|
|
|
2021-02-14 20:46:02 +01:00
|
|
|
public function getEmbed(Request $request, $id)
|
2015-08-31 16:30:02 +02:00
|
|
|
{
|
|
|
|
$track = Track
|
|
|
|
::whereId($id)
|
|
|
|
->published()
|
|
|
|
->userDetails()
|
|
|
|
->with(
|
|
|
|
'user',
|
|
|
|
'user.avatar',
|
|
|
|
'genre'
|
|
|
|
)->first();
|
|
|
|
|
2021-02-14 20:46:02 +01:00
|
|
|
if (! $track || ! $track->canView($request->user())) {
|
2021-02-14 20:38:28 +01:00
|
|
|
abort(404);
|
2015-08-31 16:30:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$userData = [
|
|
|
|
'stats' => [
|
|
|
|
'views' => 0,
|
|
|
|
'plays' => 0,
|
2021-02-14 03:39:15 +01:00
|
|
|
'downloads' => 0,
|
2015-08-31 16:30:02 +02:00
|
|
|
],
|
2021-02-14 03:39:15 +01:00
|
|
|
'is_favourited' => false,
|
2015-08-31 16:30:02 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
if ($track->users->count()) {
|
|
|
|
$userRow = $track->users[0];
|
|
|
|
$userData = [
|
|
|
|
'stats' => [
|
|
|
|
'views' => $userRow->view_count,
|
|
|
|
'plays' => $userRow->play_count,
|
|
|
|
'downloads' => $userRow->download_count,
|
|
|
|
],
|
2021-02-14 03:39:15 +01:00
|
|
|
'is_favourited' => $userRow->is_favourited,
|
2015-08-31 16:30:02 +02:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2021-02-14 20:38:28 +01:00
|
|
|
return view('tracks.embed', ['track' => $track, 'user' => $userData]);
|
2015-08-31 16:30:02 +02:00
|
|
|
}
|
|
|
|
|
2016-11-30 22:42:45 +01:00
|
|
|
public function getOembed(Request $request)
|
|
|
|
{
|
2021-02-14 03:39:15 +01:00
|
|
|
if (! $request->filled('url')) {
|
2021-02-14 20:38:28 +01:00
|
|
|
abort(404);
|
2016-11-30 22:42:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$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();
|
|
|
|
|
2021-02-14 20:46:02 +01:00
|
|
|
if (! $track || ! $track->canView($request->user())) {
|
2021-02-14 20:38:28 +01:00
|
|
|
abort(404);
|
2016-11-30 22:42:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$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,
|
2021-03-27 04:51:45 +01:00
|
|
|
'html' => '<iframe src="'.action([static::class, 'getEmbed'], ['id' => $track->id]).'" width="100%" height="150" allowTransparency="true" frameborder="0" seamless allowfullscreen></iframe>',
|
2016-11-30 22:42:45 +01:00
|
|
|
];
|
|
|
|
|
2021-02-14 20:45:52 +01:00
|
|
|
return response()->json($output);
|
2016-11-30 22:42:45 +01:00
|
|
|
}
|
|
|
|
|
2021-02-14 20:46:02 +01:00
|
|
|
public function getTrack(Request $request, $id, $slug)
|
2015-08-31 16:30:02 +02:00
|
|
|
{
|
|
|
|
$track = Track::find($id);
|
2021-02-14 20:46:02 +01:00
|
|
|
if (! $track || ! $track->canView($request->user())) {
|
2021-02-14 20:38:28 +01:00
|
|
|
abort(404);
|
2015-08-31 16:30:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($track->slug != $slug) {
|
2021-03-27 04:51:45 +01:00
|
|
|
return Redirect::action([static::class, 'getTrack'], [$id, $track->slug]);
|
2015-08-31 16:30:02 +02:00
|
|
|
}
|
|
|
|
|
2021-02-14 20:38:28 +01:00
|
|
|
return view('tracks.show', ['track' => $track]);
|
2015-08-31 16:30:02 +02:00
|
|
|
}
|
|
|
|
|
2016-02-14 04:11:32 +01:00
|
|
|
public function getEdit($id, $slug)
|
|
|
|
{
|
|
|
|
return $this->getTrack($id, $slug);
|
|
|
|
}
|
|
|
|
|
2021-02-14 20:46:02 +01:00
|
|
|
public function getShortlink(Request $request, $id)
|
2015-08-31 16:30:02 +02:00
|
|
|
{
|
|
|
|
$track = Track::find($id);
|
2021-02-14 20:46:02 +01:00
|
|
|
if (! $track || ! $track->canView($request->user())) {
|
2021-02-14 20:38:28 +01:00
|
|
|
abort(404);
|
2015-08-31 16:30:02 +02:00
|
|
|
}
|
|
|
|
|
2021-03-27 04:51:45 +01:00
|
|
|
return Redirect::action([static::class, 'getTrack'], [$id, $track->slug]);
|
2015-08-31 16:30:02 +02:00
|
|
|
}
|
|
|
|
|
2021-02-14 20:46:02 +01:00
|
|
|
public function getStream(Request $request, $id, $extension)
|
2015-08-31 16:30:02 +02:00
|
|
|
{
|
|
|
|
$track = Track::find($id);
|
2021-02-14 20:46:02 +01:00
|
|
|
if (! $track || ! $track->canView($request->user())) {
|
2021-02-14 20:38:28 +01:00
|
|
|
abort(404);
|
2015-08-31 16:30:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$trackFile = TrackFile::findOrFailByExtension($track->id, $extension);
|
|
|
|
|
2021-02-14 20:46:03 +01:00
|
|
|
$response = response()->noContent(200);
|
2015-08-31 16:30:02 +02:00
|
|
|
$filename = $trackFile->getFile();
|
|
|
|
|
2021-02-14 03:39:15 +01:00
|
|
|
if (! file_exists($filename)) {
|
2021-02-14 20:38:28 +01:00
|
|
|
abort(418);
|
2016-10-01 19:20:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ResourceLogItem::logItem('track', $id, ResourceLogItem::PLAY, $trackFile->getFormat()['index']);
|
|
|
|
|
2021-02-14 20:38:28 +01:00
|
|
|
if (config('app.sendfile')) {
|
2015-08-31 16:30:02 +02:00
|
|
|
$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;
|
|
|
|
}
|
|
|
|
|
2021-02-14 20:46:02 +01:00
|
|
|
public function getDownload(Request $request, $id, $extension)
|
2015-08-31 16:30:02 +02:00
|
|
|
{
|
|
|
|
$track = Track::find($id);
|
2021-02-14 20:46:02 +01:00
|
|
|
if (! $track || ! $track->canView($request->user())) {
|
2021-02-14 20:38:28 +01:00
|
|
|
abort(404);
|
2015-08-31 16:30:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$trackFile = TrackFile::findOrFailByExtension($track->id, $extension);
|
|
|
|
ResourceLogItem::logItem('track', $id, ResourceLogItem::DOWNLOAD, $trackFile->getFormat()['index']);
|
|
|
|
|
2021-02-14 20:46:03 +01:00
|
|
|
$response = response()->noContent(200);
|
2015-08-31 16:30:02 +02:00
|
|
|
$filename = $trackFile->getFile();
|
|
|
|
|
2021-02-14 20:38:28 +01:00
|
|
|
if (config('app.sendfile')) {
|
2015-08-31 16:30:02 +02:00
|
|
|
$response->header('X-Sendfile', $filename);
|
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);
|
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;
|
|
|
|
}
|
2015-10-25 06:17:45 +01:00
|
|
|
}
|