mirror of
https://github.com/BookStackApp/BookStack.git
synced 2024-12-14 07:07:21 +08:00
59cfc087e1
Images were missing their extension after import since it was (potentially) not part of the import data. This adds validation via mime sniffing (to match normal image upload checks) and also uses the same logic to sniff out a correct extension. Added tests to cover. Also fixed some existing tests around zip functionality.
38 lines
1017 B
PHP
38 lines
1017 B
PHP
<?php
|
|
|
|
namespace BookStack\Exports\ZipExports;
|
|
|
|
use Closure;
|
|
use Illuminate\Contracts\Validation\ValidationRule;
|
|
|
|
class ZipFileReferenceRule implements ValidationRule
|
|
{
|
|
public function __construct(
|
|
protected ZipValidationHelper $context,
|
|
protected array $acceptedMimes,
|
|
) {
|
|
}
|
|
|
|
|
|
/**
|
|
* @inheritDoc
|
|
*/
|
|
public function validate(string $attribute, mixed $value, Closure $fail): void
|
|
{
|
|
if (!$this->context->zipReader->fileExists($value)) {
|
|
$fail('validation.zip_file')->translate();
|
|
}
|
|
|
|
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
|
|
]);
|
|
}
|
|
}
|
|
}
|
|
}
|