mirror of
https://github.com/HDInnovations/UNIT3D-Community-Edition.git
synced 2026-04-23 19:50:40 -05:00
refactor: cruddify playlists
Use cruddy routes and controllers. Use route model binding. Use mass assignment. Use form requests. Use pivot model.
This commit is contained in:
@@ -13,17 +13,14 @@
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Helpers\Bencode;
|
||||
use App\Http\Requests\StorePlaylistRequest;
|
||||
use App\Http\Requests\UpdatePlaylistRequest;
|
||||
use App\Models\Movie;
|
||||
use App\Models\Playlist;
|
||||
use App\Models\PlaylistTorrent;
|
||||
use App\Models\Torrent;
|
||||
use App\Models\Tv;
|
||||
use App\Repositories\ChatRepository;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\File;
|
||||
use Intervention\Image\Facades\Image;
|
||||
use ZipArchive;
|
||||
use Exception;
|
||||
|
||||
/**
|
||||
@@ -71,98 +68,50 @@ class PlaylistController extends Controller
|
||||
/**
|
||||
* Store A New Playlist.
|
||||
*/
|
||||
public function store(Request $request): \Illuminate\Http\RedirectResponse
|
||||
public function store(StorePlaylistRequest $request): \Illuminate\Http\RedirectResponse
|
||||
{
|
||||
$user = auth()->user();
|
||||
|
||||
$playlist = new Playlist();
|
||||
$playlist->user_id = $user->id;
|
||||
$playlist->name = $request->input('name');
|
||||
$playlist->description = $request->input('description');
|
||||
$playlist->cover_image = null;
|
||||
|
||||
if ($request->hasFile('cover_image') && $request->file('cover_image')->getError() === 0) {
|
||||
$image = $request->file('cover_image');
|
||||
$filename = 'playlist-cover_'.uniqid('', true).'.'.$image->getClientOriginalExtension();
|
||||
$path = public_path('/files/img/'.$filename);
|
||||
Image::make($image->getRealPath())->fit(400, 225)->encode('png', 100)->save($path);
|
||||
$playlist->cover_image = $filename;
|
||||
}
|
||||
|
||||
$playlist->position = $request->input('position');
|
||||
$playlist->is_private = $request->input('is_private');
|
||||
$playlist = Playlist::create([
|
||||
'user_id' => $request->user()->id,
|
||||
'cover_image' => $filename ?? null
|
||||
] + $request->validated());
|
||||
|
||||
$v = validator($playlist->toArray(), [
|
||||
'user_id' => 'required',
|
||||
'name' => 'required',
|
||||
'description' => 'required',
|
||||
'is_private' => 'required',
|
||||
]);
|
||||
|
||||
if ($v->fails()) {
|
||||
return to_route('playlists.create')
|
||||
->withInput()
|
||||
->withErrors($v->errors());
|
||||
}
|
||||
|
||||
$playlist->save();
|
||||
// Announce To Shoutbox
|
||||
$appurl = config('app.url');
|
||||
|
||||
if ($playlist->is_private != 1) {
|
||||
if (! $playlist->is_private) {
|
||||
$this->chatRepository->systemMessage(
|
||||
sprintf('User [url=%s/', $appurl).$user->username.'.'.$user->id.']'.$user->username.sprintf('[/url] has created a new playlist [url=%s/playlists/', $appurl).$playlist->id.']'.$playlist->name.'[/url] check it out now! :slight_smile:'
|
||||
sprintf('User [url=%s/', config('app.url')).$request->user()->username.'.'.$request->user()->id.']'.$request->user()->username.sprintf('[/url] has created a new playlist [url=%s/playlists/', config('app.url')).$playlist->id.']'.$playlist->name.'[/url] check it out now! :slight_smile:'
|
||||
);
|
||||
}
|
||||
|
||||
return to_route('playlists.show', ['id' => $playlist->id])
|
||||
return to_route('playlists.show', ['playlist' => $playlist])
|
||||
->withSuccess(trans('playlist.published-success'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Show A Playlist.
|
||||
*/
|
||||
public function show(int $id): \Illuminate\Contracts\View\Factory|\Illuminate\View\View
|
||||
public function show(Request $request, Playlist $playlist): \Illuminate\Contracts\View\Factory|\Illuminate\View\View
|
||||
{
|
||||
$playlist = Playlist::findOrFail($id);
|
||||
abort_if($playlist->is_private && $playlist->user_id !== $request->user()->id, 403, trans('playlist.private-error'));
|
||||
|
||||
if ($playlist->is_private) {
|
||||
abort_unless($playlist->user_id === auth()->id(), 403, trans('playlist.private-error'));
|
||||
}
|
||||
|
||||
$random = PlaylistTorrent::query()
|
||||
->where('playlist_id', '=', $playlist->id)
|
||||
->whereHas('torrent')
|
||||
->inRandomOrder()
|
||||
->first();
|
||||
|
||||
$meta = null;
|
||||
|
||||
if (isset($random)) {
|
||||
$torrent = Torrent::findOrFail($random->torrent_id);
|
||||
|
||||
if ($torrent->category->tv_meta && ($torrent->tmdb || $torrent->tmdb != 0)) {
|
||||
$meta = Tv::with('genres', 'networks', 'seasons')->find($torrent->tmdb);
|
||||
}
|
||||
|
||||
if ($torrent->category->movie_meta && ($torrent->tmdb || $torrent->tmdb != 0)) {
|
||||
$meta = Movie::with('genres', 'companies', 'collection')->find($torrent->tmdb);
|
||||
}
|
||||
}
|
||||
$randomTorrent = $playlist->torrents()->inRandomOrder()->first();
|
||||
|
||||
return view('playlist.show', [
|
||||
'playlist' => $playlist,
|
||||
'meta' => $meta,
|
||||
'torrents' => PlaylistTorrent::with(['torrent:id,name,category_id,resolution_id,type_id,tmdb,seeders,leechers,times_completed,size,anon,created_at'])
|
||||
->where('playlist_id', '=', $playlist->id)
|
||||
->whereHas('torrent')
|
||||
->orderBy(function ($query): void {
|
||||
$query->select('name')
|
||||
->from('torrents')
|
||||
->whereColumn('id', 'playlist_torrents.torrent_id')
|
||||
->latest()
|
||||
->limit(1);
|
||||
})
|
||||
'meta' => match(1) {
|
||||
$randomTorrent?->category?->tv_meta => Tv::with('genres', 'networks', 'seasons')->find($randomTorrent->tmdb),
|
||||
$randomTorrent?->category?->movie_meta => Movie::with('genres', 'companies', 'collection')->find($randomTorrent->tmdb),
|
||||
default => null,
|
||||
},
|
||||
'torrents' => $playlist->torrents()
|
||||
->with(['category', 'resolution', 'type', 'user.group'])
|
||||
->orderBy('name')
|
||||
->paginate(26),
|
||||
]);
|
||||
}
|
||||
@@ -170,12 +119,9 @@ class PlaylistController extends Controller
|
||||
/**
|
||||
* Show Playlist Update Form.
|
||||
*/
|
||||
public function edit(int $id): \Illuminate\Contracts\View\Factory|\Illuminate\View\View
|
||||
public function edit(Request $request, Playlist $playlist): \Illuminate\Contracts\View\Factory|\Illuminate\View\View
|
||||
{
|
||||
$user = auth()->user();
|
||||
$playlist = Playlist::findOrFail($id);
|
||||
|
||||
abort_unless($user->id == $playlist->user_id || $user->group->is_modo, 403);
|
||||
abort_unless($request->user()->id === $playlist->user_id || $request->user()->group->is_modo, 403);
|
||||
|
||||
return view('playlist.edit', ['playlist' => $playlist]);
|
||||
}
|
||||
@@ -183,43 +129,20 @@ class PlaylistController extends Controller
|
||||
/**
|
||||
* Update A Playlist.
|
||||
*/
|
||||
public function update(Request $request, int $id): \Illuminate\Http\RedirectResponse
|
||||
public function update(UpdatePlaylistRequest $request, Playlist $playlist): \Illuminate\Http\RedirectResponse
|
||||
{
|
||||
$user = auth()->user();
|
||||
$playlist = Playlist::findOrFail($id);
|
||||
|
||||
abort_unless($user->id == $playlist->user_id || $user->group->is_modo, 403);
|
||||
|
||||
$playlist->name = $request->input('name');
|
||||
$playlist->description = $request->input('description');
|
||||
$playlist->cover_image = null;
|
||||
abort_unless($request->user()->id == $playlist->user_id || $request->user()->group->is_modo, 403);
|
||||
|
||||
if ($request->hasFile('cover_image') && $request->file('cover_image')->getError() === 0) {
|
||||
$image = $request->file('cover_image');
|
||||
$filename = 'playlist-cover_'.uniqid('', true).'.'.$image->getClientOriginalExtension();
|
||||
$path = public_path('/files/img/'.$filename);
|
||||
Image::make($image->getRealPath())->fit(400, 225)->encode('png', 100)->save($path);
|
||||
$playlist->cover_image = $filename;
|
||||
}
|
||||
|
||||
$playlist->position = $request->input('position');
|
||||
$playlist->is_private = $request->input('is_private');
|
||||
$playlist->update(['cover_image' => $filename ?? null] + $request->validated());
|
||||
|
||||
$v = validator($playlist->toArray(), [
|
||||
'name' => 'required',
|
||||
'description' => 'required',
|
||||
'is_private' => 'required',
|
||||
]);
|
||||
|
||||
if ($v->fails()) {
|
||||
return to_route('playlists.edit', ['id' => $id])
|
||||
->withInput()
|
||||
->withErrors($v->errors());
|
||||
}
|
||||
|
||||
$playlist->save();
|
||||
|
||||
return to_route('playlists.show', ['id' => $id])
|
||||
return to_route('playlists.show', ['playlist' => $playlist])
|
||||
->withSuccess(trans('playlist.update-success'));
|
||||
}
|
||||
|
||||
@@ -228,81 +151,14 @@ class PlaylistController extends Controller
|
||||
*
|
||||
* @throws Exception
|
||||
*/
|
||||
public function destroy(int $id): \Illuminate\Http\RedirectResponse
|
||||
public function destroy(Request $request, Playlist $playlist): \Illuminate\Http\RedirectResponse
|
||||
{
|
||||
$user = auth()->user();
|
||||
$playlist = Playlist::with('torrents')->findOrFail($id);
|
||||
abort_unless($request->user()->id == $playlist->user_id || $request->user()->group->is_modo, 403);
|
||||
|
||||
abort_unless($user->id == $playlist->user_id || $user->group->is_modo, 403);
|
||||
|
||||
$playlist->torrents()->delete();
|
||||
$playlist->torrents()->detach();
|
||||
$playlist->delete();
|
||||
|
||||
return to_route('playlists.index')
|
||||
->withSuccess(trans('playlist.deleted'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Download All Playlist Torrents.
|
||||
*/
|
||||
public function downloadPlaylist(int $id): \Illuminate\Http\RedirectResponse|\Symfony\Component\HttpFoundation\BinaryFileResponse
|
||||
{
|
||||
// Extend The Maximum Execution Time
|
||||
set_time_limit(300);
|
||||
|
||||
// Playlist
|
||||
$playlist = Playlist::with('torrents')->findOrFail($id);
|
||||
|
||||
// Authorized User
|
||||
$user = auth()->user();
|
||||
|
||||
// Define Dir Folder
|
||||
$path = getcwd().'/files/tmp_zip/';
|
||||
|
||||
// Check Directory exists
|
||||
if (! File::isDirectory($path)) {
|
||||
File::makeDirectory($path, 0755, true, true);
|
||||
}
|
||||
|
||||
// Zip File Name
|
||||
$zipFileName = '['.$user->username.']'.$playlist->name.'.zip';
|
||||
|
||||
// Create ZipArchive Obj
|
||||
$zipArchive = new ZipArchive();
|
||||
|
||||
// Get Users History
|
||||
$playlistTorrents = Torrent::whereRelation('playlists', 'playlist_id', '=', $playlist->id)->get();
|
||||
|
||||
if ($zipArchive->open($path.$zipFileName, ZipArchive::CREATE | ZipArchive::OVERWRITE) === true) {
|
||||
$announceUrl = route('announce', ['passkey' => $user->passkey]);
|
||||
|
||||
foreach ($playlistTorrents as $torrent) {
|
||||
$dict = Bencode::bdecode(file_get_contents(getcwd().'/files/torrents/'.$torrent->file_name));
|
||||
|
||||
// Set the announce key and add the user passkey
|
||||
$dict['announce'] = $announceUrl;
|
||||
|
||||
// Set link to torrent as the comment
|
||||
if (config('torrent.comment')) {
|
||||
$dict['comment'] = config('torrent.comment').'. '.route('torrents.show', ['id' => $torrent->id]);
|
||||
} else {
|
||||
$dict['comment'] = route('torrents.show', ['id' => $torrent->id]);
|
||||
}
|
||||
|
||||
$fileToDownload = Bencode::bencode($dict);
|
||||
|
||||
$filename = str_replace([' ', '/', '\\'], ['.', '-', '-'], '['.config('torrent.source').']'.$torrent->name.'.torrent');
|
||||
|
||||
$zipArchive->addFromString($filename, $fileToDownload);
|
||||
}
|
||||
|
||||
$zipArchive->close();
|
||||
}
|
||||
|
||||
if (file_exists($path.$zipFileName)) {
|
||||
return response()->download($path.$zipFileName)->deleteFileAfterSend(true);
|
||||
}
|
||||
|
||||
return redirect()->back()->withErrors(trans('common.something-went-wrong'));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user