From 44e38ae4e56a2658e57d3cd6d7ab4ecc7ced317d Mon Sep 17 00:00:00 2001 From: Vincent LAURENT Date: Mon, 1 Nov 2021 02:28:18 +0100 Subject: [PATCH] =?UTF-8?q?Conservation=20du=20nom=20original=20du=20fichi?= =?UTF-8?q?er=20du=20pdf=20dans=20le=20localstorage=20de=20l'utilisateur?= =?UTF-8?q?=20pour=20pouvoir=20le=20restituer=20lors=20du=20t=C3=A9l=C3=A9?= =?UTF-8?q?chargement=20du=20pdf=20sign=C3=A9=20sans=20qu'il=20soit=20stoc?= =?UTF-8?q?k=C3=A9=20sur=20le=20serveur?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app.php | 19 ++++++++++++++++--- public/js/app.js | 9 +++++++++ templates/pdf.html.php | 16 ++++++++++++++++ tests/signature.test.js | 4 ++-- 4 files changed, 43 insertions(+), 5 deletions(-) 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(); });