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

@@ -9,12 +9,12 @@ return new class extends Migration
public function up(): void
{
Schema::table('attachments', function (Blueprint $table): void {
$table->decimal('crop_horizontal_start_percent', 7, 4)->nullable()->after('size');
$table->decimal('crop_vertical_start_percent', 7, 4)->nullable()->after('crop_horizontal_start_percent');
$table->json('crop_horizontal')->nullable()->after('size');
$table->json('crop_vertical')->nullable()->after('crop_horizontal');
$table->foreignId('cropped_attachment_id')
->nullable()
->unique()
->after('crop_vertical_start_percent')
->after('crop_vertical')
->constrained('attachments')
->nullOnDelete();
});
@@ -26,8 +26,8 @@ return new class extends Migration
$table->dropForeign(['cropped_attachment_id']);
$table->dropColumn([
'cropped_attachment_id',
'crop_horizontal_start_percent',
'crop_vertical_start_percent',
'crop_horizontal',
'crop_vertical',
]);
});
}

View File

@@ -0,0 +1,43 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Support\Facades\DB;
return new class extends Migration
{
public function up(): void
{
$this->replaceRule('nullable|image_or_base64', 'nullable|cropped_image_or_base64');
}
public function down(): void
{
$this->replaceRule('nullable|cropped_image_or_base64', 'nullable|image_or_base64');
}
private function replaceRule(string $from, string $to): void
{
DB::table('website_type_extras')
->where('codigo', 'heroConfig')
->orderBy('id')
->each(function (object $definition) use ($from, $to): void {
$schema = json_decode((string) $definition->config_schema, true);
if (! is_array($schema)) {
return;
}
$currentRule = $schema['request_rules']['background_image_id'] ?? null;
if ($currentRule !== $from) {
return;
}
$schema['request_rules']['background_image_id'] = $to;
DB::table('website_type_extras')
->where('id', $definition->id)
->update(['config_schema' => json_encode($schema)]);
});
}
};