Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions ProcessMaker/Console/Commands/NormalizeScreenInlineImages.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

namespace ProcessMaker\Console\Commands;

use Illuminate\Console\Command;
use ProcessMaker\Models\Screen;
use ProcessMaker\Screens\ScreenInlineImageNormalizer;

class NormalizeScreenInlineImages extends Command
{
protected $signature = 'processmaker:normalize-screen-inline-images
{--screen= : Limit to a single screen id}
{--dry-run : Detect inline images without writing changes}';

protected $description = 'Extract base64 inline images from screen config into Spatie media';

public function handle(ScreenInlineImageNormalizer $normalizer): int
{
$query = Screen::query()->orderBy('id');
if ($this->option('screen')) {
$query->where('id', (int) $this->option('screen'));
}

$dryRun = (bool) $this->option('dry-run');
$scanned = 0;
$modified = 0;
$converted = 0;

$query->chunkById(50, function ($screens) use ($normalizer, $dryRun, &$scanned, &$modified, &$converted) {
foreach ($screens as $screen) {
$scanned++;
$config = $screen->config;
if (!is_array($config) || !$normalizer->configContainsInlineImages($config)) {
continue;
}

if ($dryRun) {
$modified++;
$this->line("[dry-run] Screen #{$screen->id} ({$screen->title}) contains inline images");
continue;
}

$result = $normalizer->normalize($screen, $config);
if (!$result->wasModified()) {
continue;
}

$screen->config = $result->config();
$screen->saveOrFail();
$modified++;
$converted += $result->convertedCount();
$this->info("Screen #{$screen->id}: converted {$result->convertedCount()} image(s)");
}
});

$this->info("Scanned {$scanned} screen(s); modified {$modified}; new media {$converted}");

return self::SUCCESS;
}
}
1 change: 1 addition & 0 deletions ProcessMaker/Http/Controllers/Api/FileController.php
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ public function store(Request $request)
'model_id' => $addedMedia->model_id,
'file_name' => $addedMedia->file_name,
'mime_type' => $addedMedia->mime_type,
'url' => $addedMedia->getUrl(),
], 200);
}

Expand Down
37 changes: 35 additions & 2 deletions ProcessMaker/Http/Controllers/Api/ScreenController.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
use ProcessMaker\Models\ScreenType;
use ProcessMaker\ProcessTranslations\ScreenTranslation;
use ProcessMaker\Query\SyntaxError;
use ProcessMaker\Screens\ScreenInlineImageNormalizationResult;
use ProcessMaker\Screens\ScreenInlineImageNormalizer;
use ProcessMaker\Traits\ProjectAssetTrait;

class ScreenController extends Controller
Expand Down Expand Up @@ -301,6 +303,8 @@ public function update(Screen $screen, Request $request)
$screen->fill($request->input());
$original = $screen->getOriginal();

$normalization = $this->normalizeInlineImages($screen);

$this->updateScreenDetails($request, $screen, $original, $lastVersion);

$screen->saveOrFail();
Expand All @@ -319,7 +323,7 @@ public function update(Screen $screen, Request $request)
$screenCache = ScreenCacheFactory::getScreenCache();
$screenCache->clearCompiledAssets();

return response([], 204);
return $this->screenSaveResponse($normalization);
}

public function updateScreenDetails($request, $screen, $original, $lastVersion)
Expand Down Expand Up @@ -392,9 +396,38 @@ public function draft(Screen $screen, Request $request)
{
$request->validate(Screen::rules($screen));
$screen->fill($request->input());
$normalization = $this->normalizeInlineImages($screen);
$screen->saveDraft();

return response([], 204);
return $this->screenSaveResponse($normalization);
}

private function normalizeInlineImages(Screen $screen): ScreenInlineImageNormalizationResult
{
$config = $screen->config;
if (!is_array($config)) {
return new ScreenInlineImageNormalizationResult([], 0, 0);
}

$result = app(ScreenInlineImageNormalizer::class)->normalize($screen, $config);
if ($result->wasModified()) {
$screen->config = $result->config();
}

return $result;
}

private function screenSaveResponse(ScreenInlineImageNormalizationResult $normalization)
{
if (!$normalization->wasModified()) {
return response([], 204);
}

return response([
'converted_images' => $normalization->convertedCount(),
'replaced_images' => $normalization->replacedCount(),
'config' => $normalization->config(),
], 200);
}

public function close(Screen $screen)
Expand Down
12 changes: 11 additions & 1 deletion ProcessMaker/Models/Screen.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
use ProcessMaker\Traits\ProjectAssetTrait;
use ProcessMaker\Traits\SerializeToIso8601;
use ProcessMaker\Validation\CategoryRule;
use Spatie\MediaLibrary\HasMedia;
use Spatie\MediaLibrary\InteractsWithMedia;

/**
* Class Screen
Expand Down Expand Up @@ -64,7 +66,7 @@
* @OA\Property(property="url", type="string"),
* )
*/
class Screen extends ProcessMakerModel implements ScreenInterface, PrometheusMetricInterface
class Screen extends ProcessMakerModel implements ScreenInterface, PrometheusMetricInterface, HasMedia
{
use SerializeToIso8601;
use HideSystemResources;
Expand All @@ -74,9 +76,12 @@ class Screen extends ProcessMakerModel implements ScreenInterface, PrometheusMet
use ExtendedPMQL;
use Exportable;
use ProjectAssetTrait;
use InteractsWithMedia;

const categoryClass = ScreenCategory::class;

public const INLINE_IMAGES_COLLECTION = 'inline_images';

protected $connection = 'processmaker';

/**
Expand Down Expand Up @@ -119,6 +124,11 @@ public static function boot()
static::deleting($clearCacheCallback);
}

public function registerMediaCollections(): void
{
$this->addMediaCollection(self::INLINE_IMAGES_COLLECTION);
}

/**
* Validation rules
*
Expand Down
33 changes: 33 additions & 0 deletions ProcessMaker/Screens/ScreenInlineImageNormalizationResult.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace ProcessMaker\Screens;

class ScreenInlineImageNormalizationResult
{
public function __construct(
private readonly array $config,
private readonly int $convertedCount,
private readonly int $replacedCount = 0,
) {
}

public function config(): array
{
return $this->config;
}

public function convertedCount(): int
{
return $this->convertedCount;
}

public function replacedCount(): int
{
return $this->replacedCount;
}

public function wasModified(): bool
{
return $this->replacedCount > 0;
}
}
Loading
Loading