build: initialize RetroHub with Laravel, Inertia, Vue, and Horizon
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
namespace App\Features\Consoles\Controllers;
|
||||
|
||||
use App\Features\Consoles\Data\ConsoleData;
|
||||
use App\Features\Consoles\Services\ConsoleService;
|
||||
use App\Features\Games\Repositories\GameRepository;
|
||||
use App\Http\Controllers\Controller;
|
||||
use Inertia\Inertia;
|
||||
use Inertia\Response;
|
||||
|
||||
class ConsoleController extends Controller
|
||||
{
|
||||
public function __construct(
|
||||
private ConsoleService $consoleService,
|
||||
private GameRepository $gameRepository
|
||||
) {}
|
||||
|
||||
public function index(): Response
|
||||
{
|
||||
$consoles = $this->consoleService->getAll()
|
||||
->map(fn ($console) => ConsoleData::fromModel($console));
|
||||
|
||||
return Inertia::render('Consoles/Index', [
|
||||
'consoles' => $consoles,
|
||||
]);
|
||||
}
|
||||
|
||||
public function show(string $slug): Response
|
||||
{
|
||||
$console = $this->consoleService->getBySlug($slug);
|
||||
abort_unless($console, 404);
|
||||
|
||||
$games = $this->gameRepository->getByConsole($console->id);
|
||||
|
||||
return Inertia::render('Consoles/Show', [
|
||||
'selectedConsole' => ConsoleData::fromModel($console->loadCount('games')),
|
||||
'games' => $games,
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
namespace App\Features\Consoles\Data;
|
||||
|
||||
use App\Features\Consoles\Models\Console;
|
||||
use Spatie\LaravelData\Attributes\MapInputName;
|
||||
use Spatie\LaravelData\Data;
|
||||
use Spatie\LaravelData\Mappers\SnakeCaseMapper;
|
||||
|
||||
#[MapInputName(SnakeCaseMapper::class)]
|
||||
class ConsoleData extends Data
|
||||
{
|
||||
public function __construct(
|
||||
public int $id,
|
||||
public string $name,
|
||||
public string $slug,
|
||||
public ?string $iconPath,
|
||||
public array $romExtensions,
|
||||
public ?int $igdbPlatformId,
|
||||
public ?int $tgdbPlatformId,
|
||||
public int $gamesCount,
|
||||
) {}
|
||||
|
||||
public static function fromModel(Console $console): self
|
||||
{
|
||||
return new self(
|
||||
id: $console->id,
|
||||
name: $console->name,
|
||||
slug: $console->slug,
|
||||
iconPath: $console->icon_path,
|
||||
romExtensions: $console->rom_extensions,
|
||||
igdbPlatformId: $console->igdb_platform_id,
|
||||
tgdbPlatformId: $console->tgdb_platform_id,
|
||||
gamesCount: $console->games_count ?? 0,
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
namespace App\Features\Consoles\Models;
|
||||
|
||||
use App\Features\Games\Models\Game;
|
||||
use Database\Factories\ConsoleFactory;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
|
||||
class Console extends Model
|
||||
{
|
||||
/** @use HasFactory<ConsoleFactory> */
|
||||
use HasFactory;
|
||||
|
||||
protected static ?string $factory = ConsoleFactory::class;
|
||||
|
||||
protected $fillable = [
|
||||
'name',
|
||||
'slug',
|
||||
'icon_path',
|
||||
'rom_extensions',
|
||||
'igdb_platform_id',
|
||||
'tgdb_platform_id',
|
||||
];
|
||||
|
||||
protected $casts = [
|
||||
'rom_extensions' => 'array',
|
||||
];
|
||||
|
||||
public function games(): HasMany
|
||||
{
|
||||
return $this->hasMany(Game::class);
|
||||
}
|
||||
|
||||
public function getRouteKeyName(): string
|
||||
{
|
||||
return 'slug';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
namespace App\Features\Consoles\Repositories;
|
||||
|
||||
use App\Features\Consoles\Models\Console;
|
||||
use Illuminate\Database\Eloquent\Collection;
|
||||
|
||||
class ConsoleRepository
|
||||
{
|
||||
public function __construct(
|
||||
private Console $model
|
||||
) {}
|
||||
|
||||
public function getAll(): Collection
|
||||
{
|
||||
return $this->model->withCount('games')->orderBy('name')->get();
|
||||
}
|
||||
|
||||
public function getBySlug(string $slug): ?Console
|
||||
{
|
||||
return $this->model->where('slug', $slug)->first();
|
||||
}
|
||||
|
||||
public function getWithGamesCount(): Collection
|
||||
{
|
||||
return $this->model->withCount('games')->orderBy('name')->get();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
namespace App\Features\Consoles\Services;
|
||||
|
||||
use App\Features\Consoles\Repositories\ConsoleRepository;
|
||||
use Illuminate\Database\Eloquent\Collection;
|
||||
|
||||
class ConsoleService
|
||||
{
|
||||
public function __construct(
|
||||
private ConsoleRepository $repository
|
||||
) {}
|
||||
|
||||
public function getAll(): Collection
|
||||
{
|
||||
return $this->repository->getAll();
|
||||
}
|
||||
|
||||
public function getBySlug(string $slug)
|
||||
{
|
||||
return $this->repository->getBySlug($slug);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,101 @@
|
||||
<?php
|
||||
|
||||
namespace App\Features\Games\Controllers;
|
||||
|
||||
use App\Features\Consoles\Models\Console;
|
||||
use App\Features\Games\Data\GameData;
|
||||
use App\Features\Games\Repositories\GameRepository;
|
||||
use App\Features\Games\Repositories\RomFileRepository;
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Inertia\Inertia;
|
||||
use Inertia\Response;
|
||||
use Symfony\Component\HttpFoundation\StreamedResponse;
|
||||
|
||||
class GameController extends Controller
|
||||
{
|
||||
public function __construct(
|
||||
private GameRepository $gameRepository,
|
||||
private RomFileRepository $romFileRepository
|
||||
) {}
|
||||
|
||||
public function show(string $slug): Response
|
||||
{
|
||||
$game = $this->gameRepository->getBySlug($slug);
|
||||
abort_unless($game, 404);
|
||||
|
||||
return Inertia::render('Games/Show', [
|
||||
'game' => GameData::fromModel($game),
|
||||
]);
|
||||
}
|
||||
|
||||
public function edit(string $slug): Response
|
||||
{
|
||||
$game = $this->gameRepository->getBySlug($slug);
|
||||
abort_unless($game, 404);
|
||||
|
||||
return Inertia::render('Games/Edit', [
|
||||
'game' => GameData::fromModel($game),
|
||||
'consoles' => Console::orderBy('name')->get(),
|
||||
]);
|
||||
}
|
||||
|
||||
public function update(Request $request, string $slug): RedirectResponse
|
||||
{
|
||||
$game = $this->gameRepository->getBySlug($slug);
|
||||
abort_unless($game, 404);
|
||||
|
||||
$validated = $request->validate([
|
||||
'title' => 'required|string|max:255',
|
||||
'description' => 'nullable|string',
|
||||
'release_year' => 'nullable|integer|min:1970|max:2030',
|
||||
'developer' => 'nullable|string|max:255',
|
||||
'publisher' => 'nullable|string|max:255',
|
||||
'genre' => 'nullable|string|max:255',
|
||||
'rating' => 'nullable|numeric|min:0|max:10',
|
||||
]);
|
||||
|
||||
$game->update($validated);
|
||||
|
||||
return redirect()->route('games.show', $game)
|
||||
->with('success', 'Game updated successfully.');
|
||||
}
|
||||
|
||||
public function destroy(string $slug): RedirectResponse
|
||||
{
|
||||
$game = $this->gameRepository->getBySlug($slug);
|
||||
abort_unless($game, 404);
|
||||
|
||||
$romFiles = $this->romFileRepository->getByGame($game->id);
|
||||
foreach ($romFiles as $romFile) {
|
||||
Storage::disk('local')->delete($romFile->file_path);
|
||||
$romFile->delete();
|
||||
}
|
||||
|
||||
if ($game->cover_path) {
|
||||
Storage::disk('local')->delete($game->cover_path);
|
||||
}
|
||||
|
||||
$console = $game->console;
|
||||
$game->delete();
|
||||
|
||||
return redirect()->route('consoles.show', $console->slug)
|
||||
->with('success', 'Game deleted successfully.');
|
||||
}
|
||||
|
||||
public function download(string $slug, int $romFileId): StreamedResponse
|
||||
{
|
||||
$game = $this->gameRepository->getBySlug($slug);
|
||||
abort_unless($game, 404);
|
||||
|
||||
$romFile = $this->romFileRepository->getByGame($game->id)->where('id', $romFileId)->first();
|
||||
abort_unless($romFile, 404);
|
||||
|
||||
return Storage::disk('local')->download(
|
||||
$romFile->file_path,
|
||||
$romFile->file_name
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
namespace App\Features\Games\Data;
|
||||
|
||||
use Spatie\LaravelData\Attributes\MapInputName;
|
||||
use Spatie\LaravelData\Data;
|
||||
use Spatie\LaravelData\Mappers\SnakeCaseMapper;
|
||||
|
||||
#[MapInputName(SnakeCaseMapper::class)]
|
||||
class DashboardStatsData extends Data
|
||||
{
|
||||
public function __construct(
|
||||
public int $totalGames,
|
||||
public int $totalConsoles,
|
||||
public int $totalRoms,
|
||||
public int $userSaves,
|
||||
) {}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
<?php
|
||||
|
||||
namespace App\Features\Games\Data;
|
||||
|
||||
use App\Features\Consoles\Data\ConsoleData;
|
||||
use App\Features\Games\Models\Game;
|
||||
use Spatie\LaravelData\Attributes\MapInputName;
|
||||
use Spatie\LaravelData\Data;
|
||||
use Spatie\LaravelData\Mappers\SnakeCaseMapper;
|
||||
|
||||
#[MapInputName(SnakeCaseMapper::class)]
|
||||
class GameData extends Data
|
||||
{
|
||||
public function __construct(
|
||||
public int $id,
|
||||
public int $consoleId,
|
||||
public string $title,
|
||||
public string $slug,
|
||||
public ?int $igdbId,
|
||||
public ?int $tgdbId,
|
||||
public ?string $description,
|
||||
public ?int $releaseYear,
|
||||
public ?string $developer,
|
||||
public ?string $publisher,
|
||||
public ?string $genre,
|
||||
public ?float $rating,
|
||||
public ?string $coverPath,
|
||||
public ?string $thumbnailPath,
|
||||
public ?ConsoleData $console,
|
||||
public ?array $romFiles,
|
||||
) {}
|
||||
|
||||
public static function fromModel(Game $game): self
|
||||
{
|
||||
return new self(
|
||||
id: $game->id,
|
||||
consoleId: $game->console_id,
|
||||
title: $game->title,
|
||||
slug: $game->slug,
|
||||
igdbId: $game->igdb_id,
|
||||
tgdbId: $game->tgdb_id,
|
||||
description: $game->description,
|
||||
releaseYear: $game->release_year,
|
||||
developer: $game->developer,
|
||||
publisher: $game->publisher,
|
||||
genre: $game->genre,
|
||||
rating: $game->rating ? (float) $game->rating : null,
|
||||
coverPath: $game->cover_path,
|
||||
thumbnailPath: $game->thumbnail_path,
|
||||
console: $game->relationLoaded('console') && $game->console
|
||||
? ConsoleData::fromModel($game->console)
|
||||
: null,
|
||||
romFiles: $game->relationLoaded('romFiles')
|
||||
? $game->romFiles->map(fn ($rom) => RomFileData::fromModel($rom))->toArray()
|
||||
: null,
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
namespace App\Features\Games\Data;
|
||||
|
||||
use App\Features\Games\Models\RomFile;
|
||||
use Spatie\LaravelData\Attributes\MapInputName;
|
||||
use Spatie\LaravelData\Data;
|
||||
use Spatie\LaravelData\Mappers\SnakeCaseMapper;
|
||||
|
||||
#[MapInputName(SnakeCaseMapper::class)]
|
||||
class RomFileData extends Data
|
||||
{
|
||||
public function __construct(
|
||||
public int $id,
|
||||
public int $gameId,
|
||||
public string $filePath,
|
||||
public string $fileName,
|
||||
public int $fileSize,
|
||||
public string $fileHash,
|
||||
public ?string $region,
|
||||
public ?string $language,
|
||||
) {}
|
||||
|
||||
public static function fromModel(RomFile $romFile): self
|
||||
{
|
||||
return new self(
|
||||
id: $romFile->id,
|
||||
gameId: $romFile->game_id,
|
||||
filePath: $romFile->file_path,
|
||||
fileName: $romFile->file_name,
|
||||
fileSize: $romFile->file_size,
|
||||
fileHash: $romFile->file_hash,
|
||||
region: $romFile->region,
|
||||
language: $romFile->language,
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
<?php
|
||||
|
||||
namespace App\Features\Games\Models;
|
||||
|
||||
use App\Features\Consoles\Models\Console;
|
||||
use App\Features\Settings\Models\SaveFile;
|
||||
use Database\Factories\GameFactory;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class Game extends Model
|
||||
{
|
||||
/** @use HasFactory<GameFactory> */
|
||||
use HasFactory;
|
||||
|
||||
protected static ?string $factory = GameFactory::class;
|
||||
|
||||
protected $fillable = [
|
||||
'console_id',
|
||||
'title',
|
||||
'slug',
|
||||
'igdb_id',
|
||||
'tgdb_id',
|
||||
'description',
|
||||
'release_year',
|
||||
'developer',
|
||||
'publisher',
|
||||
'genre',
|
||||
'rating',
|
||||
'cover_path',
|
||||
'thumbnail_path',
|
||||
];
|
||||
|
||||
protected $casts = [
|
||||
'rating' => 'decimal:1',
|
||||
];
|
||||
|
||||
protected static function booted(): void
|
||||
{
|
||||
static::creating(function (Game $game) {
|
||||
if (empty($game->slug)) {
|
||||
$game->slug = Str::slug($game->title);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public function console(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Console::class);
|
||||
}
|
||||
|
||||
public function romFiles(): HasMany
|
||||
{
|
||||
return $this->hasMany(RomFile::class);
|
||||
}
|
||||
|
||||
public function saveFiles(): HasMany
|
||||
{
|
||||
return $this->hasMany(SaveFile::class);
|
||||
}
|
||||
|
||||
public function getRouteKeyName(): string
|
||||
{
|
||||
return 'slug';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
namespace App\Features\Games\Models;
|
||||
|
||||
use Database\Factories\RomFileFactory;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
|
||||
class RomFile extends Model
|
||||
{
|
||||
/** @use HasFactory<RomFileFactory> */
|
||||
use HasFactory;
|
||||
|
||||
protected static ?string $factory = RomFileFactory::class;
|
||||
|
||||
protected $fillable = [
|
||||
'game_id',
|
||||
'file_path',
|
||||
'file_name',
|
||||
'file_size',
|
||||
'file_hash',
|
||||
'region',
|
||||
'language',
|
||||
];
|
||||
|
||||
public function game(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Game::class);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
<?php
|
||||
|
||||
namespace App\Features\Games\Repositories;
|
||||
|
||||
use App\Features\Games\Models\Game;
|
||||
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
|
||||
use Illuminate\Database\Eloquent\Collection;
|
||||
|
||||
class GameRepository
|
||||
{
|
||||
public function __construct(
|
||||
private Game $model
|
||||
) {}
|
||||
|
||||
public function getByConsole(int $consoleId, int $perPage = 24): LengthAwarePaginator
|
||||
{
|
||||
return $this->model
|
||||
->where('console_id', $consoleId)
|
||||
->with('romFiles')
|
||||
->orderBy('title')
|
||||
->paginate($perPage);
|
||||
}
|
||||
|
||||
public function getBySlug(string $slug): ?Game
|
||||
{
|
||||
return $this->model
|
||||
->with(['console', 'romFiles'])
|
||||
->where('slug', $slug)
|
||||
->first();
|
||||
}
|
||||
|
||||
public function getRecent(int $limit = 10): Collection
|
||||
{
|
||||
return $this->model
|
||||
->with('console')
|
||||
->latest()
|
||||
->take($limit)
|
||||
->get();
|
||||
}
|
||||
|
||||
public function search(string $query): Collection
|
||||
{
|
||||
return $this->model
|
||||
->with('console')
|
||||
->where('title', 'like', "%{$query}%")
|
||||
->orderBy('title')
|
||||
->get();
|
||||
}
|
||||
|
||||
public function getAll()
|
||||
{
|
||||
return $this->model
|
||||
->with('console')
|
||||
->orderBy('title')
|
||||
->paginate(24);
|
||||
}
|
||||
|
||||
public function create(array $data): Game
|
||||
{
|
||||
return $this->model->create($data);
|
||||
}
|
||||
|
||||
public function firstOrCreate(array $conditions, array $values = []): Game
|
||||
{
|
||||
return $this->model->firstOrCreate($conditions, $values);
|
||||
}
|
||||
|
||||
public function count(): int
|
||||
{
|
||||
return $this->model->count();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
namespace App\Features\Games\Repositories;
|
||||
|
||||
use App\Features\Games\Models\RomFile;
|
||||
|
||||
class RomFileRepository
|
||||
{
|
||||
public function __construct(
|
||||
private RomFile $model
|
||||
) {}
|
||||
|
||||
public function count(): int
|
||||
{
|
||||
return $this->model->count();
|
||||
}
|
||||
|
||||
public function findByHash(string $hash): ?RomFile
|
||||
{
|
||||
return $this->model->where('file_hash', $hash)->first();
|
||||
}
|
||||
|
||||
public function create(array $data): RomFile
|
||||
{
|
||||
return $this->model->create($data);
|
||||
}
|
||||
|
||||
public function getByGame(int $gameId)
|
||||
{
|
||||
return $this->model->where('game_id', $gameId)->get();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
<?php
|
||||
|
||||
namespace App\Features\Scraper\Controllers;
|
||||
|
||||
use App\Features\Consoles\Models\Console;
|
||||
use App\Features\Games\Models\Game;
|
||||
use App\Features\Scraper\Data\ScraperResultData;
|
||||
use App\Features\Scraper\Services\ScraperService;
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Inertia\Inertia;
|
||||
use Inertia\Response;
|
||||
|
||||
class ScraperController extends Controller
|
||||
{
|
||||
public function __construct(
|
||||
private ScraperService $scraper
|
||||
) {}
|
||||
|
||||
public function index(): Response
|
||||
{
|
||||
return Inertia::render('Scraper/Index', [
|
||||
'consoles' => Console::orderBy('name')->get(),
|
||||
]);
|
||||
}
|
||||
|
||||
public function search(Request $request): JsonResponse
|
||||
{
|
||||
$request->validate([
|
||||
'query' => 'required|string|min:2',
|
||||
'console' => 'nullable|string',
|
||||
]);
|
||||
|
||||
$results = $this->scraper->search(
|
||||
$request->query('query'),
|
||||
$request->query('console')
|
||||
)->map(fn ($result) => ScraperResultData::from($result));
|
||||
|
||||
return response()->json(['results' => $results]);
|
||||
}
|
||||
|
||||
public function apply(Request $request, string $slug): RedirectResponse
|
||||
{
|
||||
$game = Game::where('slug', $slug)->firstOrFail();
|
||||
|
||||
$request->validate([
|
||||
'cover_url' => 'required|url',
|
||||
]);
|
||||
|
||||
$path = $this->scraper->downloadCover(
|
||||
$request->cover_url,
|
||||
$game
|
||||
);
|
||||
|
||||
$game->update(['cover_path' => $path]);
|
||||
|
||||
return redirect()->route('games.show', $game)
|
||||
->with('success', 'Cover updated successfully.');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace App\Features\Scraper\Data;
|
||||
|
||||
use Spatie\LaravelData\Attributes\MapInputName;
|
||||
use Spatie\LaravelData\Data;
|
||||
use Spatie\LaravelData\Mappers\SnakeCaseMapper;
|
||||
|
||||
#[MapInputName(SnakeCaseMapper::class)]
|
||||
class ScraperResultData extends Data
|
||||
{
|
||||
public function __construct(
|
||||
public int $id,
|
||||
public string $title,
|
||||
public ?string $coverUrl,
|
||||
public ?string $year,
|
||||
public string $source,
|
||||
) {}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
namespace App\Features\Scraper\Services;
|
||||
|
||||
use Illuminate\Support\Collection;
|
||||
|
||||
interface CoverScraperInterface
|
||||
{
|
||||
public function search(string $query, ?int $consoleId = null): Collection;
|
||||
|
||||
public function getCoverUrl(int $externalId): ?string;
|
||||
|
||||
public function getScreenshotUrl(int $externalId): ?string;
|
||||
|
||||
public function getImages(int $externalId): array;
|
||||
}
|
||||
@@ -0,0 +1,120 @@
|
||||
<?php
|
||||
|
||||
namespace App\Features\Scraper\Services;
|
||||
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
|
||||
class IGDBCoverScraper implements CoverScraperInterface
|
||||
{
|
||||
private string $clientId;
|
||||
|
||||
private string $clientSecret;
|
||||
|
||||
private ?string $accessToken = null;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->clientId = config('services.igdb.client_id', '');
|
||||
$this->clientSecret = config('services.igdb.client_secret', '');
|
||||
}
|
||||
|
||||
public function search(string $query, ?int $consoleId = null): Collection
|
||||
{
|
||||
if (empty($this->clientId) || empty($this->clientSecret)) {
|
||||
return collect();
|
||||
}
|
||||
|
||||
$body = "search \"{$query}\"; fields name,cover.url,platforms,release_dates.year;";
|
||||
if ($consoleId) {
|
||||
$body .= " where platforms = [{$consoleId}];";
|
||||
}
|
||||
|
||||
$response = $this->makeRequest('/games', $body);
|
||||
|
||||
return collect($response)->map(fn ($game) => [
|
||||
'id' => $game['id'],
|
||||
'title' => $game['name'],
|
||||
'cover_url' => isset($game['cover']['url'])
|
||||
? str_replace('thumb', 'cover_big', $game['cover']['url'])
|
||||
: null,
|
||||
'year' => $game['release_dates'][0]['year'] ?? null,
|
||||
'source' => 'igdb',
|
||||
]);
|
||||
}
|
||||
|
||||
public function getCoverUrl(int $externalId): ?string
|
||||
{
|
||||
if (empty($this->clientId) || empty($this->clientSecret)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$response = $this->makeRequest(
|
||||
'/covers',
|
||||
"fields url; where game = {$externalId};"
|
||||
);
|
||||
|
||||
if (! empty($response[0]['url'])) {
|
||||
return str_replace('thumb', 'cover_big', $response[0]['url']);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public function getScreenshotUrl(int $externalId): ?string
|
||||
{
|
||||
if (empty($this->clientId) || empty($this->clientSecret)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$response = $this->makeRequest(
|
||||
'/screenshots',
|
||||
"fields url; where game = {$externalId};"
|
||||
);
|
||||
|
||||
return $response[0]['url'] ?? null;
|
||||
}
|
||||
|
||||
public function getImages(int $externalId): array
|
||||
{
|
||||
return array_filter([
|
||||
'cover' => $this->getCoverUrl($externalId),
|
||||
'screenshot' => $this->getScreenshotUrl($externalId),
|
||||
]);
|
||||
}
|
||||
|
||||
private function makeRequest(string $endpoint, string $body): array
|
||||
{
|
||||
try {
|
||||
$this->ensureAccessToken();
|
||||
|
||||
$response = Http::withHeaders([
|
||||
'Client-ID' => $this->clientId,
|
||||
'Authorization' => "Bearer {$this->accessToken}",
|
||||
])->timeout(10)->post("https://api.igdb.com/v4{$endpoint}", $body);
|
||||
|
||||
if ($response->successful()) {
|
||||
return $response->json();
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
// Log error
|
||||
}
|
||||
|
||||
return [];
|
||||
}
|
||||
|
||||
private function ensureAccessToken(): void
|
||||
{
|
||||
if ($this->accessToken) {
|
||||
return;
|
||||
}
|
||||
|
||||
$response = Http::asForm()->post('https://id.twitch.tv/oauth2/token', [
|
||||
'client_id' => $this->clientId,
|
||||
'client_secret' => $this->clientSecret,
|
||||
'grant_type' => 'client_credentials',
|
||||
]);
|
||||
|
||||
$this->accessToken = $response->json('access_token');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
<?php
|
||||
|
||||
namespace App\Features\Scraper\Services;
|
||||
|
||||
use App\Features\Consoles\Models\Console;
|
||||
use App\Features\Games\Models\Game;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
|
||||
class ScraperService
|
||||
{
|
||||
public function __construct(
|
||||
private IGDBCoverScraper $igdb,
|
||||
private TGDBCoverScraper $tgdb
|
||||
) {}
|
||||
|
||||
public function search(string $query, ?string $consoleSlug = null): Collection
|
||||
{
|
||||
$console = $consoleSlug
|
||||
? Console::where('slug', $consoleSlug)->first()
|
||||
: null;
|
||||
|
||||
$consoleId = $console?->igdb_platform_id;
|
||||
|
||||
$results = $this->igdb->search($query, $consoleId);
|
||||
|
||||
if ($results->isEmpty()) {
|
||||
$tgdbConsoleId = $console?->tgdb_platform_id;
|
||||
$results = $this->tgdb->search($query, $tgdbConsoleId);
|
||||
}
|
||||
|
||||
return $results;
|
||||
}
|
||||
|
||||
public function scrapeForGame(Game $game): ?string
|
||||
{
|
||||
$coverUrl = null;
|
||||
|
||||
if ($game->igdb_id) {
|
||||
$coverUrl = $this->igdb->getCoverUrl($game->igdb_id);
|
||||
}
|
||||
|
||||
if (! $coverUrl && $game->tgdb_id) {
|
||||
$coverUrl = $this->tgdb->getCoverUrl($game->tgdb_id);
|
||||
}
|
||||
|
||||
if ($coverUrl) {
|
||||
return $this->downloadCover($coverUrl, $game);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public function downloadCover(string $url, Game $game): string
|
||||
{
|
||||
$consoleSlug = $game->console->slug ?? 'unknown';
|
||||
$filename = "{$game->slug}-cover.jpg";
|
||||
$path = "covers/{$consoleSlug}/{$filename}";
|
||||
|
||||
try {
|
||||
$contents = Http::timeout(30)->get($url)->body();
|
||||
Storage::disk('local')->put($path, $contents);
|
||||
|
||||
return $path;
|
||||
} catch (\Exception $e) {
|
||||
return '';
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,115 @@
|
||||
<?php
|
||||
|
||||
namespace App\Features\Scraper\Services;
|
||||
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
|
||||
class TGDBCoverScraper implements CoverScraperInterface
|
||||
{
|
||||
private string $apiKey;
|
||||
|
||||
private string $baseUrl = 'https://api.thegamesdb.net/v1';
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->apiKey = config('services.thegamesdb.api_key', '');
|
||||
}
|
||||
|
||||
public function search(string $query, ?int $consoleId = null): Collection
|
||||
{
|
||||
if (empty($this->apiKey)) {
|
||||
return collect();
|
||||
}
|
||||
|
||||
$params = [
|
||||
'apikey' => $this->apiKey,
|
||||
'name' => $query,
|
||||
];
|
||||
if ($consoleId) {
|
||||
$params['platform'] = $consoleId;
|
||||
}
|
||||
|
||||
try {
|
||||
$response = Http::timeout(10)->get("{$this->baseUrl}/Games/ByGameName", $params);
|
||||
|
||||
if ($response->successful()) {
|
||||
$games = $response->json('data.games') ?? [];
|
||||
|
||||
return collect($games)->map(fn ($game) => [
|
||||
'id' => $game['id'],
|
||||
'title' => $game['game_title'],
|
||||
'cover_url' => $game['cover'] ?? null,
|
||||
'year' => isset($game['release_date'])
|
||||
? date('Y', strtotime($game['release_date']))
|
||||
: null,
|
||||
'source' => 'thegamesdb',
|
||||
]);
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
// Log error
|
||||
}
|
||||
|
||||
return collect();
|
||||
}
|
||||
|
||||
public function getCoverUrl(int $externalId): ?string
|
||||
{
|
||||
if (empty($this->apiKey)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
$response = Http::timeout(10)->get("{$this->baseUrl}/Games/Images", [
|
||||
'apikey' => $this->apiKey,
|
||||
'id' => $externalId,
|
||||
'images_type' => 'boxart',
|
||||
]);
|
||||
|
||||
if ($response->successful()) {
|
||||
$images = $response->json('data.images') ?? [];
|
||||
if (! empty($images[0]['filename'])) {
|
||||
return "https://cdn.thegamesdb.net/images/original/{$images[0]['filename']}";
|
||||
}
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
// Log error
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public function getScreenshotUrl(int $externalId): ?string
|
||||
{
|
||||
if (empty($this->apiKey)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
$response = Http::timeout(10)->get("{$this->baseUrl}/Games/Images", [
|
||||
'apikey' => $this->apiKey,
|
||||
'id' => $externalId,
|
||||
'images_type' => 'screenshot',
|
||||
]);
|
||||
|
||||
if ($response->successful()) {
|
||||
$images = $response->json('data.images') ?? [];
|
||||
if (! empty($images[0]['filename'])) {
|
||||
return "https://cdn.thegamesdb.net/images/original/{$images[0]['filename']}";
|
||||
}
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
// Log error
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public function getImages(int $externalId): array
|
||||
{
|
||||
return array_filter([
|
||||
'cover' => $this->getCoverUrl($externalId),
|
||||
'screenshot' => $this->getScreenshotUrl($externalId),
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
namespace App\Features\Settings\Data;
|
||||
|
||||
use App\Features\Settings\Models\SaveFile;
|
||||
use Spatie\LaravelData\Attributes\MapInputName;
|
||||
use Spatie\LaravelData\Data;
|
||||
use Spatie\LaravelData\Mappers\SnakeCaseMapper;
|
||||
|
||||
#[MapInputName(SnakeCaseMapper::class)]
|
||||
class SaveFileData extends Data
|
||||
{
|
||||
public function __construct(
|
||||
public int $id,
|
||||
public int $userId,
|
||||
public int $gameId,
|
||||
public string $filePath,
|
||||
public string $fileName,
|
||||
public int $fileSize,
|
||||
public ?string $lastSyncedAt,
|
||||
) {}
|
||||
|
||||
public static function fromModel(SaveFile $saveFile): self
|
||||
{
|
||||
return new self(
|
||||
id: $saveFile->id,
|
||||
userId: $saveFile->user_id,
|
||||
gameId: $saveFile->game_id,
|
||||
filePath: $saveFile->file_path,
|
||||
fileName: $saveFile->file_name,
|
||||
fileSize: $saveFile->file_size,
|
||||
lastSyncedAt: $saveFile->last_synced_at?->toISOString(),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
namespace App\Features\Settings\Models;
|
||||
|
||||
use App\Features\Games\Models\Game;
|
||||
use App\Models\User;
|
||||
use Database\Factories\SaveFileFactory;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
|
||||
class SaveFile extends Model
|
||||
{
|
||||
/** @use HasFactory<SaveFileFactory> */
|
||||
use HasFactory;
|
||||
|
||||
protected static ?string $factory = SaveFileFactory::class;
|
||||
|
||||
protected $fillable = [
|
||||
'user_id',
|
||||
'game_id',
|
||||
'file_path',
|
||||
'file_name',
|
||||
'file_size',
|
||||
'last_synced_at',
|
||||
];
|
||||
|
||||
protected $casts = [
|
||||
'last_synced_at' => 'datetime',
|
||||
];
|
||||
|
||||
public function user(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(User::class);
|
||||
}
|
||||
|
||||
public function game(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Game::class);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
namespace App\Features\Settings\Models;
|
||||
|
||||
use App\Models\User;
|
||||
use Database\Factories\UserSettingFactory;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
|
||||
class UserSetting extends Model
|
||||
{
|
||||
/** @use HasFactory<UserSettingFactory> */
|
||||
use HasFactory;
|
||||
|
||||
protected static ?string $factory = UserSettingFactory::class;
|
||||
|
||||
protected $fillable = [
|
||||
'user_id',
|
||||
'setting_key',
|
||||
'setting_value',
|
||||
];
|
||||
|
||||
public function user(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(User::class);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
<?php
|
||||
|
||||
namespace App\Features\Settings\Repositories;
|
||||
|
||||
use App\Features\Settings\Models\SaveFile;
|
||||
use App\Features\Settings\Models\UserSetting;
|
||||
use App\Models\User;
|
||||
|
||||
class SettingsRepository
|
||||
{
|
||||
public function __construct(
|
||||
private SaveFile $saveFileModel,
|
||||
private UserSetting $userSettingModel
|
||||
) {}
|
||||
|
||||
public function getUserSaves(User $user)
|
||||
{
|
||||
return $this->saveFileModel
|
||||
->where('user_id', $user->id)
|
||||
->with('game')
|
||||
->get();
|
||||
}
|
||||
|
||||
public function getUserSettings(User $user)
|
||||
{
|
||||
return $this->userSettingModel
|
||||
->where('user_id', $user->id)
|
||||
->get();
|
||||
}
|
||||
|
||||
public function getSetting(User $user, string $key, mixed $default = null): mixed
|
||||
{
|
||||
$setting = $this->userSettingModel
|
||||
->where('user_id', $user->id)
|
||||
->where('setting_key', $key)
|
||||
->first();
|
||||
|
||||
return $setting?->setting_value ?? $default;
|
||||
}
|
||||
|
||||
public function setSetting(User $user, string $key, mixed $value): void
|
||||
{
|
||||
$this->userSettingModel->updateOrCreate(
|
||||
['user_id' => $user->id, 'setting_key' => $key],
|
||||
['setting_value' => $value]
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
<?php
|
||||
|
||||
namespace App\Features\Uploads\Controllers;
|
||||
|
||||
use App\Features\Consoles\Models\Console;
|
||||
use App\Features\Games\Repositories\GameRepository;
|
||||
use App\Features\Games\Repositories\RomFileRepository;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Jobs\ScrapeCoverJob;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Str;
|
||||
use Inertia\Inertia;
|
||||
use Inertia\Response;
|
||||
|
||||
class UploadController extends Controller
|
||||
{
|
||||
public function __construct(
|
||||
private GameRepository $gameRepository,
|
||||
private RomFileRepository $romFileRepository
|
||||
) {}
|
||||
|
||||
public function index(): Response
|
||||
{
|
||||
return Inertia::render('Upload/Index', [
|
||||
'consoles' => Console::orderBy('name')->get(),
|
||||
]);
|
||||
}
|
||||
|
||||
public function store(Request $request): RedirectResponse
|
||||
{
|
||||
$request->validate([
|
||||
'console_id' => 'required|exists:consoles,id',
|
||||
'rom_files' => 'required|array|min:1',
|
||||
'rom_files.*' => 'file|max:102400',
|
||||
]);
|
||||
|
||||
$console = Console::findOrFail($request->console_id);
|
||||
$allowedExtensions = $console->rom_extensions;
|
||||
|
||||
foreach ($request->file('rom_files') as $file) {
|
||||
$extension = strtolower($file->getClientOriginalExtension());
|
||||
|
||||
if (! in_array($extension, $allowedExtensions)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$filename = Str::slug(pathinfo($file->getClientOriginalName(), PATHINFO_FILENAME));
|
||||
$filename = "{$filename}.{$extension}";
|
||||
|
||||
$hash = hash_file('sha256', $file->getRealPath());
|
||||
|
||||
$existing = $this->romFileRepository->findByHash($hash);
|
||||
if ($existing) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$path = $file->storeAs(
|
||||
"roms/{$console->slug}",
|
||||
$filename,
|
||||
'local'
|
||||
);
|
||||
|
||||
$gameTitle = Str::title(str_replace(['-', '_'], ' ', pathinfo($file->getClientOriginalName(), PATHINFO_FILENAME)));
|
||||
$gameSlug = Str::slug($gameTitle);
|
||||
|
||||
$game = $this->gameRepository->firstOrCreate(
|
||||
[
|
||||
'console_id' => $console->id,
|
||||
'slug' => $gameSlug,
|
||||
],
|
||||
[
|
||||
'title' => $gameTitle,
|
||||
]
|
||||
);
|
||||
|
||||
$this->romFileRepository->create([
|
||||
'game_id' => $game->id,
|
||||
'file_path' => $path,
|
||||
'file_name' => $file->getClientOriginalName(),
|
||||
'file_size' => $file->getSize(),
|
||||
'file_hash' => $hash,
|
||||
]);
|
||||
|
||||
if (! $game->cover_path) {
|
||||
ScrapeCoverJob::dispatch($game);
|
||||
}
|
||||
}
|
||||
|
||||
return redirect()->route('dashboard')
|
||||
->with('success', 'ROMs uploaded successfully.');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
|
||||
namespace App\Features\WebDav\Controllers;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Inertia\Inertia;
|
||||
use Inertia\Response;
|
||||
|
||||
class WebDavController extends Controller
|
||||
{
|
||||
public function index(Request $request): Response
|
||||
{
|
||||
$user = $request->user();
|
||||
$webDavAccounts = $user->webDavAccounts;
|
||||
|
||||
return Inertia::render('Settings/WebDav', [
|
||||
'webDavAccounts' => $webDavAccounts,
|
||||
'appUrl' => config('app.url'),
|
||||
]);
|
||||
}
|
||||
|
||||
public function store(Request $request): RedirectResponse
|
||||
{
|
||||
$request->validate([
|
||||
'username' => 'required|string|max:255|unique:webdav_accounts,username',
|
||||
'password' => 'required|string|min:8',
|
||||
]);
|
||||
|
||||
$user = $request->user();
|
||||
|
||||
$user->webDavAccounts()->create([
|
||||
'username' => $request->username,
|
||||
'password_encrypted' => $request->password,
|
||||
'enabled' => true,
|
||||
'display_name' => $request->username,
|
||||
]);
|
||||
|
||||
return redirect()->route('settings.webdav')
|
||||
->with('success', 'WebDAV account created successfully.');
|
||||
}
|
||||
|
||||
public function destroy(Request $request, int $id): RedirectResponse
|
||||
{
|
||||
$user = $request->user();
|
||||
$account = $user->webDavAccounts()->findOrFail($id);
|
||||
$account->delete();
|
||||
|
||||
return redirect()->route('settings.webdav')
|
||||
->with('success', 'WebDAV account deleted successfully.');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace App\Features\WebDav\Data;
|
||||
|
||||
use Spatie\LaravelData\Attributes\MapInputName;
|
||||
use Spatie\LaravelData\Data;
|
||||
use Spatie\LaravelData\Mappers\SnakeCaseMapper;
|
||||
|
||||
#[MapInputName(SnakeCaseMapper::class)]
|
||||
class WebDavAccountData extends Data
|
||||
{
|
||||
public function __construct(
|
||||
public int $id,
|
||||
public int $userId,
|
||||
public string $username,
|
||||
public string $displayName,
|
||||
public bool $enabled,
|
||||
) {}
|
||||
}
|
||||
Reference in New Issue
Block a user