73 lines
2.5 KiB
PHP
73 lines
2.5 KiB
PHP
<?php
|
|
// /var/www/html/api/screens-thumbnails.php
|
|
// Maps carto URLs to available L99 screenshots. Zero-regression, read-only.
|
|
// Returns: {"by_url": {url: "/l99-screenshots/xxx.png", ...}, "total_available": N}
|
|
header("Content-Type: application/json; charset=utf-8");
|
|
header("Access-Control-Allow-Origin: *");
|
|
header("Cache-Control: public, max-age=60");
|
|
|
|
$L99_DIR = "/opt/weval-l99/screenshots";
|
|
$WEB_BASE = "/l99-screenshots";
|
|
|
|
if (!is_dir($L99_DIR)) {
|
|
http_response_code(503);
|
|
echo json_encode(["error" => "l99_screenshots_dir_missing"]);
|
|
exit;
|
|
}
|
|
|
|
// Load all available screenshots once
|
|
$files = @scandir($L99_DIR) ?: [];
|
|
$available = [];
|
|
foreach ($files as $f) {
|
|
if (preg_match('/\.(png|jpg|jpeg|webp)$/i', $f)) {
|
|
// Normalize: strip prefixes (alive-, prod-, sso-, v01-, v02-, v03-, app-) and suffix (extension)
|
|
$base = preg_replace('/\.(png|jpg|jpeg|webp)$/i', '', $f);
|
|
$norm = preg_replace('/^(alive|prod|sso|app|v0[0-9]|resp|test|ux)-/', '', $base);
|
|
$norm = strtolower($norm);
|
|
// Store both exact and normalized
|
|
$available[strtolower($base)] = $f;
|
|
if ($norm !== strtolower($base)) {
|
|
$available[$norm] = $f;
|
|
}
|
|
}
|
|
}
|
|
|
|
// Read carto DATA to match
|
|
$carto = @file_get_contents("/var/www/html/cartographie-screens.html") ?: "";
|
|
$mapping = [];
|
|
if (preg_match('/const DATA = (\[.*?\]);/s', $carto, $m)) {
|
|
$data = json_decode($m[1], true) ?: [];
|
|
foreach ($data as $entry) {
|
|
$url = $entry["url"] ?? "";
|
|
$name = $entry["name"] ?? "";
|
|
if (!$url || !$name) continue;
|
|
// Normalize name: strip .html/.php, lowercase
|
|
$key = strtolower(preg_replace('/\.(html|php)$/i', '', $name));
|
|
// Try multiple match strategies
|
|
$found = null;
|
|
if (isset($available[$key])) {
|
|
$found = $available[$key];
|
|
} elseif (isset($available[$key . "_html"])) {
|
|
$found = $available[$key . "_html"];
|
|
} else {
|
|
// Fuzzy: find any filename containing the key as substring
|
|
foreach ($available as $norm => $file) {
|
|
if (strpos($norm, $key) !== false && strlen($key) > 3) {
|
|
$found = $file;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
if ($found) {
|
|
$mapping[$url] = $WEB_BASE . "/" . $found;
|
|
}
|
|
}
|
|
}
|
|
|
|
echo json_encode([
|
|
"generated_at" => date("c"),
|
|
"total_screenshots_available" => count(array_unique($available)),
|
|
"total_urls_matched" => count($mapping),
|
|
"by_url" => $mapping
|
|
], JSON_UNESCAPED_SLASHES);
|