file($realPath); } // Map queue_type to file_type if not explicitly given if ($fileType === '') { $fileTypeMap = [ 'cover' => 'cover', 'note_intention' => 'note_intention', 'tfe' => 'main', 'annexe' => 'annex', ]; $fileType = $fileTypeMap[$queueType] ?? 'main'; } require_once APP_ROOT . '/src/Database.php'; $db = Database::getInstance(); // Check if this file is already linked to this thesis (avoid duplicate) $pdo = $db->getConnection(); $stmt = $pdo->prepare('SELECT id FROM thesis_files WHERE thesis_id = ? AND file_path = ?'); $stmt->execute([$thesisId, $filePath]); if ($stmt->fetch()) { http_response_code(409); die('Ce fichier est déjà lié à ce TFE.'); } $db->insertThesisFile( $thesisId, $fileType, $filePath, $fileName, $fileSize, $mimeType, null, null ); // Get the new file's ID $newId = $pdo->lastInsertId(); error_log("[relink] thesis_id=$thesisId file_path=$filePath file_type=$fileType new_id=$newId"); header('Content-Type: application/json'); echo json_encode([ 'ok' => true, 'id' => (int)$newId, 'message' => 'Fichier relié avec succès.', ]); exit;