diff --git a/admin/api/export-stories.php b/admin/api/export-stories.php index 866e30d..3817083 100644 --- a/admin/api/export-stories.php +++ b/admin/api/export-stories.php @@ -37,18 +37,32 @@ try { $storyDir = $tempDir . '/' . $storyId; mkdir($storyDir); mkdir($storyDir . '/images'); + mkdir($storyDir . '/chapter_covers'); - // Copier l'image de couverture si elle existe + // Copier l'image de couverture du roman si elle existe if (!empty($story['cover'])) { $coverPath = __DIR__ . '/../../' . $story['cover']; if (file_exists($coverPath)) { - copy($coverPath, $storyDir . '/cover' . pathinfo($coverPath, PATHINFO_EXTENSION)); - $story['cover'] = 'cover' . pathinfo($coverPath, PATHINFO_EXTENSION); + $extension = '.' . pathinfo($coverPath, PATHINFO_EXTENSION); + copy($coverPath, $storyDir . '/cover' . $extension); + $story['cover'] = 'cover' . $extension; } } // Extraire et copier les images des chapitres foreach ($story['chapters'] as &$chapter) { + // Gestion des images de couverture des chapitres + if (!empty($chapter['cover'])) { + $chapterCoverPath = __DIR__ . '/../../' . $chapter['cover']; + if (file_exists($chapterCoverPath)) { + $extension = '.' . pathinfo($chapterCoverPath, PATHINFO_EXTENSION); + $newCoverName = 'chapter_' . $chapter['id'] . '_cover' . $extension; + copy($chapterCoverPath, $storyDir . '/chapter_covers/' . $newCoverName); + $chapter['cover'] = 'chapter_covers/' . $newCoverName; + } + } + + // Gestion du contenu et des images intégrées if (!empty($chapter['content'])) { $content = $chapter['content']; if (is_string($content) && isJson($content)) { @@ -62,7 +76,8 @@ try { $imgUrl = $op['insert']['image']; $imgPath = __DIR__ . '/../../' . preg_replace('/^(?:\.\.\/)+/', '', $imgUrl); if (file_exists($imgPath)) { - $newImgName = 'image_' . uniqid() . pathinfo($imgPath, PATHINFO_EXTENSION); + $extension = '.' . pathinfo($imgPath, PATHINFO_EXTENSION); + $newImgName = 'image_' . uniqid() . $extension; copy($imgPath, $storyDir . '/images/' . $newImgName); $op['insert']['image'] = 'images/' . $newImgName; } diff --git a/admin/api/import-stories.php b/admin/api/import-stories.php index 099b570..24c635b 100644 --- a/admin/api/import-stories.php +++ b/admin/api/import-stories.php @@ -66,25 +66,65 @@ try { continue; } - // Créer les dossiers nécessaires - $coverDir = __DIR__ . '/../../assets/images/covers/'; - $chaptersImgDir = __DIR__ . '/../../assets/images/chapters/' . $storyId; + // Créer les dossiers nécessaires avec les permissions correctes + $baseDir = __DIR__ . '/../../'; + $coverDir = $baseDir . 'assets/images/covers'; + $chaptersImgDir = $baseDir . 'assets/images/chapters/' . $storyId; + $chaptersCoverDir = $baseDir . 'assets/images/chapters/' . $storyId . '/covers'; - if (!file_exists($coverDir)) mkdir($coverDir, 0755, true); - if (!file_exists($chaptersImgDir)) mkdir($chaptersImgDir, 0755, true); + foreach ([$coverDir, $chaptersImgDir, $chaptersCoverDir] as $dir) { + if (!file_exists($dir)) { + if (!mkdir($dir, 0777, true)) { + throw new Exception("Impossible de créer le dossier: $dir"); + } + chmod($dir, 0777); + } + } - // Gérer l'image de couverture + // Gérer l'image de couverture du roman if (!empty($story['cover'])) { $coverFile = $storyDir . '/' . $story['cover']; if (file_exists($coverFile)) { - $newCoverPath = 'assets/images/covers/' . $storyId . '.' . pathinfo($coverFile, PATHINFO_EXTENSION); - copy($coverFile, __DIR__ . '/../../' . $newCoverPath); - $story['cover'] = $newCoverPath; + // Extraire l'extension correctement + $originalName = basename($coverFile); + if (preg_match('/\.([^.]+)$/', $originalName, $matches)) { + $extension = $matches[1]; + $newCoverPath = 'assets/images/covers/' . $storyId . '.' . $extension; + $targetPath = $baseDir . $newCoverPath; + + if (!copy($coverFile, $targetPath)) { + throw new Exception("Impossible de copier la couverture du roman"); + } + chmod($targetPath, 0777); + $story['cover'] = $newCoverPath; + } } } // Gérer les images des chapitres foreach ($story['chapters'] as &$chapter) { + // Gérer les images de couverture des chapitres + if (!empty($chapter['cover'])) { + $chapterCoverFile = $storyDir . '/' . $chapter['cover']; + if (file_exists($chapterCoverFile)) { + // Extraire l'extension correctement + $originalName = basename($chapterCoverFile); + if (preg_match('/\.([^.]+)$/', $originalName, $matches)) { + $extension = $matches[1]; + $newChapterCoverName = $chapter['id'] . '-cover.' . $extension; + $newChapterCoverPath = 'assets/images/chapters/' . $storyId . '/covers/' . $newChapterCoverName; + $targetPath = $baseDir . $newChapterCoverPath; + + if (!copy($chapterCoverFile, $targetPath)) { + throw new Exception("Impossible de copier la couverture du chapitre"); + } + chmod($targetPath, 0777); + $chapter['cover'] = $newChapterCoverPath; + } + } + } + + // Gérer le contenu et les images intégrées if (!empty($chapter['content'])) { $content = $chapter['content']; if (is_string($content) && isJson($content)) { @@ -96,9 +136,18 @@ try { if (is_array($op['insert']) && isset($op['insert']['image'])) { $imgPath = $storyDir . '/' . $op['insert']['image']; if (file_exists($imgPath)) { - $newImgName = 'image_' . uniqid() . '.' . pathinfo($imgPath, PATHINFO_EXTENSION); - copy($imgPath, $chaptersImgDir . '/' . $newImgName); - $op['insert']['image'] = 'assets/images/chapters/' . $storyId . '/' . $newImgName; + $originalName = basename($imgPath); + if (preg_match('/\.([^.]+)$/', $originalName, $matches)) { + $extension = $matches[1]; + $newImgName = 'image_' . uniqid() . '.' . $extension; + $targetPath = $chaptersImgDir . '/' . $newImgName; + + if (!copy($imgPath, $targetPath)) { + throw new Exception("Impossible de copier l'image intégrée"); + } + chmod($targetPath, 0777); + $op['insert']['image'] = 'assets/images/chapters/' . $storyId . '/' . $newImgName; + } } } } @@ -108,10 +157,14 @@ try { } // Sauvegarder le roman - Stories::save($story); + if (!Stories::save($story)) { + throw new Exception("Erreur lors de la sauvegarde de {$story['title']}"); + } $importedCount++; } catch (Exception $e) { + error_log("Erreur d'import pour {$storyInfo['title']}: " . $e->getMessage()); + error_log("Trace complète: " . print_r($e->getTraceAsString(), true)); $errors[] = "Erreur pour {$storyInfo['title']}: " . $e->getMessage(); } } @@ -140,6 +193,9 @@ try { exit; } catch (Exception $e) { + error_log("Erreur générale d'import: " . $e->getMessage()); + error_log("Trace complète: " . print_r($e->getTraceAsString(), true)); + if (isset($tempDir) && file_exists($tempDir)) { deleteDir($tempDir); }