Pony.fm/app/Http/Controllers/Api/Web/AlbumsController.php

212 lines
6.2 KiB
PHP
Raw 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/>.
*/
namespace Poniverse\Ponyfm\Http\Controllers\Api\Web;
2015-08-31 16:30:02 +02:00
2015-10-28 18:55:21 +01:00
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Poniverse\Ponyfm\Models\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\Models\Image;
use Poniverse\Ponyfm\Models\ResourceLogItem;
2016-01-01 03:27:21 +01:00
use Auth;
2016-05-18 02:27:59 +02:00
use Gate;
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
use Illuminate\Support\Facades\Request;
2016-02-25 20:00:12 +01:00
use Poniverse\Ponyfm\Models\User;
2016-01-01 03:27:21 +01:00
use Response;
use Poniverse\Ponyfm\Models\Track;
2015-08-31 16:30:02 +02:00
2015-09-06 19:21:11 +02:00
class AlbumsController extends ApiControllerBase
2015-08-31 16:30:02 +02:00
{
public function postCreate()
{
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
return $this->execute(new CreateAlbumCommand(Request::all()));
2015-08-31 16:30:02 +02:00
}
public function postEdit($id)
{
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
return $this->execute(new EditAlbumCommand($id, Request::all()));
2015-08-31 16:30:02 +02:00
}
public function postDelete($id)
{
return $this->execute(new DeleteAlbumCommand($id));
}
public function getShow($id)
{
$album = Album::with([
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
'tracks' => function ($query) {
2015-08-31 16:30:02 +02:00
$query->userDetails();
},
'tracks.cover',
'tracks.genre',
'tracks.user',
2016-05-28 21:29:06 +02:00
'tracks.user.avatar',
'tracks.trackFiles',
2015-08-31 16:30:02 +02:00
'user',
2016-05-28 21:29:06 +02:00
'user.avatar',
2015-08-31 16:30:02 +02:00
'comments',
'comments.user'
])
->userDetails()
->find($id);
if (!$album) {
App::abort(404);
}
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
if (Request::get('log')) {
2015-08-31 16:30:02 +02:00
ResourceLogItem::logItem('album', $id, ResourceLogItem::VIEW);
$album->view_count++;
}
$returned_album = Album::mapPublicAlbumShow($album);
if ($returned_album['is_downloadable'] == 0) {
unset($returned_album['formats']);
}
return Response::json([
'album' => $returned_album
], 200);
}
2015-10-28 18:55:21 +01:00
public function getCachedAlbum($id, $format)
{
// Validation
try {
/** @var Album $album */
2015-11-01 17:49:28 +01:00
$album = Album::with('tracks.trackFiles')->findOrFail($id);
2015-10-28 18:55:21 +01:00
} catch (ModelNotFoundException $e) {
return $this->notFound('Album not found!');
}
2015-11-01 17:49:28 +01:00
if (!in_array($format, Track::$CacheableFormats)) {
2015-10-28 18:55:21 +01:00
return $this->notFound('Format not found!');
}
$trackCount = $album->countDownloadableTracks($format);
$availableFilesCount = $album->countAvailableTrackFiles($format);
2015-10-28 18:55:21 +01:00
if ($trackCount === $availableFilesCount) {
2015-10-28 18:55:21 +01:00
$url = $album->getDownloadUrl($format);
} else {
2015-11-01 17:49:28 +01:00
$album->encodeCacheableTrackFiles($format);
2015-10-28 18:55:21 +01:00
$url = null;
}
return Response::json(['url' => $url], 200);
}
2015-08-31 16:30:02 +02:00
public function getIndex()
{
$page = 1;
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
if (Request::has('page')) {
$page = Request::get('page');
2015-08-31 16:30:02 +02:00
}
$query = Album::summary()
->with('user', 'user.avatar', 'cover')
->userDetails()
// An album with only one track is not really an album.
->where('track_count', '>', 1);
2015-08-31 16:30:02 +02:00
$count = $query->count();
$perPage = 40;
$query
->orderBy('title', 'asc')
->skip(($page - 1) * $perPage)
->take($perPage);
2015-08-31 16:30:02 +02:00
$albums = [];
foreach ($query->get() as $album) {
$albums[] = Album::mapPublicAlbumSummary($album);
}
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
return Response::json(
["albums" => $albums, "current_page" => $page, "total_pages" => ceil($count / $perPage)],
200
);
2015-08-31 16:30:02 +02:00
}
public function getOwned(User $user)
2015-08-31 16:30:02 +02:00
{
2016-02-25 20:00:12 +01:00
$this->authorize('get-albums', $user);
2016-05-28 21:29:06 +02:00
$query = Album::summary()
->with('cover', 'user.avatar')
->where('user_id', $user->id)
->orderBy('created_at', 'desc')->get();
2015-08-31 16:30:02 +02:00
$albums = [];
2015-08-31 16:30:02 +02:00
foreach ($query as $album) {
$albums[] = [
'id' => $album->id,
'title' => $album->title,
'slug' => $album->slug,
2015-12-26 21:59:45 +01:00
'created_at' => $album->created_at->format('c'),
2015-08-31 16:30:02 +02:00
'covers' => [
'small' => $album->getCoverUrl(Image::SMALL),
'normal' => $album->getCoverUrl(Image::NORMAL)
]
];
}
return Response::json($albums, 200);
}
public function getEdit($id)
{
$album = Album::with('tracks')->find($id);
if (!$album) {
return $this->notFound('Album '.$id.' not found!');
2015-08-31 16:30:02 +02:00
}
2016-05-18 02:27:59 +02:00
if (Gate::denies('edit', Auth::user())) {
2015-08-31 16:30:02 +02:00
return $this->notAuthorized();
}
$tracks = [];
foreach ($album->tracks as $track) {
$tracks[] = [
'id' => $track->id,
'title' => $track->title
];
}
return Response::json([
'id' => $album->id,
'title' => $album->title,
'user_id' => $album->user_id,
2016-05-18 02:27:59 +02:00
'username' => User::whereId($album->user_id)->first()->username,
2015-08-31 16:30:02 +02:00
'slug' => $album->slug,
'created_at' => $album->created_at,
'published_at' => $album->published_at,
'description' => $album->description,
'cover_url' => $album->hasCover() ? $album->getCoverUrl(Image::NORMAL) : null,
'real_cover_url' => $album->getCoverUrl(Image::NORMAL),
'tracks' => $tracks
], 200);
}
}