Switch admin/API flows to article IDs with legacy job fallback
This commit is contained in:
parent
3cc2828bc0
commit
efcf87d00c
5 changed files with 205 additions and 67 deletions
|
|
@ -7,17 +7,57 @@ mvlog_require_login();
|
|||
$MVLOG_ROOT = dirname(__DIR__);
|
||||
header('Content-Type: application/json; charset=utf-8');
|
||||
|
||||
$job = $_POST['job'] ?? '';
|
||||
$id = strtolower(trim((string)($_POST['id'] ?? '')));
|
||||
$job = trim((string)($_POST['job'] ?? ''));
|
||||
$max_frames = isset($_POST['max_frames']) ? intval($_POST['max_frames']) : 16;
|
||||
|
||||
if (!preg_match('/^[0-9]{8}[-_][A-Za-z0-9._-]+$/', $job)) {
|
||||
http_response_code(400);
|
||||
echo json_encode(['status'=>'error','message'=>'invalid job']);
|
||||
exit;
|
||||
function mvlog_article_id_is_valid(string $id): bool {
|
||||
return (bool)preg_match('/^[0-9]{14}[a-f0-9]{16}$/', $id);
|
||||
}
|
||||
|
||||
$jobdir = $MVLOG_ROOT . '/in-dir/' . $job;
|
||||
if (!is_dir($jobdir)) {
|
||||
function mvlog_legacy_job_is_valid(string $job): bool {
|
||||
return (bool)preg_match('/^[0-9]{8}[-_][A-Za-z0-9._-]+$/', $job);
|
||||
}
|
||||
|
||||
function mvlog_read_article_id(string $jobdir): string {
|
||||
$path = $jobdir . '/.mvlog-id';
|
||||
if (!is_file($path)) return '';
|
||||
$value = strtolower(trim((string)@file_get_contents($path)));
|
||||
return mvlog_article_id_is_valid($value) ? $value : '';
|
||||
}
|
||||
|
||||
function mvlog_find_jobdir_by_id(string $mvlogRoot, string $articleId): ?string {
|
||||
if (!mvlog_article_id_is_valid($articleId)) return null;
|
||||
|
||||
$cacheFile = $mvlogRoot . '/cache/articles-index.json';
|
||||
if (is_file($cacheFile)) {
|
||||
$cache = json_decode((string)@file_get_contents($cacheFile), true);
|
||||
if (is_array($cache) && is_array($cache['by_id'] ?? null)) {
|
||||
$name = (string)($cache['by_id'][$articleId] ?? '');
|
||||
if ($name !== '') {
|
||||
$candidate = $mvlogRoot . '/in-dir/' . basename($name);
|
||||
if (is_dir($candidate)) return $candidate;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
foreach (glob($mvlogRoot . '/in-dir/*', GLOB_ONLYDIR) ?: [] as $dir) {
|
||||
if (mvlog_read_article_id($dir) === $articleId) return $dir;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
$jobdir = null;
|
||||
if ($id !== '' && mvlog_article_id_is_valid($id)) {
|
||||
$jobdir = mvlog_find_jobdir_by_id($MVLOG_ROOT, $id);
|
||||
}
|
||||
if ($jobdir === null && $job !== '' && mvlog_legacy_job_is_valid($job)) {
|
||||
$candidate = $MVLOG_ROOT . '/in-dir/' . basename($job);
|
||||
if (is_dir($candidate)) $jobdir = $candidate;
|
||||
}
|
||||
|
||||
if ($jobdir === null || !is_dir($jobdir)) {
|
||||
http_response_code(404);
|
||||
echo json_encode(['status'=>'error','message'=>'job not found']);
|
||||
exit;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue