1
0
Fork 0
mirror of https://github.com/24eme/signaturepdf synced 2024-06-04 15:02:29 +02:00

Plus besoin du premier upload et vérification de la taille maximum

d'upload en js sur le premier écran pour évité d'être bloqué à l'upload
du php pour la signature
This commit is contained in:
Vincent LAURENT 2021-11-12 02:24:28 +01:00
parent 0b65daed8b
commit 07acab564b
2 changed files with 47 additions and 64 deletions

77
app.php
View file

@ -10,53 +10,42 @@ $f3->set('ROOT', __DIR__);
$f3->set('UI', $f3->get('ROOT')."/templates/");
$f3->set('UPLOADS', sys_get_temp_dir()."/");
function convertPHPSizeToBytes($sSize)
{
//
$sSuffix = strtoupper(substr($sSize, -1));
if (!in_array($sSuffix,array('P','T','G','M','K'))){
return (int)$sSize;
}
$iValue = substr($sSize, 0, -1);
switch ($sSuffix) {
case 'P':
$iValue *= 1024;
// Fallthrough intended
case 'T':
$iValue *= 1024;
// Fallthrough intended
case 'G':
$iValue *= 1024;
// Fallthrough intended
case 'M':
$iValue *= 1024;
// Fallthrough intended
case 'K':
$iValue *= 1024;
break;
}
return (int)$iValue;
}
$f3->route('GET /',
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')))));
echo View::instance()->render('index.html.php');
}
);
$f3->route('POST /upload',
function($f3) {
$key = $f3->get('POST.key');
if(!$key || !preg_match('/^[0-9a-z]+$/', $key)) {
$f3->error(403);
}
$files = Web::instance()->receive(function($file,$formFieldName){
if(Web::instance()->mime($file['tmp_name'], true) != 'application/pdf') {
return false;
}
return true;
}, true, function($fileBaseName, $formFieldName) use ($key) {
return $key.".pdf";
});
$filePdf = null;
foreach($files as $file => $valid) {
if(!$valid) {
continue;
}
$filePdf = $file;
}
if(!$filePdf) {
$f3->error(403);
}
if($f3->get('DEBUG')) {
return;
}
unlink($filePdf);
return $f3->reroute('/'.$key);
}
);
$f3->route('GET /@key',
function($f3) {
$f3->set('key', $f3->get('PARAMS.key'));
@ -64,12 +53,6 @@ $f3->route('GET /@key',
echo View::instance()->render('pdf.html.php');
}
);
$f3->route('GET /@key/pdf',
function($f3) {
$key = $f3->get('PARAMS.key');
Web::instance()->send($f3->get('UPLOADS').$key.'.pdf');
}
);
$f3->route('POST /image2svg',
function($f3) {
$files = Web::instance()->receive(function($file,$formFieldName){

View file

@ -13,19 +13,12 @@
<div class="px-4 py-5 my-5 text-center">
<h1 class="display-5 fw-bold"><i class="bi bi-vector-pen"></i> Signer un PDF</h1>
<div class="col-lg-3 mx-auto">
<form id="form_pdf_upload" action="/upload" method="POST" class="row g-3" enctype="multipart/form-data">
<input id="input_key" name="key" type="hidden" value="<?php echo $key ?>">
<div class="col-12">
<label for="input_pdf_upload" class="form-label">Choisir un PDF</label>
<input id="input_pdf_upload" class="form-control form-control-lg" name="pdf" type="file" accept=".pdf,application/pdf">
<a class="btn btn-sm btn-link opacity-75" href="/#https://raw.githubusercontent.com/24eme/signaturepdf/master/tests/files/document.pdf">(Tester avec un PDF de démo)</a>
</div>
<div class="col-12">
<div class="d-grid gap-2">
<button class="btn btn-light" type="submit" id="save"><i class="bi bi-upload"></i> Transmettre le PDF</button>
</div>
</div>
</form>
<div class="col-12">
<label for="input_pdf_upload" class="form-label">Choisir un PDF</label>
<input id="input_pdf_upload" class="form-control form-control-lg" type="file" accept=".pdf,application/pdf">
<p><small class="text-muted">(Le PDF ne doit pas dépasser <?php echo round($maxSize / 1024 / 1024) ?> mo)</small></p>
<a class="btn btn-sm btn-link opacity-75" href="/#https://raw.githubusercontent.com/24eme/signaturepdf/master/tests/files/document.pdf">Tester avec un PDF de démo</a>
</div>
</div>
</div>
<footer class="text-center text-muted mb-2 fixed-bottom">
@ -36,18 +29,25 @@
(async function () {
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 ?>;
if(localStorage.getItem('pdfHistory')) {
pdfHistory = JSON.parse(localStorage.getItem('pdfHistory'));
}
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());
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");
document.getElementById('input_pdf_upload').value = "";
return;
}
var response = new Response(document.getElementById('input_pdf_upload').files[0], { "status" : 200, "statusText" : "OK" });
await cache.put(urlPdf, response);
pdfHistory[key] = { filename: document.getElementById('input_pdf_upload').files[0].name }
localStorage.setItem('pdfHistory', JSON.stringify(pdfHistory));
document.getElementById('form_pdf_upload').submit();
document.location = urlSignature;
});
async function uploadFromUrl(url) {
var response = await fetch(url);