Files
RomhackPlaza/app/Livewire/HashesUpload.php
2026-07-01 17:59:50 +02:00

135 lines
3.8 KiB
PHP

<?php
namespace App\Livewire;
use App\Helpers\HashesHelpers;
use App\Models\EntryHash;
use App\Models\Game;
use App\Models\Genre;
use App\Models\Platform;
use Illuminate\View\View;
use Livewire\Component;
/**
* Hashes uploader in submission form.
* Upload hash and create fields.
*
* @phpstan-import-type HashObject from \App\Types\SubmissionTypes
*/
class HashesUpload extends Component
{
/**
* List of hashes.
* @var list<HashObject> $hashes
*/
public array $hashes = [];
public ?string $manualFilename = "";
public ?string $manualCRC32 = "";
public ?string $manualSHA1 = "";
/**
* Prepare old hashes.
*
* @param list<HashObject> $oldHashes
* @return void
*/
public function mount( array $oldHashes = [] ): void
{
foreach ($oldHashes as $hash) {
if( isset( $hash['filename'], $hash['hash_crc32'], $hash['hash_sha1'], $hash['verified'] ) )
$this->addHash( $hash['filename'], $hash['hash_crc32'], $hash['hash_sha1'], $hash['verified'] );
}
}
/**
* Add an hash to the list.
*
* @param string $filename
* @param string $crc32
* @param string $sha1
* @param string|null $verified
*
* @return void
*/
public function addHash(string $filename, string $crc32, string $sha1, ?string $verified = null): void
{
if( $verified !== null && $verified !== "No" )
$this->hashes[] = [
'filename' => $filename,
'hash_crc32' => $crc32,
'hash_sha1' => $sha1,
'verified' => $verified
];
else if( ( $hash = HashesHelpers::findHashes( $sha1 ) ) !== null )
$this->hashes[] = [
'filename' => $hash->filename,
'hash_crc32' => $hash->crc32,
'hash_sha1' => $hash->sha1,
'verified' => HashesHelpers::getReferenceName( $hash->dat_reference_id )
];
else
$this->hashes[] = [
'filename' => $filename,
'hash_crc32' => $crc32,
'hash_sha1' => $sha1,
'verified' => "No"
];
}
/**
* Remove a specific hash.
*
* @param int $index
* @return void
*/
public function removeHash(int $index): void
{
array_splice($this->hashes, $index, 1);
}
private function checkCrc32($attribute, $value, $fail)
{
if (!preg_match('/^[a-f0-9]{8}$/i', $value)) {
$fail("CRC32 is invalid");
}
}
private function checkSha1($attribute, $value, $fail)
{
if (!preg_match('/^[a-f0-9]{40}$/i', $value)) {
$fail("SHA1 is invalid");
}
}
public function addManualHash()
{
$this->validate([
'manualFilename' => "required|string|max:512",
'manualCRC32' => ['required', 'string', 'max:512', $this->checkCrc32(...) ],
'manualSHA1' => ['required', 'string', 'max:512', $this->checkSha1(...) ],
], [
'manualFilename.required' => 'Please enter a filename.',
'manualCRC32.required' => 'Please enter a crc32.',
'manualSHA1.required' => 'Please enter a SHA1.',
'manualFilename.max' => 'Filename has to be less than 512 characters.',
'manualCRC32.max' => 'CRC32 has to be less than 512 characters.',
'manualSHA1.max' => 'SHA1 has to be less than 512 characters.',
]);
$this->addHash( $this->manualFilename, $this->manualCRC32, $this->manualSHA1 );
$this->reset(['manualFilename','manualCRC32','manualSHA1']);
$this->dispatch('close-manual-modal');
}
public function render(): View
{
return view('livewire.hashes-upload');
}
}