From d4b0f7e4f9057ce39e46662dc169984899ce67cf Mon Sep 17 00:00:00 2001 From: polo Date: Wed, 18 Jun 2025 12:07:37 +0200 Subject: =?UTF-8?q?corrections=20erreur=20t=C3=A9l=C3=A9chargement=20par?= =?UTF-8?q?=20le=20serveur,=20collage=20de=20libreoffice?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- public/index.php | 60 ++++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 48 insertions(+), 12 deletions(-) diff --git a/public/index.php b/public/index.php index d6179b1..c423162 100644 --- a/public/index.php +++ b/public/index.php @@ -18,6 +18,37 @@ function imagickCleanImage(string $image_data, string $local_path): bool // "str } } +function curlDownloadImage(string $url, $maxRetries = 3, $timeout = 10): string|false +{ + $attempt = 0; + $imageData = false; + + while($attempt < $maxRetries) { + $ch = curl_init($url); // instance de CurlHandle + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); + curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); + curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true); + curl_setopt($ch, CURLOPT_USERAGENT, 'TinyMCE-Image-Downloader'); + + $imageData = curl_exec($ch); + $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); + $curlError = curl_error($ch); + + curl_close($ch); + + if($imageData !== false && $httpCode >= 200 && $httpCode < 300){ + return $imageData; + } + + $attempt++; + sleep(1); + } + + return false; // échec après trois tentatives +} + if(isset($_GET['action']) && $_GET['action'] == 'editor_submit'){ // récupération des données $data = file_get_contents('php://input'); @@ -84,7 +115,7 @@ elseif(isset($_GET['action']) && $_GET['action'] == 'upload_image_url'){ $json = json_decode(file_get_contents('php://input'), true); if(isset($json['image_url'])){ - $image_data = file_get_contents($json['image_url']); // téléchargement de l’image par le serveur + $image_data = curlDownloadImage($json['image_url']); // téléchargement de l’image par le serveur avec cURL au lieu de file_get_contents if($image_data === false){ http_response_code(400); @@ -154,25 +185,29 @@ elseif(isset($_GET['action']) && $_GET['action'] == 'upload_image_url'){ document.querySelector(`#submit-${articleId}`).classList.remove('hidden'); }); editor.on('PastePreProcess', function (e){ // déclenchement au collage AVANT insertion dans l'éditeur - //const images = e.node.querySelectorAll('img'); let parser = new DOMParser(); let doc = parser.parseFromString(e.content, 'text/html'); let images = doc.querySelectorAll('img'); let downloads_in_progress = []; - images.forEach(image => { - if(image.src.startsWith('http')){ - // le fait d'avoir lancer un téléchargement - let promise = fetch('index.php?action=upload_image_url', { + images.forEach(img => { + if(img.src.startsWith('file://')){ // détection d'images non insérables + console.warn('Image locale non insérable dans tinymce :', img.src); + img.outerHTML = '
' + + "Image locale non insérée. Pour insérer une image depuis LibreOffice, copiez l'image seule et recoller." + + '
'; + } + else if(img.src.startsWith('http')){ // détection d'images web + let promise = fetch('index.php?action=upload_image_url', { // promesse d'un fichier téléchargeable sur le serveur method: 'POST', headers: { 'Content-Type': 'application/json' }, - body: JSON.stringify({ image_url: image.src }) + body: JSON.stringify({ image_url: img.src }) }) .then(response => response.json()) .then(data => { if(data.location){ - image.src = data.location; // remplacer l'image par celle du serveur + img.src = data.location; // remplacer l'image par celle du serveur } }) .catch(error => { @@ -183,17 +218,18 @@ elseif(isset($_GET['action']) && $_GET['action'] == 'upload_image_url'){ } }); - console.log(downloads_in_progress); - + // une image web ou plus: différer l'insertion dans l'éditeur le temps que le serveur télécharge les images if(downloads_in_progress.length > 0){ - // faire attendre tinymce que le serveur récupère les images avant insertion e.preventDefault(); Promise.all(downloads_in_progress).then(() => { - e.content = doc.body.innerHTML; + e.content = doc.body.innerHTML; // remplacement du HTML dans l'éditeur par la copie modifiée (doc) editor.insertContent(e.content); }); } + else{ + e.content = doc.body.innerHTML; // remplacement du HTML dans l'éditeur par la copie modifiée (doc) + } }); }, // upload d'image -- cgit v1.2.3