2024-10-30 21:13:41 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace BookStack\Exports\ZipExports;
|
|
|
|
|
|
|
|
use Closure;
|
|
|
|
use Illuminate\Contracts\Validation\ValidationRule;
|
|
|
|
|
|
|
|
class ZipFileReferenceRule implements ValidationRule
|
|
|
|
{
|
|
|
|
public function __construct(
|
|
|
|
protected ZipValidationHelper $context,
|
2024-11-19 01:42:49 +08:00
|
|
|
protected array $acceptedMimes,
|
2024-10-30 21:13:41 +08:00
|
|
|
) {
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function validate(string $attribute, mixed $value, Closure $fail): void
|
|
|
|
{
|
2024-11-03 01:17:34 +08:00
|
|
|
if (!$this->context->zipReader->fileExists($value)) {
|
2024-10-30 21:13:41 +08:00
|
|
|
$fail('validation.zip_file')->translate();
|
|
|
|
}
|
2024-11-19 01:42:49 +08:00
|
|
|
|
|
|
|
if (!empty($this->acceptedMimes)) {
|
|
|
|
$fileMime = $this->context->zipReader->sniffFileMime($value);
|
|
|
|
if (!in_array($fileMime, $this->acceptedMimes)) {
|
|
|
|
$fail('validation.zip_file_mime')->translate([
|
|
|
|
'attribute' => $attribute,
|
|
|
|
'validTypes' => implode(',', $this->acceptedMimes),
|
|
|
|
'foundType' => $fileMime
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|
2024-10-30 21:13:41 +08:00
|
|
|
}
|
|
|
|
}
|