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
|
|
@ -4,28 +4,86 @@ require __DIR__ . '/../auth.php';
|
|||
mvlog_require_login();
|
||||
|
||||
// Web-server side handler to toggle "Show" for an MVLog job and send a web-push immediately if appropriate.
|
||||
// Usage (POST): job=<job> show=1|0
|
||||
// Preferred usage (POST): id=<article_id> show=1|0
|
||||
// Legacy fallback (POST): job=<job_dir_name> show=1|0
|
||||
|
||||
$MVLOG_ROOT = dirname(__DIR__);
|
||||
$job = $_POST['job'] ?? '';
|
||||
$id = trim((string)($_POST['id'] ?? ''));
|
||||
$job = trim((string)($_POST['job'] ?? ''));
|
||||
$show = isset($_POST['show']) ? filter_var($_POST['show'], FILTER_VALIDATE_BOOLEAN) : true;
|
||||
|
||||
// Basic validation
|
||||
if (!preg_match('/^[0-9]{8}[-_][A-Za-z0-9._-]+$/', $job)) {
|
||||
http_response_code(400);
|
||||
echo '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 '';
|
||||
$id = strtolower(trim((string)@file_get_contents($path)));
|
||||
return mvlog_article_id_is_valid($id) ? $id : '';
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
function mvlog_resolve_jobdir(string $mvlogRoot, string $id, string $job, ?string &$resolvedJob, ?string &$resolvedId): ?string {
|
||||
$resolvedJob = null;
|
||||
$resolvedId = null;
|
||||
|
||||
if ($id !== '' && mvlog_article_id_is_valid($id)) {
|
||||
$dir = mvlog_find_jobdir_by_id($mvlogRoot, strtolower($id));
|
||||
if ($dir !== null) {
|
||||
$resolvedJob = basename($dir);
|
||||
$resolvedId = mvlog_read_article_id($dir);
|
||||
return $dir;
|
||||
}
|
||||
}
|
||||
|
||||
if ($job !== '' && mvlog_legacy_job_is_valid($job)) {
|
||||
$dir = $mvlogRoot . '/in-dir/' . basename($job);
|
||||
if (is_dir($dir)) {
|
||||
$resolvedJob = basename($dir);
|
||||
$resolvedId = mvlog_read_article_id($dir);
|
||||
return $dir;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
$resolvedJob = null;
|
||||
$resolvedId = null;
|
||||
$jobdir = mvlog_resolve_jobdir($MVLOG_ROOT, strtolower($id), $job, $resolvedJob, $resolvedId);
|
||||
if ($jobdir === null) {
|
||||
http_response_code(404);
|
||||
echo 'job not found';
|
||||
exit;
|
||||
}
|
||||
|
||||
$enabled_file = "$jobdir/.movmaker-enabled";
|
||||
$cache_file = "$MVLOG_ROOT/cache/push_notifications.json";
|
||||
|
||||
if ($show) {
|
||||
file_put_contents($enabled_file, date(DATE_ATOM) . PHP_EOL, LOCK_EX);
|
||||
|
|
@ -35,20 +93,16 @@ if ($show) {
|
|||
if (is_file(__DIR__ . '/../lib/send_push.php')) {
|
||||
include_once __DIR__ . '/../lib/send_push.php';
|
||||
try {
|
||||
$sent = try_send_show_notification($job, $jobdir, $MVLOG_ROOT, '/var/log/mvlog_notify.log');
|
||||
$sent = try_send_show_notification($resolvedId ?: $resolvedJob, $jobdir, $MVLOG_ROOT, '/var/log/mvlog_notify.log');
|
||||
} catch (Throwable $e) {
|
||||
error_log("[mvlog] notify exception for $job: " . $e->getMessage() . "\n", 3, '/var/log/mvlog_notify.log');
|
||||
error_log("[mvlog] notify exception for " . ($resolvedId ?: $resolvedJob) . ": " . $e->getMessage() . "\n", 3, '/var/log/mvlog_notify.log');
|
||||
$sent = false;
|
||||
}
|
||||
} else {
|
||||
error_log("[mvlog] send_push helper missing, cannot send notification for $job\n", 3, '/var/log/mvlog_notify.log');
|
||||
error_log("[mvlog] send_push helper missing, cannot send notification for " . ($resolvedId ?: $resolvedJob) . "\n", 3, '/var/log/mvlog_notify.log');
|
||||
}
|
||||
|
||||
if ($sent) {
|
||||
echo 'enabled';
|
||||
} else {
|
||||
echo 'enabled (no notification)';
|
||||
}
|
||||
echo $sent ? 'enabled' : 'enabled (no notification)';
|
||||
} else {
|
||||
@unlink($enabled_file);
|
||||
echo 'disabled';
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue