feat(attachment): refactor image cropping functionality to use range objects and update related tests

This commit is contained in:
2026-08-18 12:54:47 -03:00
parent 58cc35fd61
commit 9cfd49f233
16 changed files with 496 additions and 54 deletions

View File

@@ -5,6 +5,7 @@ namespace App\Domains\Tenant\Services;
use App\Domains\Attachable\Enums\AttachmentType;
use App\Domains\Attachable\Models\Attachment;
use App\Domains\Attachable\Services\AttachmentService;
use App\Domains\Shared\Rules\CroppedImageOrBase64Rule;
use App\Domains\Shared\Rules\ImageOrBase64Rule;
use App\Domains\Tenant\Models\Tenant;
use App\Domains\Tenant\Models\WebsiteExtra;
@@ -222,9 +223,11 @@ class WebsiteExtraService
$compiled = is_string($rules) ? explode('|', $rules) : $rules;
return array_map(
fn (mixed $rule): mixed => $rule === 'image_or_base64'
? new ImageOrBase64Rule
: $rule,
fn (mixed $rule): mixed => match ($rule) {
'image_or_base64' => new ImageOrBase64Rule,
'cropped_image_or_base64' => new CroppedImageOrBase64Rule,
default => $rule,
},
$compiled
);
}
@@ -331,8 +334,12 @@ class WebsiteExtraService
);
}
if (is_string($value) && Str::isUuid($value)) {
$attachment = Attachment::query()->where('key', $value)->first();
$cropHorizontal = is_array($value) ? $value['crop_horizontal'] ?? null : null;
$cropVertical = is_array($value) ? $value['crop_vertical'] ?? null : null;
$image = is_array($value) ? $value['image'] ?? null : $value;
if (is_string($image) && Str::isUuid($image)) {
$attachment = Attachment::query()->where('key', $image)->first();
if (! $attachment) {
throw ValidationException::withMessages([
@@ -341,9 +348,24 @@ class WebsiteExtraService
],
]);
}
if (is_array($cropHorizontal) && is_array($cropVertical)) {
$attachment = $this->attachmentService->updateImageCrop(
$attachment,
$cropHorizontal,
$cropVertical,
);
}
} elseif (is_array($cropHorizontal) && is_array($cropVertical)) {
$attachment = $this->attachmentService->storeCroppedImage(
$image,
"tenants/{$tenant->codigo}/extras/{$definition->codigo}",
$cropHorizontal,
$cropVertical,
);
} else {
$attachment = $this->attachmentService->store(
$value,
$image,
"tenants/{$tenant->codigo}/extras/{$definition->codigo}"
);
}