From 20ae5082896cfeb1c6564b0d62d6754a860c3e2e Mon Sep 17 00:00:00 2001 From: Vincent LAURENT Date: Sun, 31 Oct 2021 22:05:48 +0100 Subject: [PATCH] =?UTF-8?q?Nom=20de=20fichier=20al=C3=A9atoire=20avant=20l?= =?UTF-8?q?e=20move=20de=20l'upload=20et=20=C3=A9galement=20pour=20l'actio?= =?UTF-8?q?n=20d'upload=20d'image=20pour=20conversion=20en=20svg=20Nettoya?= =?UTF-8?q?ge=20des=20fichiers=20non=20utilis=C3=A9s?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app.php | 60 +++++++++++++++++++++++++++++++++------------------------ 1 file changed, 35 insertions(+), 25 deletions(-) diff --git a/app.php b/app.php index 322b5b5..2298346 100644 --- a/app.php +++ b/app.php @@ -19,26 +19,28 @@ $f3->route('GET /', $f3->route('POST /upload', function($f3) { $files = Web::instance()->receive(function($file,$formFieldName){ - if(Web::instance()->mime($file['tmp_name'], true) != 'application/pdf') { - - return false; - } - if($file['size'] > (20 * 1024 * 1024)) { // if bigger than 20 MB - - return false; - } - return true; - }, true); - - $key = null; + if(Web::instance()->mime($file['tmp_name'], true) != 'application/pdf') { + + return false; + } + if($file['size'] > (20 * 1024 * 1024)) { // if bigger than 20 MB + + return false; + } + return true; + }, true, function($fileBaseName, $formFieldName) { + + return substr(hash('sha256', $fileBaseName.uniqid().mt_rand()), 0, 24).".pdf"; + }); + foreach($files as $file => $valid) { if(!$valid) { continue; } - $key = substr(hash('sha256', hash_file('sha256', $file).uniqid()), 0, 24); - rename($file, $f3->get('UPLOADS').$key.'.pdf'); + + $key = str_replace(".pdf", "", basename($file)); } - + if(!$key) { $f3->error(403); } @@ -61,16 +63,20 @@ $f3->route('GET /@key/pdf', $f3->route('POST /image2svg', function($f3) { $files = Web::instance()->receive(function($file,$formFieldName){ - if(strpos(Web::instance()->mime($file['tmp_name'], true), 'image/') !== 0) { + if(strpos(Web::instance()->mime($file['tmp_name'], true), 'image/') !== 0) { - return false; - } - if($file['size'] > (20 * 1024 * 1024)) { // if bigger than 20 MB + return false; + } + if($file['size'] > (20 * 1024 * 1024)) { // if bigger than 20 MB - return false; - } - return true; - }, true); + return false; + } + + return true; + }, true, function($fileBaseName, $formFieldName) { + + return substr(hash('sha256', $fileBaseName.uniqid().mt_rand()), 0, 24).strrchr($fileBaseName, '.'); + }); $imageFile = null; foreach($files as $file => $valid) { @@ -90,6 +96,7 @@ $f3->route('POST /image2svg', header('Content-Type: image/svg+xml'); echo file_get_contents($imageFile.".svg"); + array_map('unlink', glob($imageFile."*")); } ); $f3->route('POST /@key/save', @@ -104,11 +111,14 @@ $f3->route('POST /@key/save', $svgFiles .= $svgFile . " "; } - 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'); + + array_map('unlink', glob($f3->get('UPLOADS').$key."_*.svg")); + unlink($f3->get('UPLOADS').$key.'.svg.pdf'); + unlink($f3->get('UPLOADS').$key.'_signe.pdf'); } );