diff --git a/app.php b/app.php index 2522519..9a74e09 100644 --- a/app.php +++ b/app.php @@ -21,6 +21,7 @@ $f3->route('GET /', ); $f3->route('POST /upload', function($f3) { + $fileName = null; $files = Web::instance()->receive(function($file,$formFieldName){ if(Web::instance()->mime($file['tmp_name'], true) != 'application/pdf') { @@ -31,8 +32,8 @@ $f3->route('POST /upload', return false; } return true; - }, true, function($fileBaseName, $formFieldName) { - + }, true, function($fileBaseName, $formFieldName) use (&$fileName) { + $fileName = $fileBaseName; return substr(hash('sha256', $fileBaseName.uniqid().mt_rand()), 0, 24).".pdf"; }); @@ -48,12 +49,20 @@ $f3->route('POST /upload', $f3->error(403); } + if($fileName) { + $f3->set('SESSION.fileName', $fileName); + } + return $f3->reroute('/'.$key); } ); $f3->route('GET /@key', function($f3) { $f3->set('key', $f3->get('PARAMS.key')); + if($f3->get('SESSION.fileName')) { + $f3->set('fileName', $f3->get('SESSION.fileName')); + $f3->clear('SESSION.fileName'); + } echo View::instance()->render('pdf.html.php'); } ); @@ -110,6 +119,10 @@ $f3->route('POST /@key/save', function($f3) { $key = $f3->get('PARAMS.key'); $svgData = $_POST['svg']; + $filename = null; + if(isset($_POST['filename']) && $_POST['filename']) { + $filename = str_replace(".pdf", "_signe.pdf", $_POST['filename']); + } $svgFiles = ""; foreach($svgData as $index => $svgItem) { @@ -121,7 +134,7 @@ $f3->route('POST /@key/save', shell_exec(sprintf("rsvg-convert -f pdf -o %s %s", $f3->get('UPLOADS').$key.'.svg.pdf', $svgFiles)); shell_exec(sprintf("pdftk %s multibackground %s output %s", $f3->get('UPLOADS').$key.'.svg.pdf', $f3->get('UPLOADS').$key.'.pdf', $f3->get('UPLOADS').$key.'_signe.pdf')); - Web::instance()->send($f3->get('UPLOADS').$key.'_signe.pdf'); + Web::instance()->send($f3->get('UPLOADS').$key.'_signe.pdf', null, 0, TRUE, $filename); if($f3->get('DEBUG')) { return; diff --git a/public/js/app.js b/public/js/app.js index 56fcd26..6b84118 100644 --- a/public/js/app.js +++ b/public/js/app.js @@ -20,11 +20,16 @@ loadingTask.promise.then(function(pdf) { var pdfPages = []; var svgCollections = []; var resizeTimeout; + var pdfHistory = {}; var currentScale = 1.5; var windowWidth = window.innerWidth; var menu = document.getElementById('offcanvasTop') var menuOffcanvas = new bootstrap.Offcanvas(menu) + if(localStorage.getItem('pdfHistory')) { + pdfHistory = JSON.parse(localStorage.getItem('pdfHistory')); + } + var is_mobile = function() { return !(window.getComputedStyle(document.getElementById('is_mobile')).display === "none"); } @@ -438,6 +443,10 @@ loadingTask.promise.then(function(pdf) { xhr.send( formData ); } + if(filename) { + document.getElementById('input_filename').value = filename; + } + document.getElementById('save').addEventListener('click', function(event) { canvasEditions.forEach(function(canvasEdition, index) { document.getElementById('data-svg-'+index).value = canvasEdition.toSVG(); diff --git a/templates/pdf.html.php b/templates/pdf.html.php index 9cbee26..2199351 100644 --- a/templates/pdf.html.php +++ b/templates/pdf.html.php @@ -69,6 +69,7 @@
+
@@ -144,6 +145,21 @@ diff --git a/tests/signature.test.js b/tests/signature.test.js index caf4f91..b011da9 100644 --- a/tests/signature.test.js +++ b/tests/signature.test.js @@ -130,13 +130,13 @@ describe("Signature d'un pdf", () => { await page._client.send('Page.setDownloadBehavior', {behavior: 'allow', downloadPath: './tests/downloads'}); await page.click("#save"); await page.waitForTimeout(500); - await expect(require('fs').existsSync('./tests/downloads/'+hash+'_signe.pdf')).toBe(true); + await expect(require('fs').existsSync('./tests/downloads/document_signe.pdf')).toBe(true); }); afterAll(async () => { if(process.env.DEBUG) { return; } - await require('fs').unlinkSync('./tests/downloads/'+hash+'_signe.pdf'); + await require('fs').unlinkSync('./tests/downloads/document_signe.pdf'); await server.kill(); await browser.close(); });