diff --git a/app/Jobs/ProcessModPackFile.php b/app/Jobs/ProcessModPackFile.php index b9888c3c..3e1a8b33 100644 --- a/app/Jobs/ProcessModPackFile.php +++ b/app/Jobs/ProcessModPackFile.php @@ -4,13 +4,13 @@ use App\Events\ModPack\ModPackProcessProgress; use App\Models\Modpack; +use App\Services\Modpacks\ModpackUpdaterService; use Illuminate\Bus\Batchable; use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Foundation\Bus\Dispatchable; use Illuminate\Queue\InteractsWithQueue; use Illuminate\Queue\SerializesModels; -use Illuminate\Support\Facades\Redis; use Illuminate\Support\Facades\Storage; use Illuminate\Support\Str; use Throwable; @@ -64,19 +64,15 @@ public function handle() $this->modpack->path, $this->modpack->name ); - $filePathPrevented = Str::of($filePath) - ->replace('.', '-'); - Redis::hIncrBy("modpackManifestInfoUpdate:{$this->modpack->id}", 'size', $fileSize); - Redis::hIncrBy("modpackManifestInfoUpdate:{$this->modpack->id}", 'files', 1); - - Redis::hSet("modpackManifestUpdate:{$this->modpack->id}", $filePathPrevented, json_encode([ - 'url' => $fileUrl, - 'size' => $fileSize, - 'name' => $fileName, - 'path' => $filePath, - 'sha256' => $fileHash - ])); + ModpackUpdaterService::fileProcessed( + modPack: $this->modpack, + fileName: $fileName, + fileSize: $fileSize, + fileUrl: $fileUrl, + filePath: $filePath, + fileHash: $fileHash + ); ModPackProcessProgress::broadcast($this->modpack, $this->batch()->progress()); } diff --git a/app/Listeners/Modpack/StartModPackUpdate.php b/app/Listeners/Modpack/StartModPackUpdate.php index 9625acb2..f96035d9 100644 --- a/app/Listeners/Modpack/StartModPackUpdate.php +++ b/app/Listeners/Modpack/StartModPackUpdate.php @@ -8,16 +8,15 @@ use App\Events\ModPack\ModPackProcessStarted; use App\Events\ModPack\ModPackUpdateRequested; use App\Jobs\ProcessModPackFile; +use App\Services\Modpacks\ModpackUpdaterService; use Exception; use Illuminate\Bus\Batch; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Queue\InteractsWithQueue; use Illuminate\Support\Facades\Bus; use Illuminate\Support\Facades\Log; -use Illuminate\Support\Facades\Redis; use Illuminate\Support\Facades\Storage; use Throwable; -use function React\Promise\map; class StartModPackUpdate implements ShouldQueue { @@ -40,8 +39,7 @@ public function handle(ModPackUpdateRequested $event) return true; } - Redis::del("modpackManifestUpdate:$modpack->id"); - Redis::del("modpackManifestInfoUpdate:$modpack->id"); + ModpackUpdaterService::flush($modpack); $jobs = $files->map(fn($file) => new ProcessModPackFile($modpack, $file)); $batch = Bus::batch($jobs->toArray()) @@ -50,8 +48,8 @@ public function handle(ModPackUpdateRequested $event) ModPackProcessCanceled::broadcast($modpack); return; } - $manifest = Redis::hGetAll("modpackManifestUpdate:$modpack->id"); - $manifestInfo = Redis::hGetAll("modpackManifestInfoUpdate:$modpack->id"); + + [$manifest, $manifestInfo] = ModpackUpdaterService::getUpdate($modpack); if (!empty($manifest) && !empty($manifestInfo)) { $modpack->update([ diff --git a/app/Services/Modpacks/ModpackUpdaterService.php b/app/Services/Modpacks/ModpackUpdaterService.php new file mode 100644 index 00000000..a73bd3d3 --- /dev/null +++ b/app/Services/Modpacks/ModpackUpdaterService.php @@ -0,0 +1,63 @@ +id"; + } + + private static function getManifestInfoKey(Modpack $modpack): string + { + return "modPack:manifestInfoUpdate:$modpack->id"; + } + + + static public function flush(Modpack $modpack): void + { + Redis::del(self::getManifestKey($modpack)); + Redis::del(self::getManifestInfoKey($modpack)); + } + + static public function getUpdate(Modpack $modpack): array + { + return [ + Redis::hGetAll(self::getManifestKey($modpack)), + Redis::hGetAll(self::getManifestInfoKey($modpack)), + ]; + } + + public static function fileProcessed( + Modpack $modPack, + string $fileName, + int $fileSize, + string $fileUrl, + string $filePath, + string $fileHash, + ) + { + $manifestKey = self::getManifestKey($modPack); + $manifestInfoKey = self::getManifestInfoKey($modPack); + + $safeFilePath = Str::of($filePath) + ->replace('.', '-'); + + + Redis::hIncrBy($manifestInfoKey, 'size', $fileSize); + Redis::hIncrBy($manifestInfoKey, 'files', 1); + + Redis::hSet($manifestKey, $safeFilePath, json_encode([ + 'url' => $fileUrl, + 'size' => $fileSize, + 'name' => $fileName, + 'path' => $filePath, + 'sha256' => $fileHash + ])); + } +}