diff --git a/.gitignore b/.gitignore index f3224d7..a697b3b 100644 --- a/.gitignore +++ b/.gitignore @@ -5,5 +5,7 @@ partitions/.svelte-kit/ partitions/build/ partitions/node_modules/ partitions/static/pdf.worker.min.js +partitions/.env api/vendor/ api/composer.phar +api/.env diff --git a/api/index.php b/api/index.php index 4b78d66..2216829 100644 --- a/api/index.php +++ b/api/index.php @@ -289,16 +289,17 @@ if ($path === 'admin/scores' && $method === 'POST') { if (empty($id)) { $scores = $scanner->listScores(); $maxId = 0; - foreach ($scores as $s) { - $num = intval($s['id']); - if ($num > $maxId) $maxId = $maxId; - } - // Find highest numeric ID foreach ($scores as $s) { $num = intval($s['id']); if ($num > $maxId) $maxId = $num; } + + // Skip existing IDs + $scoresPath = getenv('SCORES_PATH') ?: __DIR__ . '/../legacy/Scores/'; $id = strval($maxId + 1); + while (is_dir($scoresPath . $id)) { + $id = strval(intval($id) + 1); + } // Pad with zeros to 3 digits if needed if (strlen($id) < 3) { $id = str_pad($id, 3, '0', STR_PAD_LEFT); diff --git a/api/lib/ScoreScanner.php b/api/lib/ScoreScanner.php index 88a955e..97438c3 100644 --- a/api/lib/ScoreScanner.php +++ b/api/lib/ScoreScanner.php @@ -47,15 +47,40 @@ class ScoreScanner { } $ini = @parse_ini_file($iniFile, true); + + // If parse fails, try to extract info manually if ($ini === false) { + $content = file_get_contents($iniFile); + + $name = ''; + $compositor = ''; + + if (preg_match('/name\s*=\s*(.+)/i', $content, $matches)) { + $name = trim($matches[1]); + } + if (preg_match('/compositor\s*=\s*(.+)/i', $content, $matches)) { + $compositor = trim($matches[1]); + } + + if ($name || $compositor) { + return [ + 'id' => $id, + 'name' => $name, + 'compositor' => $compositor, + 'ressource' => null + ]; + } + return null; } + $info = $ini['info'] ?? []; + return [ 'id' => $id, - 'name' => $ini['info']['name'] ?? 'Inconnu', - 'compositor' => $ini['info']['compositor'] ?? 'Inconnu', - 'ressource' => $ini['info']['ressource'] ?? null + 'name' => $info['name'] ?? '', + 'compositor' => $info['compositor'] ?? '', + 'ressource' => $info['ressource'] ?? null ]; } diff --git a/api/tests.php b/api/tests.php index 179f00e..7911d37 100644 --- a/api/tests.php +++ b/api/tests.php @@ -175,6 +175,14 @@ class APITest { public function testCreateScoreWithPieces() { $this->section("Create Score with Pieces - Functional"); + // Test 0: Try to create score with existing ID should fail + $result = $this->request('POST', 'admin/scores', [ + 'name' => 'Test Duplicate', + 'compositor' => 'Test', + 'id' => '001' + ]); + $this->test('Create with existing ID returns error', $result['code'] === 400 && isset($result['body']['error'])); + // Test 1: Create score with 2 pieces $testName = 'Test Score ' . date('YmdHis'); $result = $this->request('POST', 'admin/scores', [