From 7cc18fc08d4178245652d285e8161e94cdfb75b6 Mon Sep 17 00:00:00 2001 From: Tangui Morlier Date: Wed, 24 Sep 2025 12:00:59 +0200 Subject: [PATCH] =?UTF-8?q?Possibilit=C3=A9=20d'initier=20un=20metadata=20?= =?UTF-8?q?=C3=A0=20partir=20d'une=20image=20qui=20se=20trouve=20dans=20l'?= =?UTF-8?q?espace=20local?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app.php | 25 +++++++++++++++++++++---- public/js/common.js | 12 +++++++++--- 2 files changed, 30 insertions(+), 7 deletions(-) diff --git a/app.php b/app.php index 0a42dd6..79355c0 100644 --- a/app.php +++ b/app.php @@ -501,13 +501,27 @@ $f3->route('GET /api/file/get', function($f3) { } $pdf_path = $localRootFolder . '/' . $f3->get('GET.path'); $pdf_filename = basename($pdf_path); - if (!preg_match('/.pdf$/', $pdf_path)) { + $extension = 'pdf'; + if (preg_match('/.(pdf|png|jpg|jpeg)$/', $pdf_path, $m)) { + $extension = $m[1]; + }else{ $f3->error(403); } if (!file_exists($pdf_path)) { $f3->error(403); } - header('Content-type: application/pdf'); + switch ($extension) { + case 'jpg': + case 'jpeg': + header('Content-type: image/jpg'); + break; + case 'png': + header('Content-type: image/png'); + break; + default: + header('Content-type: application/pdf'); + break; + } header("Content-Disposition: attachment; filename=$pdf_filename"); echo file_get_contents($pdf_path); }); @@ -519,13 +533,16 @@ $f3->route('PUT /api/file/save', function($f3) { } $pdf_path = $localRootFolder . '/' . $f3->get('GET.path'); $pdf_filename = basename($pdf_path); - if (!preg_match('/.pdf$/', $pdf_path)) { + if (preg_match('/(.*).(pdf|png|jpg|jpeg)$/', $pdf_path, $m)) { + $basefile = $m[1]; + $extension = $m[2]; + }else{ $f3->error(403); } if (!file_exists($pdf_path)) { $f3->error(403); } - file_put_contents($pdf_path, $f3->get('BODY')); + file_put_contents($basefile.'.pdf', $f3->get('BODY')); }); diff --git a/public/js/common.js b/public/js/common.js index 0097f09..ce7bd10 100644 --- a/public/js/common.js +++ b/public/js/common.js @@ -79,8 +79,14 @@ async function loadFileFromUrl(url, pageUrl, local = null) { if(response.headers.has('content-disposition') && response.headers.get('Content-Disposition').match(/attachment; filename="/)) { file_id = response.headers.get('Content-Disposition').replace(/^[^"]*"/, "").replace(/"[^"]*$/, "").replace(/_signe-[0-9]+\x.pdf/, '.pdf'); } - - if(pdfBlob.type != 'application/pdf' && pdfBlob.type != 'application/octet-stream') { + filetype = 'application/pdf'; + if(pdfBlob.type == 'application/pdf' || pdfBlob.type == 'application/octet-stream') { + filetype = 'application/pdf'; + }else if (pdfBlob.type == 'image/jpg' || pdfBlob.type == 'image/jpeg') { + filetype = 'image/jpg'; + }else if (pdfBlob.type == 'image/png') { + filetype = 'image/png'; + }else{ return; } let dataTransfer = new DataTransfer(); @@ -88,7 +94,7 @@ async function loadFileFromUrl(url, pageUrl, local = null) { file_id = local; } dataTransfer.items.add(new File([pdfBlob], file_id, { - type: 'application/pdf' + type: filetype })); document.getElementById('input_pdf_upload').files = dataTransfer.files; endLoading()