1
0
Fork 0
mirror of https://github.com/24eme/signaturepdf synced 2026-03-14 13:55:44 +01:00

Allow to edit the metadata of a pdf stored on the server

This commit is contained in:
Tangui Morlier 2024-08-22 18:03:16 +02:00
commit ba930d034c
3 changed files with 59 additions and 3 deletions

37
app.php
View file

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

View file

@ -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

View file

@ -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 {