mirror of
https://github.com/HDInnovations/UNIT3D-Community-Edition.git
synced 2026-04-22 01:38:49 -05:00
refactor: use route model binding, mass assignment and form requests for subtitles
This commit is contained in:
@@ -26,6 +26,8 @@ use App\Achievements\UserUploaded700Subtitles;
|
||||
use App\Achievements\UserUploaded800Subtitles;
|
||||
use App\Achievements\UserUploaded900Subtitles;
|
||||
use App\Achievements\UserUploadedFirstSubtitle;
|
||||
use App\Http\Requests\StoreSubtitleRequest;
|
||||
use App\Http\Requests\UpdateSubtitleRequest;
|
||||
use App\Models\MediaLanguage;
|
||||
use App\Models\Subtitle;
|
||||
use App\Models\Torrent;
|
||||
@@ -54,10 +56,10 @@ class SubtitleController extends Controller
|
||||
/**
|
||||
* Show the form for creating a new resource.
|
||||
*/
|
||||
public function create(int $torrentId): \Illuminate\Contracts\View\Factory|\Illuminate\View\View
|
||||
public function create(Request $request): \Illuminate\Contracts\View\Factory|\Illuminate\View\View
|
||||
{
|
||||
return view('subtitle.create', [
|
||||
'torrent' => Torrent::withAnyStatus()->findOrFail($torrentId),
|
||||
'torrent' => Torrent::withAnyStatus()->findOrFail($request->integer('torrent_id')),
|
||||
'media_languages' => MediaLanguage::orderBy('name')->get(),
|
||||
]);
|
||||
}
|
||||
@@ -65,58 +67,48 @@ class SubtitleController extends Controller
|
||||
/**
|
||||
* Store a newly created resource in storage.
|
||||
*/
|
||||
public function store(Request $request): \Illuminate\Http\RedirectResponse
|
||||
public function store(StoreSubtitleRequest $request): \Illuminate\Http\RedirectResponse
|
||||
{
|
||||
$user = $request->user();
|
||||
$subtitleFile = $request->file('subtitle_file');
|
||||
$filename = uniqid('', true).'.'.$subtitleFile->getClientOriginalExtension();
|
||||
|
||||
$subtitle = new Subtitle();
|
||||
$subtitle->title = $request->input('torrent_name');
|
||||
$subtitle->file_name = $filename;
|
||||
$subtitle->file_size = $subtitleFile->getSize();
|
||||
$subtitle->extension = '.'.$subtitleFile->getClientOriginalExtension();
|
||||
$subtitle->language_id = $request->input('language_id');
|
||||
$subtitle->note = $request->input('note');
|
||||
$subtitle->downloads = 0;
|
||||
$subtitle->verified = 0;
|
||||
$subtitle->user_id = $user->id;
|
||||
$subtitle->anon = $request->input('anonymous');
|
||||
$subtitle->torrent_id = $request->input('torrent_id');
|
||||
$subtitle->status = 1;
|
||||
$subtitle->moderated_at = now();
|
||||
$subtitle->moderated_by = 1;
|
||||
|
||||
$v = validator($subtitle->toArray(), [
|
||||
'title' => 'required',
|
||||
'file_name' => 'required',
|
||||
'file_size' => 'required',
|
||||
'extension' => 'required|in:.srt,.ass,.sup,.zip',
|
||||
'language_id' => 'required',
|
||||
'user_id' => 'required',
|
||||
'torrent_id' => 'required',
|
||||
]);
|
||||
|
||||
if ($v->fails()) {
|
||||
return to_route('subtitles.create', ['torrent_id' => $request->input('torrent_id')])
|
||||
->withErrors($v->errors());
|
||||
}
|
||||
$subtitle = Subtitle::create([
|
||||
'title' => Torrent::findOrFail($request->integer('torrent_id'))->name,
|
||||
'file_name' => $filename,
|
||||
'file_size' => $subtitleFile->getSize(),
|
||||
'extension' => '.'.$subtitleFile->getClientOriginalExtension(),
|
||||
'downloads' => 0,
|
||||
'verified' => 0,
|
||||
'user_id' => $user->id,
|
||||
'status' => 1,
|
||||
'moderated_at' => now(),
|
||||
'moderated_by' => 1,
|
||||
] + $request->safe()->except('subtitle_file'));
|
||||
|
||||
// Save Subtitle
|
||||
Storage::disk('subtitles')->put($filename, file_get_contents($subtitleFile));
|
||||
$subtitle->save();
|
||||
|
||||
// Announce To Shoutbox
|
||||
$torrentUrl = href_torrent($subtitle->torrent);
|
||||
$profileUrl = href_profile($user);
|
||||
|
||||
if (! $subtitle->anon) {
|
||||
$this->chatRepository->systemMessage(
|
||||
sprintf('[url=%s]%s[/url] has uploaded a new %s subtitle for [url=%s]%s[/url]', $profileUrl, $user->username, $subtitle->language->name, $torrentUrl, $subtitle->torrent->name)
|
||||
sprintf(
|
||||
'[url=%s]%s[/url] has uploaded a new %s subtitle for [url=%s]%s[/url]',
|
||||
href_profile($user),
|
||||
$user->username,
|
||||
$subtitle->language->name,
|
||||
href_torrent($subtitle->torrent),
|
||||
$subtitle->torrent->name
|
||||
)
|
||||
);
|
||||
} else {
|
||||
$this->chatRepository->systemMessage(
|
||||
sprintf('An anonymous user has uploaded a new %s subtitle for [url=%s]%s[/url]', $subtitle->language->name, $torrentUrl, $subtitle->torrent->name)
|
||||
sprintf(
|
||||
'An anonymous user has uploaded a new %s subtitle for [url=%s]%s[/url]',
|
||||
$subtitle->language->name,
|
||||
href_torrent($subtitle->torrent),
|
||||
$subtitle->torrent->name
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
@@ -142,26 +134,11 @@ class SubtitleController extends Controller
|
||||
/**
|
||||
* Update the specified resource in storage.
|
||||
*/
|
||||
public function update(Request $request, int $id): \Illuminate\Http\RedirectResponse
|
||||
public function update(UpdateSubtitleRequest $request, Subtitle $subtitle): \Illuminate\Http\RedirectResponse
|
||||
{
|
||||
$subtitle = Subtitle::findOrFail($id);
|
||||
abort_unless($request->user()->group->is_modo || $request->user()->id == $subtitle->user_id, 403);
|
||||
|
||||
$user = $request->user();
|
||||
abort_unless($user->group->is_modo || $user->id == $subtitle->user_id, 403);
|
||||
|
||||
$subtitle->language_id = $request->input('language_id');
|
||||
$subtitle->note = $request->input('note');
|
||||
|
||||
$v = validator($subtitle->toArray(), [
|
||||
'language_id' => 'required',
|
||||
]);
|
||||
|
||||
if ($v->fails()) {
|
||||
return to_route('torrents.show', ['id' => $request->input('torrent_id')])
|
||||
->withErrors($v->errors());
|
||||
}
|
||||
|
||||
$subtitle->save();
|
||||
$subtitle->update($request->validated());
|
||||
|
||||
return to_route('torrents.show', ['id' => $request->input('torrent_id')])
|
||||
->withSuccess('Subtitle Successfully Updated');
|
||||
@@ -172,12 +149,11 @@ class SubtitleController extends Controller
|
||||
*
|
||||
* @throws Exception
|
||||
*/
|
||||
public function destroy(Request $request, int $id): \Illuminate\Http\RedirectResponse
|
||||
public function destroy(Request $request, Subtitle $subtitle): \Illuminate\Http\RedirectResponse
|
||||
{
|
||||
$subtitle = Subtitle::findOrFail($id);
|
||||
|
||||
$user = $request->user();
|
||||
abort_unless($user->group->is_modo || $user->id == $subtitle->user_id, 403);
|
||||
|
||||
abort_unless($user->group->is_modo || $user->id === $subtitle->user_id, 403);
|
||||
|
||||
if (Storage::disk('subtitles')->exists($subtitle->file_name)) {
|
||||
Storage::disk('subtitles')->delete($subtitle->file_name);
|
||||
@@ -192,9 +168,8 @@ class SubtitleController extends Controller
|
||||
/**
|
||||
* Download the specified resource from storage.
|
||||
*/
|
||||
public function download(Request $request, int $id): \Illuminate\Http\RedirectResponse|\Symfony\Component\HttpFoundation\BinaryFileResponse|\Symfony\Component\HttpFoundation\StreamedResponse
|
||||
public function download(Request $request, Subtitle $subtitle): \Illuminate\Http\RedirectResponse|\Symfony\Component\HttpFoundation\BinaryFileResponse|\Symfony\Component\HttpFoundation\StreamedResponse
|
||||
{
|
||||
$subtitle = Subtitle::findOrFail($id);
|
||||
$user = $request->user();
|
||||
|
||||
// User's download rights are revoked
|
||||
|
||||
@@ -0,0 +1,60 @@
|
||||
<?php
|
||||
/**
|
||||
* NOTICE OF LICENSE.
|
||||
*
|
||||
* UNIT3D Community Edition is open-sourced software licensed under the GNU Affero General Public License v3.0
|
||||
* The details is bundled with this project in the file LICENSE.txt.
|
||||
*
|
||||
* @project UNIT3D Community Edition
|
||||
*
|
||||
* @author Roardom <roardom@protonmail.com>
|
||||
* @license https://www.gnu.org/licenses/agpl-3.0.en.html/ GNU Affero General Public License v3.0
|
||||
*/
|
||||
|
||||
namespace App\Http\Requests;
|
||||
|
||||
use App\Models\User;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
class StoreSubtitleRequest extends FormRequest
|
||||
{
|
||||
/**
|
||||
* Determine if the user is authorized to make this request.
|
||||
*/
|
||||
public function authorize(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
*/
|
||||
public function rules(Request $request): array
|
||||
{
|
||||
return [
|
||||
'subtitle_file' => [
|
||||
'required',
|
||||
'mimes:srt,ass,sup,zip',
|
||||
],
|
||||
'language_id' => [
|
||||
'required',
|
||||
Rule::exists('media_languages', 'id'),
|
||||
],
|
||||
'note' => [
|
||||
'required',
|
||||
'max:65535',
|
||||
],
|
||||
'anon' => [
|
||||
'required',
|
||||
'boolean',
|
||||
],
|
||||
'torrent_id' => [
|
||||
'required',
|
||||
'integer',
|
||||
Rule::exists('torrents', 'id'),
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
<?php
|
||||
/**
|
||||
* NOTICE OF LICENSE.
|
||||
*
|
||||
* UNIT3D Community Edition is open-sourced software licensed under the GNU Affero General Public License v3.0
|
||||
* The details is bundled with this project in the file LICENSE.txt.
|
||||
*
|
||||
* @project UNIT3D Community Edition
|
||||
*
|
||||
* @author Roardom <roardom@protonmail.com>
|
||||
* @license https://www.gnu.org/licenses/agpl-3.0.en.html/ GNU Affero General Public License v3.0
|
||||
*/
|
||||
|
||||
namespace App\Http\Requests;
|
||||
|
||||
use App\Models\User;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
class UpdateSubtitleRequest extends FormRequest
|
||||
{
|
||||
/**
|
||||
* Determine if the user is authorized to make this request.
|
||||
*/
|
||||
public function authorize(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
*/
|
||||
public function rules(Request $request): array
|
||||
{
|
||||
return [
|
||||
'language_id' => [
|
||||
'required',
|
||||
Rule::exists('media_languages', 'id'),
|
||||
],
|
||||
'note' => [
|
||||
'required',
|
||||
'max:65535',
|
||||
],
|
||||
'anon' => [
|
||||
'required',
|
||||
'boolean',
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -21,6 +21,8 @@ class Subtitle extends Model
|
||||
{
|
||||
use Auditable;
|
||||
|
||||
protected $guarded = [];
|
||||
|
||||
/**
|
||||
* Belongs To A User.
|
||||
*/
|
||||
|
||||
@@ -80,7 +80,7 @@
|
||||
<li class="data-table__action">
|
||||
<a
|
||||
class="form__button form__button--text"
|
||||
href="{{ route('subtitles.download', ['id' => $subtitle->id]) }}"
|
||||
href="{{ route('subtitles.download', ['subtitle' => $subtitle]) }}"
|
||||
>
|
||||
{{ __('common.download') }}
|
||||
</a>
|
||||
|
||||
@@ -32,7 +32,6 @@
|
||||
>
|
||||
@csrf
|
||||
<input name="torrent_id" type="hidden" value="{{ $torrent->id }}">
|
||||
<input name="torrent_name" type="hidden" value="{{ $torrent->name }}">
|
||||
<p class="form__group">
|
||||
<label for="subtitle_file" class="form__label">
|
||||
{{ __('subtitle.subtitle-file') }} ({{ __('subtitle.subtitle-file-types') }})
|
||||
@@ -71,22 +70,23 @@
|
||||
id="note"
|
||||
class="form__text"
|
||||
placeholder=" "
|
||||
required
|
||||
>
|
||||
<label class="form__label form__label--floating" for="note">
|
||||
{{ __('subtitle.note') }} ({{ __('subtitle.note-help') }})
|
||||
</label>
|
||||
</p>
|
||||
<p class="form__group">
|
||||
<input type="hidden" name="anonymous" value="0">
|
||||
<input type="hidden" name="anon" value="0">
|
||||
<input
|
||||
id="anonymous"
|
||||
id="anon"
|
||||
class="form__checkbox"
|
||||
name="anonymous"
|
||||
name="anon"
|
||||
type="checkbox"
|
||||
value="1"
|
||||
@checked(old('anonymous'))
|
||||
@checked(old('anon'))
|
||||
>
|
||||
<label class="form__label" for="anonymous">{{ __('common.anonymous') }}?</label>
|
||||
<label class="form__label" for="anon">{{ __('common.anonymous') }}?</label>
|
||||
</p>
|
||||
<p class="form__group">
|
||||
<button class="form__button form__button--filled">
|
||||
|
||||
@@ -45,7 +45,7 @@
|
||||
<menu class="data-table__actions">
|
||||
<li class="data-table__action">
|
||||
<a
|
||||
href="{{ route('subtitles.download', ['id' => $subtitle->id]) }}"
|
||||
href="{{ route('subtitles.download', ['subtitle' => $subtitle]) }}"
|
||||
class="form__button form__button--text"
|
||||
title="{{ __('common.download') }}"
|
||||
download
|
||||
@@ -70,10 +70,11 @@
|
||||
<form
|
||||
class="dialog__form"
|
||||
method="POST"
|
||||
action="{{ route('subtitles.update', ['id' => $subtitle->id]) }}"
|
||||
action="{{ route('subtitles.update', ['subtitle' => $subtitle]) }}"
|
||||
x-on:click.outside="$refs.dialog.close()"
|
||||
>
|
||||
@csrf
|
||||
@method('PATCH')
|
||||
<input id="torrent_id" name="torrent_id" type="hidden" value="{{ $torrent->id }}">
|
||||
<p class="form__group">
|
||||
<select class="form__select" id="language_id" name="language_id" required>
|
||||
@@ -86,7 +87,7 @@
|
||||
</option>
|
||||
@endforeach
|
||||
</select>
|
||||
<label class="form__label form__label--floating" for="torrent_id">{{ __('common.language') }}</label>
|
||||
<label class="form__label form__label--floating" for="language_id">{{ __('common.language') }}</label>
|
||||
</p>
|
||||
<p class="form__group">
|
||||
<input
|
||||
@@ -99,6 +100,18 @@
|
||||
>
|
||||
<label class="form__label form__label--floating" for="note">{{ __('subtitle.note') }}</label>
|
||||
</p>
|
||||
<p class="form__group">
|
||||
<input type="hidden" name="anon" value="0">
|
||||
<input
|
||||
id="anon"
|
||||
class="form__checkbox"
|
||||
name="anon"
|
||||
type="checkbox"
|
||||
value="1"
|
||||
@checked($subtitle->anon)
|
||||
>
|
||||
<label class="form__label" for="anon">{{ __('common.anonymous') }}?</label>
|
||||
</p>
|
||||
<p class="form__group">
|
||||
<button class="form__button form__button--filled">
|
||||
{{ __('common.save') }}
|
||||
@@ -114,7 +127,7 @@
|
||||
<li class="data-table__action">
|
||||
<form
|
||||
method="POST"
|
||||
action="{{ route('subtitles.destroy', ['id' => $subtitle->id]) }}"
|
||||
action="{{ route('subtitles.destroy', ['subtitle' => $subtitle]) }}"
|
||||
x-data
|
||||
style="display: inline"
|
||||
>
|
||||
|
||||
+5
-5
@@ -259,11 +259,11 @@ Route::middleware('language')->group(function (): void {
|
||||
Route::prefix('subtitles')->group(function (): void {
|
||||
Route::name('subtitles.')->group(function (): void {
|
||||
Route::get('/', [App\Http\Controllers\SubtitleController::class, 'index'])->name('index');
|
||||
Route::get('/create/{torrent_id}', [App\Http\Controllers\SubtitleController::class, 'create'])->where('id', '[0-9]+')->name('create');
|
||||
Route::post('/store', [App\Http\Controllers\SubtitleController::class, 'store'])->name('store');
|
||||
Route::post('/{id}/update', [App\Http\Controllers\SubtitleController::class, 'update'])->name('update');
|
||||
Route::delete('/{id}/delete', [App\Http\Controllers\SubtitleController::class, 'destroy'])->name('destroy');
|
||||
Route::get('/{id}/download', [App\Http\Controllers\SubtitleController::class, 'download'])->name('download');
|
||||
Route::get('/create', [App\Http\Controllers\SubtitleController::class, 'create'])->name('create');
|
||||
Route::post('/', [App\Http\Controllers\SubtitleController::class, 'store'])->name('store');
|
||||
Route::patch('/{subtitle}', [App\Http\Controllers\SubtitleController::class, 'update'])->name('update');
|
||||
Route::delete('/{subtitle}', [App\Http\Controllers\SubtitleController::class, 'destroy'])->name('destroy');
|
||||
Route::get('/{subtitle}/download', [App\Http\Controllers\SubtitleController::class, 'download'])->name('download');
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user