No longer use php to generate the key of a pdf file and use the filename in javascript instead

This commit is contained in:
Vincent LAURENT 2022-02-06 01:09:41 +01:00
parent 97e994a1be
commit d2f773a91d
3 changed files with 8 additions and 24 deletions

View File

@ -40,16 +40,14 @@ function convertPHPSizeToBytes($sSize)
$f3->route('GET /', $f3->route('GET /',
function($f3) { function($f3) {
$f3->set('key', hash('md5', uniqid().rand()));
$f3->set('maxSize', min(array(convertPHPSizeToBytes(ini_get('post_max_size')), convertPHPSizeToBytes(ini_get('upload_max_filesize'))))); $f3->set('maxSize', min(array(convertPHPSizeToBytes(ini_get('post_max_size')), convertPHPSizeToBytes(ini_get('upload_max_filesize')))));
$f3->set('maxPage', ini_get('max_file_uploads') - 1); $f3->set('maxPage', ini_get('max_file_uploads') - 1);
echo View::instance()->render('index.html.php'); echo View::instance()->render('index.html.php');
} }
); );
$f3->route('GET /@key', $f3->route('GET /sign',
function($f3) { function($f3) {
$f3->set('key', $f3->get('PARAMS.key'));
$f3->set('maxPage', ini_get('max_file_uploads') - 1); $f3->set('maxPage', ini_get('max_file_uploads') - 1);
echo View::instance()->render('pdf.html.php'); echo View::instance()->render('pdf.html.php');

View File

@ -28,26 +28,19 @@
<script> <script>
(async function () { (async function () {
const cache = await caches.open('pdf'); const cache = await caches.open('pdf');
var key = "<?php echo $key ?>";
var urlPdf = '/'+key+'/pdf';
var urlSignature = '/'+key;
var pdfHistory = {};
var maxSize = <?php echo $maxSize ?>; var maxSize = <?php echo $maxSize ?>;
if(localStorage.getItem('pdfHistory')) {
pdfHistory = JSON.parse(localStorage.getItem('pdfHistory'));
}
document.getElementById('input_pdf_upload').addEventListener('change', async function(event) { document.getElementById('input_pdf_upload').addEventListener('change', async function(event) {
if(document.getElementById('input_pdf_upload').files[0].size > maxSize) { if(document.getElementById('input_pdf_upload').files[0].size > maxSize) {
alert("Le PDF ne doit pas dépasser <?php echo round($maxSize / 1024 / 1024) ?> Mo"); alert("Le PDF ne doit pas dépasser <?php echo round($maxSize / 1024 / 1024) ?> Mo");
document.getElementById('input_pdf_upload').value = ""; document.getElementById('input_pdf_upload').value = "";
return; return;
} }
let filename = document.getElementById('input_pdf_upload').files[0].name;
var response = new Response(document.getElementById('input_pdf_upload').files[0], { "status" : 200, "statusText" : "OK" }); var response = new Response(document.getElementById('input_pdf_upload').files[0], { "status" : 200, "statusText" : "OK" });
let urlPdf = '/pdf/#'+filename;
await cache.put(urlPdf, response); await cache.put(urlPdf, response);
pdfHistory[key] = { filename: document.getElementById('input_pdf_upload').files[0].name } document.location = '/sign/#'+filename;
localStorage.setItem('pdfHistory', JSON.stringify(pdfHistory));
document.location = urlSignature;
}); });
async function uploadFromUrl(url) { async function uploadFromUrl(url) {
var response = await fetch(url); var response = await fetch(url);
@ -69,7 +62,7 @@
history.replaceState({}, "Signature de PDF", "/"); history.replaceState({}, "Signature de PDF", "/");
} }
if(window.location.hash) { if(window.location.hash && window.location.hash.match(/^\#http/)) {
uploadFromUrl(window.location.hash.replace(/^\#/, '')); uploadFromUrl(window.location.hash.replace(/^\#/, ''));
} }
window.addEventListener('hashchange', function() { window.addEventListener('hashchange', function() {

View File

@ -130,16 +130,9 @@
<script src="/vendor/signature_pad.umd.min.js?3.0.0-beta.3"></script> <script src="/vendor/signature_pad.umd.min.js?3.0.0-beta.3"></script>
<script src="/vendor/opentype.min.js?1.3.3"></script> <script src="/vendor/opentype.min.js?1.3.3"></script>
<script> <script>
var url = '/<?php echo $key ?>/pdf'; var url = '/pdf/'+window.location.hash;
var pdfHistory = {};
var maxPage = <?php echo $maxPage ?>; var maxPage = <?php echo $maxPage ?>;
var filename = null; var filename = window.location.hash.replace(/^\#/, '');
if(localStorage.getItem('pdfHistory')) {
pdfHistory = JSON.parse(localStorage.getItem('pdfHistory'));
}
if(pdfHistory["<?php echo $key ?>"]) {
filename = pdfHistory["<?php echo $key ?>"].filename;
}
</script> </script>
<script src="/js/app.js"></script> <script src="/js/app.js"></script>
</body> </body>