1
0
mirror of https://github.com/24eme/signaturepdf.git synced 2023-08-25 09:33:08 +02:00

Le pdf est stocké en local dans le CacheAPI et il transite mais n'est plus conservé

sur le serveur
This commit is contained in:
Vincent LAURENT 2021-11-12 01:49:23 +01:00
parent 19025fa07f
commit 82b1dcc913
4 changed files with 97 additions and 42 deletions

40
app.php
View File

@ -8,11 +8,8 @@ if(getenv("DEBUG")) {
$f3->set('ROOT', __DIR__);
$f3->set('UI', $f3->get('ROOT')."/templates/");
$f3->set('UPLOADS', $f3->get('ROOT').'/data/');
$f3->set('UPLOADS', sys_get_temp_dir()."/");
if(!is_dir($f3->get('UPLOADS'))) {
mkdir($f3->get('UPLOADS'));
}
$f3->route('GET /',
function($f3) {
$f3->set('key', hash('md5', uniqid().rand()));
@ -51,6 +48,12 @@ $f3->route('POST /upload',
$f3->error(403);
}
if($f3->get('DEBUG')) {
return;
}
unlink($filePdf);
return $f3->reroute('/'.$key);
}
);
@ -103,12 +106,37 @@ $f3->route('POST /image2svg',
if($f3->get('DEBUG')) {
return;
}
array_map('unlink', glob($imageFile."*"));
}
);
$f3->route('POST /@key/save',
function($f3) {
$key = $f3->get('PARAMS.key');
$files = Web::instance()->receive(function($file,$formFieldName){
if(strpos(Web::instance()->mime($file['tmp_name'], true), 'application/pdf') !== 0) {
return false;
}
return true;
}, true, function($fileBaseName, $formFieldName) use ($key) {
return $key.".pdf";
});
$pdfFile = null;
foreach($files as $file => $valid) {
if(!$valid) {
continue;
}
$pdfFile = $file;
}
if(!$pdfFile) {
$f3->error(403);
}
$svgData = $_POST['svg'];
$filename = null;
if(isset($_POST['filename']) && $_POST['filename']) {
@ -130,9 +158,7 @@ $f3->route('POST /@key/save',
if($f3->get('DEBUG')) {
return;
}
array_map('unlink', glob($f3->get('UPLOADS').$key."_*.svg"));
unlink($f3->get('UPLOADS').$key.'.svg.pdf');
unlink($f3->get('UPLOADS').$key.'_signe.pdf');
array_map('unlink', glob($f3->get('UPLOADS').$key."*"));
}
);

View File

@ -1,3 +1,16 @@
var canvasEditions = [];
(async function () {
const cache = await caches.open('pdf');
var responsePdf = await cache.match(url);
var pdfBlob = await responsePdf.blob();
url = await URL.createObjectURL(pdfBlob);
var dataTransfer = new DataTransfer();
dataTransfer.items.add(new File([pdfBlob], filename, {
type: 'application/pdf'
}));
document.getElementById('input_pdf').files = dataTransfer.files;
// Loaded via <script> tag, create shortcut to access PDF.js exports.
var pdfjsLib = window['pdfjs-dist/build/pdf'];
@ -5,8 +18,6 @@ var pdfjsLib = window['pdfjs-dist/build/pdf'];
// The workerSrc property shall be specified.
pdfjsLib.GlobalWorkerOptions.workerSrc = '/vendor/pdf.worker.js?legacy';
var canvasEditions = [];
// Asynchronous download of PDF
var loadingTask = pdfjsLib.getDocument(url);
loadingTask.promise.then(function(pdf) {
@ -130,6 +141,7 @@ loadingTask.promise.then(function(pdf) {
input_selected = null;
}
if(input_selected) {
document.body.style.setProperty('cursor', 'copy');
} else {

View File

@ -33,41 +33,57 @@
</footer>
<script>
var key = "<?php echo $key ?>";
var pdfHistory = {};
if(localStorage.getItem('pdfHistory')) {
pdfHistory = JSON.parse(localStorage.getItem('pdfHistory'));
}
document.getElementById('input_pdf_upload').addEventListener('change', function(event) {
pdfHistory[key] = { filename: document.getElementById('input_pdf_upload').files[0].name }
localStorage.setItem('pdfHistory', JSON.stringify(pdfHistory));
document.getElementById('form_pdf_upload').submit();
});
async function uploadFromUrl(url) {
var response = await fetch(url);
if(response.status != 200) {
return;
(async function () {
const cache = await caches.open('pdf');
var key = "<?php echo $key ?>";
var pdfHistory = {};
function dataURLtoBlob(dataurl) {
var arr = dataurl.split(','), mime = arr[0].match(/:(.*?);/)[1],
bstr = atob(arr[1]), n = bstr.length, u8arr = new Uint8Array(n);
while(n--){
u8arr[n] = bstr.charCodeAt(n);
}
return new Blob([u8arr], {type:mime});
}
var pdfBlob = await response.blob();
if(pdfBlob.type != 'application/pdf' && pdfBlob.type != 'application/octet-stream') {
return;
if(localStorage.getItem('pdfHistory')) {
pdfHistory = JSON.parse(localStorage.getItem('pdfHistory'));
}
var dataTransfer = new DataTransfer();
var filename = url.replace(/^.*\//, '');
dataTransfer.items.add(new File([pdfBlob], filename, {
type: 'application/pdf'
}));
document.getElementById('input_pdf_upload').files = dataTransfer.files;
document.getElementById('input_pdf_upload').dispatchEvent(new Event("change"));
document.getElementById('input_pdf_upload').addEventListener('change', async function(event) {
var response = new Response(document.getElementById('input_pdf_upload').files[0], { "status" : 200, "statusText" : "OK" });
await cache.put('/'+key+'/pdf', response);
console.log(await (await cache.match('/'+key+'/pdf')).blob());
history.replaceState({}, "Signature de PDF", "/");
}
if(window.location.hash) {
uploadFromUrl(window.location.hash.replace(/^\#/, ''));
}
window.addEventListener('hashchange', function() {
uploadFromUrl(window.location.hash.replace(/^\#/, ''));
})
pdfHistory[key] = { filename: document.getElementById('input_pdf_upload').files[0].name }
localStorage.setItem('pdfHistory', JSON.stringify(pdfHistory));
document.getElementById('form_pdf_upload').submit();
});
async function uploadFromUrl(url) {
var response = await fetch(url);
if(response.status != 200) {
return;
}
var pdfBlob = await response.blob();
if(pdfBlob.type != 'application/pdf' && pdfBlob.type != 'application/octet-stream') {
return;
}
var dataTransfer = new DataTransfer();
var filename = url.replace(/^.*\//, '');
dataTransfer.items.add(new File([pdfBlob], filename, {
type: 'application/pdf'
}));
document.getElementById('input_pdf_upload').files = dataTransfer.files;
document.getElementById('input_pdf_upload').dispatchEvent(new Event("change"));
history.replaceState({}, "Signature de PDF", "/");
}
if(window.location.hash) {
uploadFromUrl(window.location.hash.replace(/^\#/, ''));
}
window.addEventListener('hashchange', function() {
uploadFromUrl(window.location.hash.replace(/^\#/, ''));
})
})();
</script>
</body>
</html>

View File

@ -68,8 +68,9 @@
<button type="button" id="btn-add-svg" class="btn btn-sm btn-light" data-bs-toggle="modal" data-bs-target="#modalAddSvg"><i class="bi bi-plus-circle"></i> Ajouter un élément</button>
</div>
<form class="position-absolute bottom-0 pb-2 ps-0 pe-4 w-100 d-none d-sm-none d-md-block" id="form_pdf" action="/<?php echo $key ?>/save" method="post">
<form class="position-absolute bottom-0 pb-2 ps-0 pe-4 w-100 d-none d-sm-none d-md-block" id="form_pdf" action="/<?php echo $key ?>/save" method="post" enctype="multipart/form-data">
<input id="input_filename" type="hidden" name="filename" value="" />
<input id="input_pdf" name="pdf" type="file" class="d-none" />
<div class="d-grid gap-2 mt-2">
<button class="btn btn-primary" disabled="disabled" type="submit" id="save"><i class="bi bi-download"></i> Télécharger le PDF Signé</button>
</div>