Skip to content
Merged
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
208 changes: 208 additions & 0 deletions bin/promote-legacy-images.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,208 @@
<?php

declare(strict_types=1);

/*
* One-shot migration — promote `Item.data.images` entries (the migrated
* 1.x payload) into proper FileRepository rows.
*
* Why: the editor and the frontend used to carry two parallel render
* paths — one for new uploads in the `files` table, one for legacy
* entries embedded in `data.images`. Legacy rows lacked the delete
* button, dimensions / size, and the title-PATCH endpoint. Promoting
* the entries lets us collapse to a single rendering path.
*
* Per legacy entry we:
* 1. Resolve the source on disk (relative to FileStorage root).
* 2. Read mime, size, and image dimensions from the bytes.
* 3. Insert a `File` row pointing at the same on-disk path (no file
* movement — the file already lives where FileStorage expects it
* under data/uploads-2.0/).
* 4. Generate the `300x300_<name>` thumbnail (matching the editor
* preview convention from UploadEndpoint::ensureThumbnail).
* 5. Strip the entry from `Item.data.images` and save the item.
*
* Idempotency: skips items where a file row with the same
* (itemId, fieldId, name) already exists.
*
* Usage:
* php bin/promote-legacy-images.php
* php bin/promote-legacy-images.php --dry-run
* php bin/promote-legacy-images.php --db=/path/to/other.db
*/

require __DIR__ . '/../vendor/autoload.php';

use Imanager\Domain\File;
use Imanager\Domain\Item;
use Imanager\Files\FileStorage;
use Imanager\Files\ImageProcessor;
use Imanager\Storage\CategoryRepository;
use Imanager\Storage\FieldRepository;
use Imanager\Storage\FileRepository;
use Imanager\Storage\ItemRepository;
use Scriptor\Boot\ImanagerBootstrap;

$dryRun = false;
$dbPath = null;
foreach ($argv as $arg) {
if ($arg === '--dry-run') {
$dryRun = true;
} elseif (str_starts_with($arg, '--db=')) {
$dbPath = substr($arg, 5);
}
}

$paths = $dbPath !== null ? ['databasePath' => $dbPath] : [];
$container = ImanagerBootstrap::create(__DIR__ . '/..', $paths);

$categories = $container->get(CategoryRepository::class);
$fields = $container->get(FieldRepository::class);
$items = $container->get(ItemRepository::class);
$files = $container->get(FileRepository::class);
$storage = $container->get(FileStorage::class);
$processor = $container->get(ImageProcessor::class);

$pages = $categories->findBySlug('pages');
if ($pages === null || $pages->id === null) {
fwrite(STDERR, "No 'pages' category\n");
exit(1);
}
$field = $fields->findByName($pages->id, 'images');
if ($field === null || $field->id === null) {
fwrite(STDERR, "No 'images' field on Pages category\n");
exit(1);
}
$fieldId = (int) $field->id;

$total = $items->countByCategory($pages->id);
$promoted = 0;
$missing = 0;
$dupes = 0;
$itemsTouched = 0;

echo "Scanning {$total} pages" . ($dryRun ? ' (dry-run)' : '') . "\n";
echo str_repeat('-', 70) . "\n";

$batch = 100;
for ($offset = 0; $offset < $total; $offset += $batch) {
foreach ($items->findByCategory($pages->id, $offset, $batch) as $item) {
$rawImages = $item->data->get('images');
if (! is_array($rawImages) || $rawImages === []) {
continue;
}

$existingNames = [];
foreach ($files->findByItemAndField($item->id, $fieldId) as $f) {
$existingNames[$f->name] = true;
}

$promotedHere = 0;
$remaining = [];

foreach ($rawImages as $img) {
if (! is_array($img)) {
$remaining[] = $img;
continue;
}
$name = (string) ($img['name'] ?? '');
$dir = ltrim((string) ($img['path'] ?? ''), '/');
$dir = preg_replace('#^data/uploads(-2\.0)?/#', '', $dir) ?? $dir;
$dir = trim($dir, '/');
if ($name === '') {
$remaining[] = $img;
continue;
}
$relPath = ($dir !== '' ? $dir . '/' : '') . $name;

if (isset($existingNames[$name])) {
$dupes++;
printf(" item #%-3d skip dupe: %s\n", $item->id, $name);
continue;
}
if (! $storage->exists($relPath)) {
$missing++;
printf(" item #%-3d MISSING on disk: %s\n", $item->id, $relPath);
$remaining[] = $img;
continue;
}

$absPath = $storage->absolutePath($relPath);
$size = (int) (filesize($absPath) ?: 0);
$mime = (string) (mime_content_type($absPath) ?: 'application/octet-stream');
$width = 0;
$height = 0;
if (str_starts_with($mime, 'image/')) {
$dims = $processor->dimensions($absPath);
$width = $dims['width'];
$height = $dims['height'];
}

$title = (string) ($img['title'] ?? '');
$position = (int) ($img['position'] ?? 0);

$file = new File(
id: null,
itemId: (int) $item->id,
fieldId: $fieldId,
name: $name,
path: $relPath,
mime: $mime,
size: $size,
width: $width,
height: $height,
position: $position,
created: time(),
title: $title,
);

printf(" item #%-3d promote: %-50s %dx%d %d bytes\n",
$item->id,
mb_strimwidth($name, 0, 50, '…'),
$width, $height, $size,
);

if (! $dryRun) {
$files->save($file);

$thumbRel = ($dir !== '' ? $dir . '/' : '') . 'thumbnail/300x300_' . $name;
if (! $storage->exists($thumbRel)) {
try {
$bytes = $processor->thumbnail($absPath, 300, 300);
$storage->write($thumbRel, $bytes);
} catch (\Throwable $e) {
fprintf(STDERR, " thumbnail failed for %s: %s\n", $relPath, $e->getMessage());
}
}
}
$promoted++;
$promotedHere++;
}

if ($promotedHere === 0) {
continue;
}

$itemsTouched++;

if (! $dryRun) {
$newData = $item->data->with('images', $remaining);
$items->save(new Item(
id: $item->id,
categoryId: $item->categoryId,
name: $item->name,
label: $item->label,
position: $item->position,
active: $item->active,
data: $newData,
created: $item->created,
updated: time(),
));
}
}
}

