This commit is contained in:
User
2024-12-27 20:43:18 -05:00
parent 89d0394de9
commit da918835c2
84 changed files with 6160 additions and 1068 deletions

View File

@@ -30,7 +30,7 @@ class NewPasswordController extends Controller
/**
* Handle an incoming new password request.
*
* @throws \Illuminate\Validation\ValidationException
* @throws ValidationException
*/
public function store(Request $request): RedirectResponse
{

View File

@@ -25,7 +25,7 @@ class PasswordResetLinkController extends Controller
/**
* Handle an incoming password reset link request.
*
* @throws \Illuminate\Validation\ValidationException
* @throws ValidationException
*/
public function store(Request $request): RedirectResponse
{

View File

@@ -10,8 +10,10 @@ use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use Illuminate\Validation\Rules;
use Illuminate\Validation\ValidationException;
use Inertia\Inertia;
use Inertia\Response;
use function Laravel\Prompts\error;
class RegisteredUserController extends Controller
{
@@ -26,13 +28,13 @@ class RegisteredUserController extends Controller
/**
* Handle an incoming registration request.
*
* @throws \Illuminate\Validation\ValidationException
* @throws ValidationException
*/
public function store(Request $request): RedirectResponse
{
$request->validate([
'name' => 'required|string|max:255',
'email' => 'required|string|lowercase|email|max:255|unique:'.User::class,
'email' => 'required|string|lowercase|email|max:255|contains:yumj.in|unique:'.User::class,
'password' => ['required', 'confirmed', Rules\Password::defaults()],
]);

View File

@@ -0,0 +1,230 @@
<?php
namespace App\Http\Controllers;
use App\Models\Author;
use App\Models\Chapter;
use App\Models\Comic;
use App\Models\Image;
use App\Remote\CopyManga;
use App\Remote\ImageFetcher;
use Illuminate\Contracts\Routing\ResponseFactory;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Foundation\Application;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Response as IlluminateHttpResponse;
use Illuminate\Support\Facades\Cache;
use Inertia\Inertia;
use Inertia\Response;
class ComicController extends Controller
{
public function __construct(private readonly CopyManga $copyManga)
{
}
public function favourites(Request $request)
{
$favourites = $request->user()->favourites()->with(['authors'])->orderBy('upstream_updated_at', 'desc')->get();
return Inertia::render('Comic/Favourites', [
'favourites' => $favourites,
]);
}
public function postFavourite(Request $request): JsonResponse
{
try {
// Get pathname to comic_id
$comic = Comic::where('pathword', $request->pathword)->firstOrFail();
} catch (ModelNotFoundException $e) {
// Fetch from remote
$remoteComic = $this->copyManga->comic($request->pathword);
$comic = new Comic;
$comic->pathword = $remoteComic['comic']['path_word'];
$comic->name = $remoteComic['comic']['name'];
$comic->cover = $remoteComic['comic']['cover'];
$comic->upstream_updated_at = $remoteComic['comic']['datetime_updated'];
$comic->uuid = $remoteComic['comic']['uuid'];
$comic->alias = explode(',', $remoteComic['comic']['alias']);
$comic->description = $remoteComic['comic']['brief'];
$comic->metadata = $remoteComic;
$comic->save();
}
// Set favourite
if ($request->user()->favourites()->where('comic_id', $comic->id)->exists()) {
$request->user()->favourites()->detach($comic->id);
} else {
$request->user()->favourites()->attach($comic->id);
}
return response()->json($request->user()->favourites()->get(['pathword'])->pluck('pathword'));
}
public function image(Request $request, string $url): ResponseFactory|Application|IlluminateHttpResponse
{
// TODO: Ref check and make it require auth
$fetcher = new ImageFetcher(base64_decode($url));
return response($fetcher->fetch())->withHeaders([
'Content-Type' => $fetcher->getMimeType()->value,
'Cache-Control' => 'max-age=604800',
]);
}
public function index(Request $request): Response
{
$params = [];
if ($request->has('tag')) {
$params['theme'] = $request->get('tag');
}
$comics = $this->copyManga->comics(30, $request->header('offset', 0), $request->get('top', 'all'), $params);
// Prep the array for upsert
$comicsUpsertArray = [];
$authorsUpsertArray = [];
foreach ($comics['list'] as $comic) {
$comicsUpsertArray[] = [
'pathword' => $comic['path_word'],
'uuid' => '',
'name' => $comic['name'],
'alias' => '{}',
'description' => '',
'cover' => $comic['cover'],
'upstream_updated_at' => $comic['datetime_updated'],
];
foreach ($comic['author'] as $author) {
$authorsUpsertArray[] = [
'name' => $author['name']
];
}
}
// Do an upsert for comics
Comic::upsert($comicsUpsertArray, uniqueBy: 'pathword', update: ['upstream_updated_at']);
Author::upsert($authorsUpsertArray, uniqueBy: 'name');
// Had to do a second pass to insert the relationships
foreach ($comics['list'] as $comic) {
// Get the comic id
$comicObj = Comic::where('pathword', $comic['path_word'])->first();
foreach ($comic['author'] as $author) {
$authorObj = Author::where('name', $author['name'])->first();
$comicObj->authors()->sync($authorObj);
}
}
return Inertia::render('Comic/Index', [
'comics' => $comics,
'offset' => $request->header('offset', 0)
]);
}
public function chapters(Request $request, string $pathword = ''): Response
{
$comic = $this->copyManga->comic($pathword);
$chapters = $this->copyManga->chapters($pathword, 200, 0, [], $request->get('group', 'default'));
// Get the comic object and fill other parameters
$comicObject = Comic::where('pathword', $pathword)->first();
$comicObject->uuid = $comic['comic']['uuid'];
$comicObject->alias = explode(',', $comic['comic']['alias']);
$comicObject->description = $comic['comic']['brief'];
$comicObject->metadata = $comic;
$comicObject->save();
// Get the authors and update the pathword
foreach ($comic['comic']['author'] as $author) {
$authorObj = Author::where('name', $author['name'])->whereNull('pathword')->first();
if ($authorObj) {
// Do nothing if pathword already exist
$authorObj->pathword = $author['path_word'];
$authorObj->save();
}
}
// Do the Chapters
// Prep the array for upsert
$arrayForUpsert = [];
foreach ($chapters['list'] as $chapter) {
$arrayForUpsert[] = [
'comic_id' => $comicObject->id,
'chapter_uuid' => $chapter['uuid'],
'name' => $chapter['name'],
'order' => $chapter['index'],
'upstream_created_at' => $chapter['datetime_created'],
'metadata' => json_encode($chapter),
];
}
// Do an upsert
Chapter::upsert($arrayForUpsert, uniqueBy: 'chapter_uuid');
return Inertia::render('Comic/Chapters', [
'comic' => $comic,
'chapters' => $chapters,
]);
}
public function read(Request $request, string $pathword = '', string $uuid = ''): Response
{
$comic = $this->copyManga->comic($pathword);
$chapter = $this->copyManga->chapter($pathword, $uuid);
// Get the authors and update the pathword
foreach ($comic['comic']['author'] as $author) {
$authorObj = Author::where('name', $author['name'])->whereNull('pathword')->first();
if ($authorObj) {
// Do nothing if pathword already exist
$authorObj->pathword = $author['path_word'];
$authorObj->save();
}
}
$chapterObj = Chapter::where('chapter_uuid', $chapter['chapter']['uuid'])->first();
$comicObj = Comic::where('pathword', $pathword)->first();
$arrayForUpsert = [];
foreach ($chapter['sorted'] as $k => $image) {
$metadata = $chapter;
unset($metadata['sorted']);
unset($metadata['chapter']['contents'], $metadata['chapter']['words']);
$arrayForUpsert[] = [
'comic_id' => $comicObj->id,
'chapter_id' => $chapterObj->id,
'order' => $k,
'url' => $image['url'],
'metadata' => json_encode($metadata),
];
}
// Do an upsert
Image::upsert($arrayForUpsert, uniqueBy: 'url');
return Inertia::render('Comic/Read', [
'comic' => $comic,
'chapter' => $chapter,
]);
}
public function tags()
{
// TODO
$tags = $this->copyManga->tags();
Cache::forever('tags', $tags);
return response()->json($tags);
}
}

View File

@@ -2,7 +2,9 @@
namespace App\Http\Middleware;
use App\Http\Resources\UserCollection;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Inertia\Middleware;
class HandleInertiaRequests extends Middleware
@@ -31,8 +33,9 @@ class HandleInertiaRequests extends Middleware
{
return [
...parent::share($request),
'tags' => Cache::get('tags'),
'auth' => [
'user' => $request->user(),
'user' => $request->user() ? (new UserCollection($request->user()))->toArray($request) : null,
],
];
}

View File

@@ -3,6 +3,7 @@
namespace App\Http\Requests\Auth;
use Illuminate\Auth\Events\Lockout;
use Illuminate\Contracts\Validation\ValidationRule;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\RateLimiter;
@@ -22,7 +23,7 @@ class LoginRequest extends FormRequest
/**
* Get the validation rules that apply to the request.
*
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
* @return array<string, ValidationRule|array<mixed>|string>
*/
public function rules(): array
{
@@ -35,7 +36,7 @@ class LoginRequest extends FormRequest
/**
* Attempt to authenticate the request's credentials.
*
* @throws \Illuminate\Validation\ValidationException
* @throws ValidationException
*/
public function authenticate(): void
{
@@ -55,7 +56,7 @@ class LoginRequest extends FormRequest
/**
* Ensure the login request is not rate limited.
*
* @throws \Illuminate\Validation\ValidationException
* @throws ValidationException
*/
public function ensureIsNotRateLimited(): void
{

View File

@@ -3,6 +3,7 @@
namespace App\Http\Requests;
use App\Models\User;
use Illuminate\Contracts\Validation\ValidationRule;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;
@@ -11,7 +12,7 @@ class ProfileUpdateRequest extends FormRequest
/**
* Get the validation rules that apply to the request.
*
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
* @return array<string, ValidationRule|array<mixed>|string>
*/
public function rules(): array
{
@@ -19,6 +20,7 @@ class ProfileUpdateRequest extends FormRequest
'name' => ['required', 'string', 'max:255'],
'email' => [
'required',
'contains:@yumj.in',
'string',
'lowercase',
'email',

View File

@@ -0,0 +1,24 @@
<?php
namespace App\Http\Resources;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\ResourceCollection;
class UserCollection extends ResourceCollection
{
/**
* Transform the resource collection into an array.
*
* @return array<int|string, mixed>
*/
public function toArray(Request $request): array
{
return [
'id' => $request->user()->id,
'name' => $request->user()->name,
'email' => $request->user()->email,
'favourites' => $request->user()->favourites()->get(['pathword'])->pluck('pathword')
];
}
}