Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions app/Attributes/FromCurrentUserId.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace App\Attributes;

use Attribute;
use Spatie\LaravelData\Attributes\FromAuthenticatedUserProperty;

#[Attribute(Attribute::TARGET_PROPERTY)]
class FromCurrentUserId extends FromAuthenticatedUserProperty
{
public function __construct() {
parent::__construct(null, 'id', true);
}
}
37 changes: 37 additions & 0 deletions app/Data/Staff/ArticleData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

/**
* 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\Data\Staff;

use Illuminate\Http\UploadedFile;
use App\Attributes\FromCurrentUserId;
use Spatie\LaravelData\Attributes\Validation\Max;
use Spatie\LaravelData\Data;

class ArticleData extends Data
{
public function __construct(
#[Max(255)]
public string $title,
#[Max(65535)]
public string $content,
#[Max(10240)]
public ?UploadedFile $image,
#[FromCurrentUserId]
public int $userId,
) {
}
}
64 changes: 64 additions & 0 deletions app/Data/Staff/AutomaticTorrentFreeleechData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

declare(strict_types=1);

/**
* 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\Data\Staff;

use App\Models\Category;
use App\Models\Resolution;
use App\Models\Type;
use Closure;
use Spatie\LaravelData\Attributes\MergeValidationRules;
use Spatie\LaravelData\Attributes\Validation\Exists;
use Spatie\LaravelData\Attributes\Validation\Max;
use Spatie\LaravelData\Attributes\Validation\Min;
use Spatie\LaravelData\Data;

#[MergeValidationRules]
class AutomaticTorrentFreeleechData extends Data
{
public function __construct(
public ?string $nameRegex,
#[Min(0)]
public ?int $position,
#[Min(0)]
public ?int $size,
#[Exists(Category::class)]
public ?int $categoryId,
#[Exists(Type::class)]
public ?int $typeId,
#[Exists(Resolution::class)]
public ?int $resolutionId,
#[Min(0), Max(100)]
public int $freeleechPercentage,
) {
}

/**
* @return array<string, array<Closure>>
*/
public static function rules(): array
{
return [
'name_regex' => [
function (string $attribute, mixed $value, Closure $fail): void {
if (@preg_match($value, 'Validate regex') === false) {
$fail('Regex syntax error.');
}
},
],
];
}
}
36 changes: 36 additions & 0 deletions app/Data/Staff/BanData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

/**
* 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\Data\Staff;

use App\Attributes\FromCurrentUserId;
use App\Models\User;
use Spatie\LaravelData\Attributes\Validation\Exists;
use Spatie\LaravelData\Attributes\Validation\Max;
use Spatie\LaravelData\Data;

class BanData extends Data
{
public function __construct(
#[Max(65535)]
public string $banReason,
#[Exists(User::class, 'id')]
public int $ownedBy,
#[FromCurrentUserId]
public int $createdBy,
) {
}
}
28 changes: 28 additions & 0 deletions app/Data/Staff/TypeData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

/**
* 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\Data\Staff;

use Spatie\LaravelData\Data;

class TypeData extends Data
{
public function __construct(
public string $name,
public int $position,
) {
}
}
31 changes: 11 additions & 20 deletions app/Http/Controllers/Staff/ArticleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@

namespace App\Http\Controllers\Staff;

use App\Data\Staff\ArticleData;
use App\Http\Controllers\Controller;
use App\Http\Requests\Staff\StoreArticleRequest;
use App\Http\Requests\Staff\UpdateArticleRequest;
use App\Models\Article;
use App\Models\UnreadArticle;
use App\Models\User;
Expand Down Expand Up @@ -55,19 +54,15 @@ public function create(): \Illuminate\Contracts\View\Factory|\Illuminate\View\Vi
/**
* Store A New Article.
*/
public function store(StoreArticleRequest $request): \Illuminate\Http\RedirectResponse
public function store(ArticleData $data): \Illuminate\Http\RedirectResponse
{
if ($request->hasFile('image')) {
$image = $request->file('image');

abort_if(\is_array($image), 400);

$filename = 'article-'.uniqid('', true).'.'.$image->getClientOriginalExtension();
if ($data->image !== null) {
$filename = 'article-'.uniqid('', true).'.'.$data->image->getClientOriginalExtension();
$path = Storage::disk('article-images')->path($filename);
Image::make($image->getRealPath())->fit(75, 75)->encode('png', 100)->save($path);
Image::make($data->image->getRealPath())->fit(75, 75)->encode('png', 100)->save($path);
}

$article = Article::create(['user_id' => $request->user()->id, 'image' => $filename ?? null] + $request->validated());
$article = Article::create(['image' => $filename ?? null] + $data->toArray());

UnreadArticle::query()->insertUsing(
['article_id', 'user_id'],
Expand All @@ -94,23 +89,19 @@ public function edit(Article $article): \Illuminate\Contracts\View\Factory|\Illu
/**
* Edit A Article.
*/
public function update(UpdateArticleRequest $request, Article $article): \Illuminate\Http\RedirectResponse
public function update(ArticleData $data, Article $article): \Illuminate\Http\RedirectResponse
{
if ($request->hasFile('image')) {
$image = $request->file('image');

abort_if(\is_array($image), 400);

$filename = 'article-'.uniqid('', true).'.'.$image->getClientOriginalExtension();
if ($data->image !== null) {
$filename = 'article-'.uniqid('', true).'.'.$data->image->getClientOriginalExtension();
$path = Storage::disk('article-images')->path($filename);
Image::make($image->getRealPath())->fit(75, 75)->encode('png', 100)->save($path);
Image::make($data->image->getRealPath())->fit(75, 75)->encode('png', 100)->save($path);

if ($article->image !== null) {
Storage::disk('article-images')->delete($article->image);
}
}

$article->update(['image' => $filename ?? null,] + $request->validated());
$article->update(['image' => $filename ?? null,] + $data->toArray());

return to_route('staff.articles.index')
->with('success', 'Your article changes have successfully published!');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@

namespace App\Http\Controllers\Staff;

use App\Data\Staff\AutomaticTorrentFreeleechData;
use App\Http\Controllers\Controller;
use App\Http\Requests\Staff\StoreAutomaticTorrentFreeleechRequest;
use App\Http\Requests\Staff\UpdateAutomaticTorrentFreeleechRequest;
use App\Models\AutomaticTorrentFreeleech;
use App\Models\Category;
use App\Models\Resolution;
Expand All @@ -42,9 +41,9 @@ public function create(): \Illuminate\Contracts\View\Factory|\Illuminate\View\Vi
]);
}

public function store(StoreAutomaticTorrentFreeleechRequest $request): \Illuminate\Http\RedirectResponse
public function store(AutomaticTorrentFreeleechData $data): \Illuminate\Http\RedirectResponse
{
AutomaticTorrentFreeleech::create($request->validated());
AutomaticTorrentFreeleech::create($data->toArray());

return to_route('staff.automatic_torrent_freeleeches.index')
->with('success', 'Resolution Successfully Added');
Expand All @@ -60,9 +59,9 @@ public function edit(AutomaticTorrentFreeleech $automaticTorrentFreeleech): \Ill
]);
}

