feat(admin): rebuild release upload form with Livewire and CDN validation
Replace the server-rendered release upload form with a Livewire component, introducing an interactive admin release form that supports dynamic episode management, real‑time CDN path validation, and direct download file checks. Key changes: - New `AdminReleaseForm` Livewire component with dynamic episode fields, Tagify integration, and upload handling via Livewire's file uploads. - Added `CdnPathValidator` service to verify download and stream paths across all configured mirrors, caching results and capturing file sizes. - Extended `EpisodeService` and `GalleryService` to support array‑based data and temporary uploaded files from Livewire. - Persist validated download sizes and store the validation timestamp on the `downloads` table via a new migration; also add a unique constraint on `hentais.slug` to prevent duplicates. - Remove the old POST `/admin/release/upload` route and controller logic, along with the legacy `upload.js` script. - Add comprehensive feature and unit tests covering the new component and the CDN validation service.
This commit is contained in:
@@ -0,0 +1,115 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use Illuminate\Http\Client\Response;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Illuminate\Support\Facades\Crypt;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
|
||||
class CdnPathValidator
|
||||
{
|
||||
public const DOWNLOAD_PREFIX = [
|
||||
'FHD' => 'hentai-1080p/',
|
||||
'FHDi' => 'hentai-1080p/',
|
||||
'UHD' => 'hentai/',
|
||||
'UHDi' => 'hentai/',
|
||||
];
|
||||
|
||||
private const STREAM_PREFIX = 'hentai-stream/';
|
||||
|
||||
private const CACHE_TTL = 300;
|
||||
|
||||
public function validateDownload(string $type, string $url, bool $fresh = false): array
|
||||
{
|
||||
$path = self::DOWNLOAD_PREFIX[$type].ltrim($url, '/');
|
||||
$domains = config('hstream.download_domain_4k');
|
||||
|
||||
return $this->checkMirrors($domains, [$path], $fresh);
|
||||
}
|
||||
|
||||
public function validateStream(string $baseurl, int $episodeCount, bool $fresh = false): array
|
||||
{
|
||||
$domains = config('hstream.download_domain_4k');
|
||||
|
||||
// Validate the base directory, plus the first episode manifest as a sanity check.
|
||||
$paths = [self::STREAM_PREFIX.rtrim($baseurl, '/')];
|
||||
if ($episodeCount >= 1) {
|
||||
$paths[] = self::STREAM_PREFIX.rtrim($baseurl, '/').'/E'.str_pad(1, 2, '0', STR_PAD_LEFT).'/720/manifest.mpd';
|
||||
}
|
||||
|
||||
return $this->checkMirrors($domains, $paths, $fresh);
|
||||
}
|
||||
|
||||
private function checkMirrors(array $domains, array $paths, bool $fresh = false): array
|
||||
{
|
||||
$cacheKey = 'cdncheck:'.sha1(serialize([$domains, $paths]));
|
||||
|
||||
if ($fresh) {
|
||||
Cache::forget($cacheKey);
|
||||
}
|
||||
|
||||
return Cache::remember($cacheKey, self::CACHE_TTL, fn () => $this->runChecks($domains, $paths));
|
||||
}
|
||||
|
||||
private function runChecks(array $domains, array $paths): array
|
||||
{
|
||||
$entries = [];
|
||||
foreach ($domains as $domain) {
|
||||
foreach ($paths as $path) {
|
||||
$entries[] = ['domain' => $domain, 'path' => $path];
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
$responses = Http::pool(function ($pool) use ($entries) {
|
||||
foreach ($entries as $entry) {
|
||||
$pool->timeout(4)
|
||||
->withHeaders(['Accept' => 'application/json'])
|
||||
->get($entry['domain'].'/check/'.$this->tokenize($entry['path']).'/'.$this->tokenize(now()->addHours(6)));
|
||||
}
|
||||
});
|
||||
} catch (\Throwable $e) {
|
||||
$mirrors = [];
|
||||
foreach ($entries as $entry) {
|
||||
$mirrors[$entry['domain']][$entry['path']] = false;
|
||||
}
|
||||
|
||||
return [
|
||||
'valid' => false,
|
||||
'mirrors' => $mirrors,
|
||||
'size' => null,
|
||||
];
|
||||
}
|
||||
|
||||
$mirrors = [];
|
||||
$allValid = true;
|
||||
$firstSize = null;
|
||||
|
||||
foreach ($entries as $index => $entry) {
|
||||
$response = $responses[$index];
|
||||
$ok = $response instanceof Response
|
||||
&& $response->ok()
|
||||
&& $response->json('valid') === true;
|
||||
|
||||
$mirrors[$entry['domain']][$entry['path']] = $ok;
|
||||
|
||||
if (! $ok) {
|
||||
$allValid = false;
|
||||
} elseif ($firstSize === null) {
|
||||
$firstSize = $response->json('size');
|
||||
}
|
||||
}
|
||||
|
||||
return [
|
||||
'valid' => $allValid,
|
||||
'mirrors' => $mirrors,
|
||||
'size' => $allValid ? $firstSize : null,
|
||||
];
|
||||
}
|
||||
|
||||
private function tokenize(string $value): string
|
||||
{
|
||||
return urlencode(Crypt::encryptString($value));
|
||||
}
|
||||
}
|
||||
@@ -33,4 +33,28 @@ class DownloadService
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Persist validated download URLs for an episode. Sizes are written
|
||||
* directly from the CDN validation response, so no size job is dispatched.
|
||||
*
|
||||
* @param array<string, array{url: string, size?: float|int|null}> $downloads
|
||||
*/
|
||||
public function createOrUpdateDownloadsFromArray(Episode $episode, array $downloads): void
|
||||
{
|
||||
foreach ($downloads as $type => $data) {
|
||||
if (empty($data['url'])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
Downloads::updateOrCreate([
|
||||
'episode_id' => $episode->id,
|
||||
'type' => $type,
|
||||
], [
|
||||
'url' => $data['url'],
|
||||
'size' => $data['size'] ?? null,
|
||||
'validated_at' => now(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,14 +4,16 @@ namespace App\Services;
|
||||
|
||||
use App\Models\Episode;
|
||||
use App\Models\Hentai;
|
||||
use App\Models\Studios;
|
||||
use App\Models\ModLog;
|
||||
use App\Models\Studios;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\UploadedFile;
|
||||
use Illuminate\Support\Carbon;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Illuminate\Support\Str;
|
||||
use Intervention\Image\Encoders\WebpEncoder;
|
||||
use Intervention\Image\Laravel\Facades\Image;
|
||||
use Livewire\Features\SupportFileUploads\TemporaryUploadedFile;
|
||||
|
||||
class EpisodeService
|
||||
{
|
||||
@@ -171,19 +173,60 @@ class EpisodeService
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an episode from an array of data (used by the Livewire release form).
|
||||
*
|
||||
* @param array{title: string, title_jpn: string, baseurl: string, description: string, releasedate: string, tags: string[], interpolated_uhd?: bool} $data
|
||||
*/
|
||||
public function createEpisodeFromArray(
|
||||
array $data,
|
||||
Hentai $hentai,
|
||||
int $episodeNumber,
|
||||
?Studios $studio = null
|
||||
): Episode {
|
||||
$episode = new Episode;
|
||||
$episode->title = $data['title'];
|
||||
$episode->title_search = preg_replace('/[^A-Za-z0-9 ]/', '', $episode->title);
|
||||
$episode->title_jpn = $data['title_jpn'];
|
||||
$episode->slug = "{$hentai->slug}-{$episodeNumber}";
|
||||
$episode->hentai_id = $hentai->id;
|
||||
$episode->studios_id = $studio->id;
|
||||
$episode->episode = $episodeNumber;
|
||||
$episode->description = $data['description'];
|
||||
$episode->url = rtrim($data['baseurl'], '/').'/E'.str_pad($episodeNumber, 2, '0', STR_PAD_LEFT);
|
||||
$episode->view_count = 0;
|
||||
$episode->interpolated = true;
|
||||
$episode->interpolated_uhd = $data['interpolated_uhd'] ?? false;
|
||||
$episode->is_dvd_aspect = false;
|
||||
$episode->release_date = Carbon::parse($data['releasedate'])->format('Y-m-d');
|
||||
$episode->cover_url = "/images/hentai/{$hentai->slug}/cover-ep-{$episodeNumber}.webp";
|
||||
$episode->save();
|
||||
|
||||
foreach ($data['tags'] as $tag) {
|
||||
$episode->tag($tag);
|
||||
}
|
||||
|
||||
return $episode;
|
||||
}
|
||||
|
||||
public function createOrUpdateCover(Request $request, Episode $episode, string $slug, int $episodeNumber): void
|
||||
{
|
||||
if (! $request->hasFile("episodecover{$episodeNumber}")) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->saveCoverFromFile($episode, $slug, $episodeNumber, $request->file("episodecover{$episodeNumber}"));
|
||||
}
|
||||
|
||||
public function saveCoverFromFile(Episode $episode, string $slug, int $episodeNumber, UploadedFile|TemporaryUploadedFile $file): void
|
||||
{
|
||||
// Create Folder for Image Upload
|
||||
if (! Storage::disk('public')->exists("/images/hentai/{$slug}")) {
|
||||
Storage::disk('public')->makeDirectory("/images/hentai/{$slug}");
|
||||
}
|
||||
|
||||
// Encode and save cover image
|
||||
Image::read($request->file("episodecover{$episodeNumber}")->getRealPath())
|
||||
Image::read($file->getRealPath())
|
||||
->cover(268, 394)
|
||||
->encode(new WebpEncoder)
|
||||
->save(Storage::disk('public')->path($episode->cover_url));
|
||||
|
||||
@@ -6,9 +6,11 @@ use App\Models\Episode;
|
||||
use App\Models\Gallery;
|
||||
use App\Models\Hentai;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\UploadedFile;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Intervention\Image\Encoders\WebpEncoder;
|
||||
use Intervention\Image\Laravel\Facades\Image;
|
||||
use Livewire\Features\SupportFileUploads\TemporaryUploadedFile;
|
||||
|
||||
class GalleryService
|
||||
{
|
||||
@@ -17,17 +19,26 @@ class GalleryService
|
||||
$galleryInputNumber = $override ? 1 : $episodeNumber;
|
||||
|
||||
if ($request->hasFile('episodegallery'.$galleryInputNumber)) {
|
||||
$this->saveGalleryFiles($hentai, $episode, $episodeNumber, $request->file('episodegallery'.$galleryInputNumber));
|
||||
}
|
||||
}
|
||||
|
||||
$this->deleteOldGallery($episode);
|
||||
/**
|
||||
* Save a gallery for the given episode from a list of uploaded files.
|
||||
*
|
||||
* @param array<int, UploadedFile|TemporaryUploadedFile> $files
|
||||
*/
|
||||
public function saveGalleryFiles(Hentai $hentai, Episode $episode, int $episodeNumber, array $files): void
|
||||
{
|
||||
$this->deleteOldGallery($episode);
|
||||
|
||||
$this->createGalleryFolder($hentai);
|
||||
$this->createGalleryFolder($hentai);
|
||||
|
||||
$counter = 0;
|
||||
foreach ($request->file('episodegallery'.$galleryInputNumber) as $file) {
|
||||
$gallery = $this->createGallery($hentai, $episode, $episodeNumber, $counter);
|
||||
$this->saveGalleryImage($gallery, $file);
|
||||
$counter += 1;
|
||||
}
|
||||
$counter = 0;
|
||||
foreach ($files as $file) {
|
||||
$gallery = $this->createGallery($hentai, $episode, $episodeNumber, $counter);
|
||||
$this->saveGalleryImage($gallery, $file);
|
||||
$counter += 1;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -51,7 +62,7 @@ class GalleryService
|
||||
return $gallery;
|
||||
}
|
||||
|
||||
private function saveGalleryImage(Gallery $gallery, $sourceImage): void
|
||||
private function saveGalleryImage(Gallery $gallery, UploadedFile|TemporaryUploadedFile $sourceImage): void
|
||||
{
|
||||
Image::read($sourceImage->getRealPath())
|
||||
->cover(1920, 1080)
|
||||
|
||||
Reference in New Issue
Block a user