[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']]);

View File

@@ -474,7 +474,7 @@ class ScoreScanner {
rmdir($dir);
}
public function uploadPdf(string $scoreId, array $file, string $piece, string $instrument, string $version, string $filename): array {
public function uploadPdf(string $scoreId, array $file, string $piece, string $instrument, string $version, string $key = '', string $clef = '', string $variant = '', string $part = '1'): array {
$scoreDir = $this->scoresPath . $scoreId;
if (!is_dir($scoreDir)) {
@@ -484,18 +484,49 @@ class ScoreScanner {
// Create directory structure: scoreId/piece/instrument/version
$targetDir = $scoreDir . '/' . $piece . '/' . $instrument . '/' . $version;
if (!mkdir($targetDir, 0755, true)) {
return ['success' => false, 'error' => 'Failed to create directory'];
if (!is_dir($targetDir)) {
if (!mkdir($targetDir, 0755, true)) {
return ['success' => false, 'error' => 'Failed to create directory'];
}
}
// Determine filename
if (empty($filename)) {
$filename = $file['name'];
// Map instrument code to name
$instrumentNames = [
'dir' => 'direction', 'pic' => 'piccolo', 'flu' => 'flute', 'cla' => 'clarinette',
'clb' => 'clarinette_basse', 'sax' => 'saxophone_alto', 'sat' => 'saxophone_tenor',
'sab' => 'saxophone_baryton', 'coa' => 'cor_anglais', 'htb' => 'hautbois',
'bas' => 'basson', 'cor' => 'cor', 'trp' => 'trompette', 'crn' => 'cornet',
'trb' => 'trombone', 'eup' => 'euphonium', 'tub' => 'tuba', 'cba' => 'contrebasse',
'per' => 'percussion', 'pia' => 'piano', 'har' => 'harpe'
];
$instName = $instrumentNames[$instrument] ?? $instrument;
// Build filename: instrument_variant_key_clef_part.pdf
$filenameParts = [$instName];
if (!empty($variant)) {
$filenameParts[] = $variant;
}
if (!empty($key)) {
$filenameParts[] = $key;
}
if (!empty($clef)) {
$filenameParts[] = $clef;
}
$filenameParts[] = $part;
$filename = implode('_', $filenameParts) . '.pdf';
$targetPath = $targetDir . '/' . $filename;
if (move_uploaded_file($file['tmp_name'], $targetPath)) {
// Use copy for CLI testing, move_uploaded_file for real uploads
if (is_uploaded_file($file['tmp_name'])) {
$result = move_uploaded_file($file['tmp_name'], $targetPath);
} else {
$result = copy($file['tmp_name'], $targetPath);
}
if ($result) {
return ['success' => true, 'path' => "$scoreId/$piece/$instrument/$version/$filename"];
}

2
api/php-upload.ini Normal file
View File

@@ -0,0 +1,2 @@
upload_max_filesize = 64M
post_max_size = 64M