l'import/export est compatible avec les images d'en-tête des chapitres
This commit is contained in:
parent
3660e796ee
commit
b7e7c54ecf
@ -37,18 +37,32 @@ try {
|
|||||||
$storyDir = $tempDir . '/' . $storyId;
|
$storyDir = $tempDir . '/' . $storyId;
|
||||||
mkdir($storyDir);
|
mkdir($storyDir);
|
||||||
mkdir($storyDir . '/images');
|
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'])) {
|
if (!empty($story['cover'])) {
|
||||||
$coverPath = __DIR__ . '/../../' . $story['cover'];
|
$coverPath = __DIR__ . '/../../' . $story['cover'];
|
||||||
if (file_exists($coverPath)) {
|
if (file_exists($coverPath)) {
|
||||||
copy($coverPath, $storyDir . '/cover' . pathinfo($coverPath, PATHINFO_EXTENSION));
|
$extension = '.' . pathinfo($coverPath, PATHINFO_EXTENSION);
|
||||||
$story['cover'] = 'cover' . pathinfo($coverPath, PATHINFO_EXTENSION);
|
copy($coverPath, $storyDir . '/cover' . $extension);
|
||||||
|
$story['cover'] = 'cover' . $extension;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Extraire et copier les images des chapitres
|
// Extraire et copier les images des chapitres
|
||||||
foreach ($story['chapters'] as &$chapter) {
|
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'])) {
|
if (!empty($chapter['content'])) {
|
||||||
$content = $chapter['content'];
|
$content = $chapter['content'];
|
||||||
if (is_string($content) && isJson($content)) {
|
if (is_string($content) && isJson($content)) {
|
||||||
@ -62,7 +76,8 @@ try {
|
|||||||
$imgUrl = $op['insert']['image'];
|
$imgUrl = $op['insert']['image'];
|
||||||
$imgPath = __DIR__ . '/../../' . preg_replace('/^(?:\.\.\/)+/', '', $imgUrl);
|
$imgPath = __DIR__ . '/../../' . preg_replace('/^(?:\.\.\/)+/', '', $imgUrl);
|
||||||
if (file_exists($imgPath)) {
|
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);
|
copy($imgPath, $storyDir . '/images/' . $newImgName);
|
||||||
$op['insert']['image'] = 'images/' . $newImgName;
|
$op['insert']['image'] = 'images/' . $newImgName;
|
||||||
}
|
}
|
||||||
|
@ -66,25 +66,65 @@ try {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Créer les dossiers nécessaires
|
// Créer les dossiers nécessaires avec les permissions correctes
|
||||||
$coverDir = __DIR__ . '/../../assets/images/covers/';
|
$baseDir = __DIR__ . '/../../';
|
||||||
$chaptersImgDir = __DIR__ . '/../../assets/images/chapters/' . $storyId;
|
$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);
|
foreach ([$coverDir, $chaptersImgDir, $chaptersCoverDir] as $dir) {
|
||||||
if (!file_exists($chaptersImgDir)) mkdir($chaptersImgDir, 0755, true);
|
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'])) {
|
if (!empty($story['cover'])) {
|
||||||
$coverFile = $storyDir . '/' . $story['cover'];
|
$coverFile = $storyDir . '/' . $story['cover'];
|
||||||
if (file_exists($coverFile)) {
|
if (file_exists($coverFile)) {
|
||||||
$newCoverPath = 'assets/images/covers/' . $storyId . '.' . pathinfo($coverFile, PATHINFO_EXTENSION);
|
// Extraire l'extension correctement
|
||||||
copy($coverFile, __DIR__ . '/../../' . $newCoverPath);
|
$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;
|
$story['cover'] = $newCoverPath;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Gérer les images des chapitres
|
// Gérer les images des chapitres
|
||||||
foreach ($story['chapters'] as &$chapter) {
|
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'])) {
|
if (!empty($chapter['content'])) {
|
||||||
$content = $chapter['content'];
|
$content = $chapter['content'];
|
||||||
if (is_string($content) && isJson($content)) {
|
if (is_string($content) && isJson($content)) {
|
||||||
@ -96,22 +136,35 @@ try {
|
|||||||
if (is_array($op['insert']) && isset($op['insert']['image'])) {
|
if (is_array($op['insert']) && isset($op['insert']['image'])) {
|
||||||
$imgPath = $storyDir . '/' . $op['insert']['image'];
|
$imgPath = $storyDir . '/' . $op['insert']['image'];
|
||||||
if (file_exists($imgPath)) {
|
if (file_exists($imgPath)) {
|
||||||
$newImgName = 'image_' . uniqid() . '.' . pathinfo($imgPath, PATHINFO_EXTENSION);
|
$originalName = basename($imgPath);
|
||||||
copy($imgPath, $chaptersImgDir . '/' . $newImgName);
|
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;
|
$op['insert']['image'] = 'assets/images/chapters/' . $storyId . '/' . $newImgName;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
$chapter['content'] = json_encode($content);
|
$chapter['content'] = json_encode($content);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Sauvegarder le roman
|
// Sauvegarder le roman
|
||||||
Stories::save($story);
|
if (!Stories::save($story)) {
|
||||||
|
throw new Exception("Erreur lors de la sauvegarde de {$story['title']}");
|
||||||
|
}
|
||||||
$importedCount++;
|
$importedCount++;
|
||||||
|
|
||||||
} catch (Exception $e) {
|
} 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();
|
$errors[] = "Erreur pour {$storyInfo['title']}: " . $e->getMessage();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -140,6 +193,9 @@ try {
|
|||||||
exit;
|
exit;
|
||||||
|
|
||||||
} catch (Exception $e) {
|
} 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)) {
|
if (isset($tempDir) && file_exists($tempDir)) {
|
||||||
deleteDir($tempDir);
|
deleteDir($tempDir);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user