echo str_repeat('-', 70) . "\n";
echo $dryRun
? "Dry-run: {$promoted} would be promoted across {$itemsTouched} items, {$dupes} skipped (dupe), {$missing} missing.\n"
: "Done: {$promoted} promoted across {$itemsTouched} items, {$dupes} skipped (dupe), {$missing} missing.\n";
52 changes: 5 additions & 47 deletions boot/Editor/Api/UploadEndpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Imanager\Storage\FileRepository;
use Imanager\Validation\Sanitizer as ImanagerSanitizer;
use Scriptor\Boot\Editor\Editor;
use Scriptor\Boot\Files\DirectoryCleanup;

/**
* Phase 14d-1 upload endpoint — JSON API mounted at `/editor/api/upload`,
Expand All @@ -38,6 +39,9 @@
* tokenName, tokenValue
* Response 200: {"status": "ok"}
*
* Captions / titles do not have an endpoint here: they live on the
* page form and travel with the page-save POST as `image_titles[<id>]`.
*
* Auth gate sits one level up in EditorRouter (anonymous requests get
* 302'd to /editor/auth/ before reaching here). CSRF is enforced
* locally for both verbs because FilePond posts JSON-friendly form
Expand All @@ -57,7 +61,6 @@ public function handle(string $method): never
{
match (strtoupper($method)) {
'POST' => $this->handlePost(),
'PATCH' => $this->handlePatch(),
'DELETE' => $this->handleDelete(),
default => $this->error(405, 'Method not allowed'),
};
Expand Down Expand Up @@ -137,40 +140,6 @@ private function ensureThumbnail(string $sourceRel, int $width, int $height): ?s
return $thumbRel;
}

/**
* Update the title (caption / alt text) of an existing file row.
* Body fields (form-urlencoded; PHP doesn't populate `$_POST` for
* PATCH so the body is parsed via {@see parseBody()}):
*
* fileId file id to update
* title new title; empty string clears the caption
* tokenName, tokenValue
*
* Returns 200 `{"status":"ok","title":"..."}` on success,
* 404 when the file id is unknown, 400 on bad input.
*/
private function handlePatch(): never
{
$body = self::parseBody();
$token = (string) ($body['tokenName'] ?? $this->editor->input->getString('tokenName'));
$tokenV = (string) ($body['tokenValue'] ?? $this->editor->input->getString('tokenValue'));
$this->assertCsrf($token, $tokenV);

$fileId = isset($body['fileId']) ? (int) $body['fileId'] : 0;
if ($fileId < 1) {
$this->error(400, 'fileId is required');
}
$title = isset($body['title']) ? (string) $body['title'] : '';

$file = $this->files->find($fileId);
if ($file === null) {
$this->error(404, 'File not found');
}

$updated = $this->files->save($file->withTitle($title));
$this->json(200, ['status' => 'ok', 'title' => $updated->title]);
}

private function handleDelete(): never
{
// PHP doesn't populate $_POST for DELETE bodies, so parse the
Expand Down Expand Up @@ -198,18 +167,7 @@ private function handleDelete(): never
$this->json(200, ['status' => 'gone']);
}

// Best-effort cleanup of the matching thumbnail next door.
$thumbDir = \dirname($file->path) . '/thumbnail';
if ($this->storage->exists($thumbDir)) {
// No bulk-delete on FileStorage; tolerate missing matches.
foreach (['300x300', '600x600', '1200x0', '800x350'] as $size) {
$thumb = $thumbDir . '/' . $size . '_' . $file->name;
if ($this->storage->exists($thumb)) {
$this->storage->delete($thumb);
}
}
}
$this->storage->delete($file->path);
DirectoryCleanup::purge($this->storage, $file->path);
$this->files->delete($fileId);
$this->json(200, ['status' => 'ok']);
}
Expand Down
Loading