geoproject-app/public/site/plugins/map-editor/routes/image.php
isUnknown 590c842072
All checks were successful
Deploy / Build and Deploy to Production (push) Successful in 15s
refactor: split API routes into separate files
Reorganize routes following design-to-pack pattern:
- routes/markers.php: CRUD operations for markers
- routes/position.php: Position update (single mode)
- routes/image.php: Image capture and regeneration flag management

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2026-02-06 11:55:55 +01:00

185 lines
5.8 KiB
PHP

<?php
/**
* Image Capture Routes
* POST for capturing map image, GET/DELETE for regeneration flag
*/
return [
// POST capture and save map image
[
'pattern' => 'map-editor/pages/(:all)/capture-image',
'method' => 'POST',
'auth' => false,
'action' => function (string $pageId) {
try {
$user = kirby()->user();
if (!$user && !kirby()->option('debug', false)) {
return [
'status' => 'error',
'message' => 'Unauthorized',
'code' => 401
];
}
$mapPage = kirby()->page($pageId);
if (!$mapPage) {
return [
'status' => 'error',
'message' => 'Map page not found',
'code' => 404
];
}
if (!$mapPage->permissions()->can('update')) {
return [
'status' => 'error',
'message' => 'Forbidden',
'code' => 403
];
}
$data = kirby()->request()->data();
if (!isset($data['image'])) {
return [
'status' => 'error',
'message' => 'Image data is required',
'code' => 400
];
}
$imageData = $data['image'];
if (preg_match('/^data:image\/(png|jpeg|jpg);base64,(.+)$/', $imageData, $matches)) {
$imageData = $matches[2];
$extension = $matches[1] === 'jpeg' ? 'jpg' : $matches[1];
} else {
return [
'status' => 'error',
'message' => 'Invalid image format',
'code' => 400
];
}
$decodedImage = base64_decode($imageData);
if ($decodedImage === false) {
return [
'status' => 'error',
'message' => 'Failed to decode image',
'code' => 400
];
}
$filename = 'map-static.' . $extension;
$tempPath = sys_get_temp_dir() . '/' . uniqid() . '.' . $extension;
file_put_contents($tempPath, $decodedImage);
$existingFile = $mapPage->files()->filterBy('name', 'map-static')->first();
if ($existingFile) {
$existingFile->delete();
}
try {
$file = $mapPage->createFile([
'source' => $tempPath,
'filename' => $filename
]);
@unlink($tempPath);
} catch (Exception $e) {
@unlink($tempPath);
throw $e;
}
return [
'status' => 'success',
'data' => [
'message' => 'Image saved successfully',
'filename' => $filename,
'path' => $file->root()
]
];
} catch (Exception $e) {
return [
'status' => 'error',
'message' => $e->getMessage(),
'code' => 500
];
}
}
],
// GET check if regeneration flag exists
[
'pattern' => 'map-editor/pages/(:all)/check-regenerate-flag',
'method' => 'GET',
'auth' => false,
'action' => function (string $pageId) {
try {
$page = kirby()->page($pageId);
if (!$page) {
return [
'status' => 'error',
'message' => 'Page not found',
'code' => 404
];
}
$markerFile = $page->root() . '/.regenerate-map-image';
$needsRegeneration = file_exists($markerFile);
return [
'status' => 'success',
'data' => [
'needsRegeneration' => $needsRegeneration
]
];
} catch (Exception $e) {
return [
'status' => 'error',
'message' => $e->getMessage(),
'code' => 500
];
}
}
],
// DELETE clear regeneration flag
[
'pattern' => 'map-editor/pages/(:all)/clear-regenerate-flag',
'method' => 'DELETE',
'auth' => false,
'action' => function (string $pageId) {
try {
$page = kirby()->page($pageId);
if (!$page) {
return [
'status' => 'error',
'message' => 'Page not found',
'code' => 404
];
}
$markerFile = $page->root() . '/.regenerate-map-image';
if (file_exists($markerFile)) {
unlink($markerFile);
}
return [
'status' => 'success',
'data' => [
'message' => 'Flag cleared'
]
];
} catch (Exception $e) {
return [
'status' => 'error',
'message' => $e->getMessage(),
'code' => 500
];
}
}
]
];