[WIP] Skeleton of the admin part.

This commit is contained in:
NADAL Jean-Baptiste
2026-02-18 14:04:34 +01:00
parent 1fb0072fc7
commit 3abc6f6371
6 changed files with 592 additions and 11 deletions

View File

@@ -1,7 +1,11 @@
<?php
// Increase upload limits for this script
ini_set('upload_max_filesize', '64M');
ini_set('post_max_size', '64M');
header('Content-Type: application/json');
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');
header('Access-Control-Allow-Headers: Authorization, Content-Type');
@@ -31,6 +35,12 @@ $method = $_SERVER['REQUEST_METHOD'];
$uri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
$path = trim($uri, '/');
// Remove 'api/' prefix if present
$path = preg_replace('#^api/#', '', $path);
// Debug: log the path
// file_put_contents('/tmp/api_debug.log', date('Y-m-d H:i:s') . " PATH: $path METHOD: $method\n", FILE_APPEND);
// GET /download/:path - Download PDF (BEFORE auth check)
if (preg_match('#^download/([^?]+)#', $path, $matches) && $method === 'GET') {
$filePath = urldecode($matches[1]);
@@ -68,7 +78,7 @@ if (preg_match('#^download/([^?]+)#', $path, $matches) && $method === 'GET') {
}
// Route matching
if ($path === 'login' && $method === 'POST') {
if (($path === 'login' || $path === 'api/login') && $method === 'POST') {
$input = json_decode(file_get_contents('php://input'), true);
$username = $input['username'] ?? '';
$password = $input['password'] ?? '';
@@ -162,7 +172,27 @@ if ($path === 'admin/scores' && $method === 'POST') {
$name = $input['name'] ?? '';
$compositor = $input['compositor'] ?? '';
if (empty($id) || empty($name)) {
// Auto-generate ID if not provided
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;
}
$id = strval($maxId + 1);
// Pad with zeros to 3 digits if needed
if (strlen($id) < 3) {
$id = str_pad($id, 3, '0', STR_PAD_LEFT);
}
}
if (empty($name)) {
http_response_code(400);
echo json_encode(['error' => 'ID and name required']);
exit;
@@ -227,9 +257,12 @@ if (preg_match('#^admin/scores/(\d+)/upload$#', $path, $matches) && $method ===
$piece = $_POST['piece'] ?? '1';
$instrument = $_POST['instrument'] ?? '';
$version = $_POST['version'] ?? '1';
$filename = $_POST['filename'] ?? '';
$key = $_POST['key'] ?? '';
$clef = $_POST['clef'] ?? '';
$variant = $_POST['variant'] ?? '';
$part = $_POST['part'] ?? '1';
$result = $scanner->uploadPdf($scoreId, $file, $piece, $instrument, $version, $filename);
$result = $scanner->uploadPdf($scoreId, $file, $piece, $instrument, $version, $key, $clef, $variant, $part);
if ($result['success']) {
echo json_encode(['success' => true, 'path' => $result['path']]);