false, 'error' => 'Non autorisé']); exit; } // Vérification de la méthode HTTP if ($_SERVER['REQUEST_METHOD'] !== 'POST') { http_response_code(405); echo json_encode(['success' => false, 'error' => 'Méthode non autorisée']); exit; } // Récupération et validation des données $input = json_decode(file_get_contents('php://input'), true); $storyId = $input['id'] ?? null; if (!$storyId) { http_response_code(400); echo json_encode(['success' => false, 'error' => 'ID du roman manquant']); exit; } try { // Récupération du roman $story = Stories::get($storyId); if (!$story) { throw new Exception('Roman non trouvé'); } // 1. Suppression de l'image de couverture if (!empty($story['cover'])) { $coverPath = __DIR__ . '/../../' . $story['cover']; if (file_exists($coverPath)) { unlink($coverPath); } } // 2. Suppression des images des chapitres $chaptersImagesDir = __DIR__ . '/../../assets/images/chapters/' . $storyId; if (file_exists($chaptersImagesDir)) { // Parcours récursif du dossier des images $files = new RecursiveIteratorIterator( new RecursiveDirectoryIterator($chaptersImagesDir, RecursiveDirectoryIterator::SKIP_DOTS), RecursiveIteratorIterator::CHILD_FIRST ); // Suppression de tous les fichiers et sous-dossiers foreach ($files as $file) { if ($file->isDir()) { rmdir($file->getRealPath()); } else { unlink($file->getRealPath()); } } // Suppression du dossier principal des images du roman rmdir($chaptersImagesDir); } // 3. Suppression du fichier JSON du roman $storyFile = __DIR__ . '/../../stories/' . $storyId . '.json'; if (file_exists($storyFile)) { unlink($storyFile); // Réponse de succès echo json_encode([ 'success' => true, 'message' => 'Roman et médias associés supprimés avec succès' ]); } else { throw new Exception('Fichier du roman introuvable'); } } catch (Exception $e) { http_response_code(500); echo json_encode([ 'success' => false, 'error' => 'Erreur lors de la suppression : ' . $e->getMessage() ]); }