Skip to content

Commit

Permalink
Allow to edit the metadata of a pdf stored on the server
Browse files Browse the repository at this point in the history
  • Loading branch information
teymour committed Aug 22, 2024
1 parent 77a7c37 commit ba930d0
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 3 deletions.
37 changes: 37 additions & 0 deletions app.php
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,43 @@ function($f3) {
}
);

$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')) {

Expand Down
3 changes: 3 additions & 0 deletions config/config.ini.example
Original file line number Diff line number Diff line change
Expand Up @@ -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
22 changes: 19 additions & 3 deletions public/js/metadata.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down Expand Up @@ -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) {
Expand All @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit ba930d0

Please sign in to comment.