public function update(UpdateAutomaticTorrentFreeleechRequest $request, AutomaticTorrentFreeleech $automaticTorrentFreeleech): \Illuminate\Http\RedirectResponse
public function update(AutomaticTorrentFreeleechData $data, AutomaticTorrentFreeleech $automaticTorrentFreeleech): \Illuminate\Http\RedirectResponse
{
$automaticTorrentFreeleech->update($request->validated());
$automaticTorrentFreeleech->update($data->toArray());

return to_route('staff.automatic_torrent_freeleeches.index')
->with('success', 'Resolution Successfully Modified');
Expand Down
9 changes: 5 additions & 4 deletions app/Http/Controllers/Staff/BanController.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

namespace App\Http\Controllers\Staff;

use App\Data\Staff\BanData;
use App\Http\Controllers\Controller;
use App\Http\Requests\Staff\StoreBanRequest;
use App\Models\Ban;
Expand All @@ -24,6 +25,7 @@
use App\Notifications\UserBan;
use App\Services\Unit3dAnnounce;
use Exception;
use Illuminate\Container\Attributes\CurrentUser;

/**
* @see \Tests\Todo\Feature\Http\Controllers\Staff\BanControllerTest
Expand All @@ -45,10 +47,9 @@ public function index(): \Illuminate\Contracts\View\Factory|\Illuminate\View\Vie
*
* @throws Exception
*/
public function store(StoreBanRequest $request): \Illuminate\Http\RedirectResponse
public function store(BanData $data, #[CurrentUser] User $staff): \Illuminate\Http\RedirectResponse
{
$user = User::findOrFail($request->string('owned_by'));
$staff = $request->user();
$user = User::findOrFail($data->ownedBy);

abort_if($user->group->is_modo || $staff->is($user), 403);

Expand All @@ -57,7 +58,7 @@ public function store(StoreBanRequest $request): \Illuminate\Http\RedirectRespon
'can_download' => 0,
]);

$ban = Ban::create(['created_by' => $staff->id] + $request->validated());
$ban = Ban::create($data->toArray());

cache()->forget('user:'.$user->passkey);

Expand Down
11 changes: 5 additions & 6 deletions app/Http/Controllers/Staff/TypeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@

namespace App\Http\Controllers\Staff;

use App\Data\Staff\TypeData;
use App\Http\Controllers\Controller;
use App\Http\Requests\Staff\StoreTypeRequest;
use App\Http\Requests\Staff\UpdateTypeRequest;
use App\Models\Type;
use Exception;

Expand Down Expand Up @@ -48,9 +47,9 @@ public function create(): \Illuminate\Contracts\View\Factory|\Illuminate\View\Vi
/**
* Store A New Type.
*/
public function store(StoreTypeRequest $request): \Illuminate\Http\RedirectResponse
public function store(TypeData $typeData): \Illuminate\Http\RedirectResponse
{
Type::create($request->validated());
Type::create($typeData->toArray());

return to_route('staff.types.index')
->with('success', 'Type Successfully Added');
Expand All @@ -69,9 +68,9 @@ public function edit(Type $type): \Illuminate\Contracts\View\Factory|\Illuminate
/**
* Edit A Type.
*/
public function update(UpdateTypeRequest $request, Type $type): \Illuminate\Http\RedirectResponse
public function update(TypeData $typeData, Type $type): \Illuminate\Http\RedirectResponse
{
$type->update($request->validated());
$type->update($typeData->toArray());

return to_route('staff.types.index')
->with('success', 'Type Successfully Modified');
Expand Down
Loading
Loading