@@ -41,6 +41,33 @@ class OpportunityImageProcessor
|
||||
abort_unless(hash_equals(trim($doc->sha256), hash('sha256', $bytes)), 409, 'Integridade divergente.');
|
||||
$size = @getimagesizefromstring($bytes);
|
||||
abort_unless($size && $size[0] >= 400 && $size[1] >= 400 && $size[0] * $size[1] <= 40000000, 422, 'Dimensões inválidas ou imagem excessiva para processamento seguro.');
|
||||
$previousPayload = json_decode($image->payload, true, 512, JSON_THROW_ON_ERROR);
|
||||
$previousDerivatives = $previousPayload['derivatives'] ?? [];
|
||||
$expectedWidths = array_unique(array_map(fn ($width) => min($width, $size[0]), [320, 640, 1280]));
|
||||
if (($previousPayload['derived_from_sha256'] ?? null) === trim($doc->sha256)
|
||||
&& array_values(array_column($previousDerivatives, 'width')) === array_values($expectedWidths)) {
|
||||
$reusable = true;
|
||||
$retry = [];
|
||||
foreach ($previousDerivatives as $derivative) {
|
||||
$existing = DB::table('documents')->where('id', $derivative['document_id'])->whereNull('deleted_at')->first();
|
||||
if (! $existing || $existing->source_system !== 'opportunity-image-derivatives' || ! $disk->exists($existing->storage_key)) {
|
||||
$reusable = false;
|
||||
break;
|
||||
}
|
||||
$existingBytes = Crypt::decryptString($disk->get($existing->storage_key));
|
||||
if (! hash_equals(trim($existing->sha256), hash('sha256', $existingBytes))) {
|
||||
$reusable = false;
|
||||
break;
|
||||
}
|
||||
if ($existing->malware_scan_status !== 'clean') {
|
||||
$retry[] = $existing->id;
|
||||
}
|
||||
}
|
||||
if ($reusable) {
|
||||
$documents = $retry;
|
||||
return; // Retry scans of the same streams, without new files, versions or audit decisions.
|
||||
}
|
||||
}
|
||||
$decoded = @imagecreatefromstring($bytes);
|
||||
abort_unless($decoded !== false, 422, 'Não foi possível descodificar a imagem.');
|
||||
$derivatives = [];
|
||||
|
||||
Reference in New Issue
Block a user