30a6b080eb
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.
116 lines
3.4 KiB
PHP
116 lines
3.4 KiB
PHP
<?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));
|
|
}
|
|
}
|