mirror of
https://github.com/ceratic/MediaCollectorLibary.git
synced 2026-05-13 23:56:46 +02:00
158 lines
5.3 KiB
PHP
158 lines
5.3 KiB
PHP
<?php
|
|
|
|
namespace App\Controllers;
|
|
|
|
use Psr\Http\Message\ResponseInterface as Response;
|
|
use Psr\Http\Message\ServerRequestInterface as Request;
|
|
use App\Models\Source;
|
|
use App\Models\SyncLog;
|
|
use App\Services\SteamSyncService;
|
|
use App\Services\JellyfinSyncService;
|
|
use App\Services\StashSyncService;
|
|
use App\Services\XbvrSyncService;
|
|
use App\Services\AdultSyncService;
|
|
use App\Services\ExophaseSyncService;
|
|
use PDO;
|
|
use Slim\Views\Twig;
|
|
|
|
class AdminController extends Controller
|
|
{
|
|
private PDO $pdo;
|
|
|
|
public function __construct(PDO $pdo, Twig $view)
|
|
{
|
|
parent::__construct($view);
|
|
$this->pdo = $pdo;
|
|
}
|
|
|
|
public function index(Request $request, Response $response, $args)
|
|
{
|
|
$sourceModel = new Source($this->pdo);
|
|
$sources = $sourceModel->findAll();
|
|
|
|
$syncLogModel = new SyncLog($this->pdo);
|
|
$recentSyncs = SyncLog::getRecent($this->pdo, 10);
|
|
|
|
return $this->view->render($response, 'admin/index.twig', [
|
|
'title' => 'Admin Dashboard',
|
|
'sources' => $sources,
|
|
'recent_syncs' => $recentSyncs
|
|
]);
|
|
}
|
|
|
|
public function syncSource(Request $request, Response $response, $args)
|
|
{
|
|
$sourceId = $args['id'];
|
|
$syncType = $request->getQueryParams()['type'] ?? 'full';
|
|
|
|
$sourceModel = new Source($this->pdo);
|
|
$source = $sourceModel->find($sourceId);
|
|
|
|
if (!$source) {
|
|
return $response->withStatus(404)->withHeader('Content-Type', 'application/json');
|
|
}
|
|
|
|
// Validate sync type based on source type
|
|
if ($source['name'] === 'jellyfin') {
|
|
$validSyncTypes = ['full', 'incremental', 'all', 'movies', 'tvshows'];
|
|
if (!in_array($syncType, $validSyncTypes)) {
|
|
return $this->json($response, [
|
|
'success' => false,
|
|
'message' => 'Invalid sync type for Jellyfin source. Valid types: ' . implode(', ', $validSyncTypes)
|
|
], 400);
|
|
}
|
|
} else {
|
|
// For other sources, only allow full/incremental
|
|
$validSyncTypes = ['full', 'incremental'];
|
|
if (!in_array($syncType, $validSyncTypes)) {
|
|
return $this->json($response, [
|
|
'success' => false,
|
|
'message' => 'Invalid sync type. Valid types: ' . implode(', ', $validSyncTypes)
|
|
], 400);
|
|
}
|
|
}
|
|
|
|
// Start sync in background (simplified - in production you'd use queues)
|
|
$syncLogId = $this->startSync($source, $syncType);
|
|
|
|
return $this->json($response, [
|
|
'success' => true,
|
|
'sync_log_id' => $syncLogId,
|
|
'message' => 'Sync started successfully'
|
|
]);
|
|
}
|
|
|
|
public function syncStatus(Request $request, Response $response, $args)
|
|
{
|
|
$syncLogId = $args['id'];
|
|
|
|
$syncLogModel = new SyncLog($this->pdo);
|
|
$syncLog = $syncLogModel->find($syncLogId);
|
|
|
|
if (!$syncLog) {
|
|
return $response->withStatus(404)->withHeader('Content-Type', 'application/json');
|
|
}
|
|
|
|
return $this->json($response, [
|
|
'id' => $syncLog['id'],
|
|
'status' => $syncLog['status'],
|
|
'sync_type' => $syncLog['sync_type'],
|
|
'total_items' => $syncLog['total_items'] ?? 0,
|
|
'processed_items' => $syncLog['processed_items'],
|
|
'new_items' => $syncLog['new_items'],
|
|
'updated_items' => $syncLog['updated_items'],
|
|
'deleted_items' => $syncLog['deleted_items'],
|
|
'started_at' => $syncLog['started_at'],
|
|
'completed_at' => $syncLog['completed_at'],
|
|
'message' => $syncLog['message'],
|
|
'errors' => $syncLog['errors'] ? json_decode($syncLog['errors'], true) : [],
|
|
'progress_percentage' => $this->calculateProgressPercentage($syncLog)
|
|
]);
|
|
}
|
|
|
|
private function calculateProgressPercentage(array $syncLog): float
|
|
{
|
|
$total = $syncLog['total_items'] ?? 0;
|
|
if ($total <= 0) return 0;
|
|
|
|
$processed = $syncLog['processed_items'] ?? 0;
|
|
return min(100, round(($processed / $total) * 100, 2));
|
|
}
|
|
|
|
public function sources(Request $request, Response $response, $args)
|
|
{
|
|
$sourceModel = new Source($this->pdo);
|
|
$sources = $sourceModel->findAll();
|
|
|
|
return $this->view->render($response, 'admin/sources.twig', [
|
|
'title' => 'Source Management',
|
|
'sources' => $sources
|
|
]);
|
|
}
|
|
|
|
private function startSync(array $source, string $syncType): int
|
|
{
|
|
// Create appropriate sync service based on source type
|
|
switch ($source['name']) {
|
|
case 'steam':
|
|
$syncService = new SteamSyncService($this->pdo, $source);
|
|
break;
|
|
case 'jellyfin':
|
|
$syncService = new JellyfinSyncService($this->pdo, $source);
|
|
break;
|
|
case 'stash':
|
|
$syncService = new StashSyncService($this->pdo, $source);
|
|
break;
|
|
case 'adult':
|
|
$syncService = new AdultSyncService($this->pdo, $source);
|
|
break;
|
|
case 'xbvr':
|
|
$syncService = new XbvrSyncService($this->pdo, $source);
|
|
break;
|
|
}
|
|
|
|
// Start sync (this would typically be queued in production)
|
|
return $syncService->startSync($syncType);
|
|
}
|
|
}
|