From ba930d034c33044d22fda3d58b4d192a4791eb28 Mon Sep 17 00:00:00 2001 From: Tangui Morlier Date: Thu, 22 Aug 2024 18:03:16 +0200 Subject: [PATCH] Allow to edit the metadata of a pdf stored on the server --- app.php | 37 +++++++++++++++++++++++++++++++++++++ config/config.ini.example | 3 +++ public/js/metadata.js | 22 +++++++++++++++++++--- 3 files changed, 59 insertions(+), 3 deletions(-) diff --git a/app.php b/app.php index ab303d9..f4793a1 100644 --- a/app.php +++ b/app.php @@ -443,6 +443,43 @@ $f3->route ('POST /compress', } ); +$f3->route('GET /api/file/get', function($f3) { + $localRootFolder = $f3->get('PDF_LOCAL_PATH'); + if (!$localRootFolder) { + $f3->error(403); + } + $pdf_path = $localRootFolder . '/' . $f3->get('GET.path'); + $pdf_filename = basename($pdf_path); + if (!preg_match('/.pdf$/', $pdf_path)) { + $f3->error(403); + } + if (!file_exists($pdf_path)) { + $f3->error(403); + } + header('Content-type: application/pdf'); + header("Content-Disposition: attachment; filename=$pdf_filename"); + echo file_get_contents($pdf_path); +}); + +$f3->route('PUT /api/file/save', function($f3) { + $localRootFolder = $f3->get('PDF_LOCAL_PATH'); + if (!$localRootFolder) { + $f3->error(403); + } + $pdf_path = $localRootFolder . '/' . $f3->get('GET.path'); + $pdf_filename = basename($pdf_path); + if (!preg_match('/.pdf$/', $pdf_path)) { + $f3->error(403); + } + if (!file_exists($pdf_path)) { + $f3->error(403); + } + file_put_contents($pdf_path, $f3->get('BODY')); + +}); + + + function getCommit() { if(!file_exists(__DIR__.'/.git/HEAD')) { diff --git a/config/config.ini.example b/config/config.ini.example index dff71c6..075c455 100644 --- a/config/config.ini.example +++ b/config/config.ini.example @@ -26,3 +26,6 @@ PDF_STORAGE_PATH=/path/to/folder ; Authorize these IP to use debug mode (separate IP adresses with space ' ') ;ADMIN_AUTHORIZED_IP= + +; Enable the edition of local server pdf metadata +; PDF_LOCAL_PATH=/path/to/pdf/metadata/edition diff --git a/public/js/metadata.js b/public/js/metadata.js index 990ca40..8374fc2 100644 --- a/public/js/metadata.js +++ b/public/js/metadata.js @@ -179,6 +179,15 @@ const save = async function () { }); const newPDF = new Blob([await pdf.save()], {type: "application/pdf"}); + + if(window.location.hash && window.location.hash.match(/^\#local/)) { + let apiUrl = window.location.origin + "/api/file/save?path=" + window.location.hash.replace(/^\#local:/, ''); + fetch(apiUrl, { + method: 'PUT', + body: newPDF, + }); + return ; + } DL(newPDF, filename) } @@ -220,7 +229,7 @@ async function getPDFBlobFromCache(cacheUrl) { return pdfBlob; } -async function uploadFromUrl(url) { +async function uploadFromUrl(url, local = null) { history.replaceState({}, '', '/metadata'); var response = await fetch(url); if(response.status != 200) { @@ -232,8 +241,11 @@ async function uploadFromUrl(url) { return; } let dataTransfer = new DataTransfer(); - let filename = url.replace(/^.*\//, ''); - dataTransfer.items.add(new File([pdfBlob], filename, { + let file_id = url.replace(/^.*\//, ''); + if (local) { + file_id = local; + } + dataTransfer.items.add(new File([pdfBlob], file_id, { type: 'application/pdf' })); document.getElementById('input_pdf_upload').files = dataTransfer.files; @@ -289,6 +301,10 @@ var pageMetadata = async function(url) { let hashUrl = window.location.hash.replace(/^\#/, ''); pageUpload(); uploadFromUrl(hashUrl); + } else if(window.location.hash && window.location.hash.match(/^\#local/)) { + let hashUrl = window.location.origin + "/api/file/get?path=" + window.location.hash.replace(/^\#local:/, ''); + pageUpload(); + uploadFromUrl(hashUrl, window.location.hash.replace(/^\#/, '')); } else if(window.location.hash) { pageMetadata('/pdf/'+window.location.hash.replace(/^\#/, '')); } else {