From 7732ed44478cf40d15bf2ed27ae010cc1131f9dd Mon Sep 17 00:00:00 2001 From: Jb Lm Date: Thu, 31 Mar 2022 18:38:12 +0200 Subject: [PATCH] refactoring --- app.php | 43 ++++++++++++++----------------------------- 1 file changed, 14 insertions(+), 29 deletions(-) diff --git a/app.php b/app.php index f43d93f..95681fb 100644 --- a/app.php +++ b/app.php @@ -249,48 +249,33 @@ $f3->route('POST /organize', $f3->route('GET /signature/@hash/pdf', function($f3) { - $targetDir = $f3->get('STORAGE').$f3->get('PARAMS.hash'); - $files = array_diff(scandir($targetDir), array('..', '.')); - $original = null; - $originalFilename = null; + $sharingFolder = $f3->get('STORAGE').$f3->get('PARAMS.hash'); + $files = scandir($sharingFolder); + $originalFile = $sharingFolder.'/original.pdf'; + $finalFile = $sharingFolder.'/'.$f3->get('PARAMS.hash').'.pdf'; $layers = []; foreach($files as $file) { - if (strpos($file, '.pdf') === false || strpos($file, 'signe.pdf') !== false) { - continue; - } if(strpos($file, 'svg.pdf') !== false) { - $layers[] = $targetDir.'/'.$file; - continue; + $layers[] = $sharingFolder.'/'.$file; } - $original = $targetDir.'/'.$file; - $originalFilename = $file; - } - if (!$original) { - $f3->error(404); } if (!$layers) { - Web::instance()->send($original, null, 0, TRUE, str_replace('.pdf', '_signe.pdf', $originalFilename)); + Web::instance()->send($originalFile, null, 0, TRUE, $f3->get('PARAMS.hash').'.pdf'); } - $newFile = str_replace('.pdf', '_signe.pdf', $original); - $newFilename = str_replace('.pdf', '_signe.pdf', $originalFilename); - shell_exec(sprintf("pdftk %s multibackground %s output %s", $layers[0], $original, $newFile)); - for($i = 1, $max = count($layers); $i < $max; $i++) { - shell_exec(sprintf("pdftk %1\$s multibackground %2\$s output %3\$s && mv %3\$s %2\$s", $layers[$i], $newFile, str_replace('_signe.pdf', '_tmp_signe.pdf', $newFile))); + $bufferFile = str_replace('.pdf', '_tmp.pdf', $originalFile); + shell_exec(sprintf("cp %s %s", $originalFile, $finalFile)); + foreach($layers as $layer) { + shell_exec(sprintf("pdftk %1\$s multibackground %2\$s output %3\$s && mv %3\$s %2\$s", $layer, $finalFile, $bufferFile)); } - Web::instance()->send($newFile, null, 0, TRUE, $newFilename); - - if($f3->get('DEBUG')) { - return; - } - unlink($newFile); + Web::instance()->send($finalFile, null, 0, TRUE, $f3->get('PARAMS.hash').'.pdf'); } ); $f3->route('POST /signature/@hash/save', function($f3) { - $targetDir = $f3->get('STORAGE').$f3->get('PARAMS.hash').'/'; - $f3->set('UPLOADS', $targetDir); - $tmpfile = tempnam($targetDir, 'pdfsignature_sign'); + $sharingFolder = $f3->get('STORAGE').$f3->get('PARAMS.hash').'/'; + $f3->set('UPLOADS', $sharingFolder); + $tmpfile = tempnam($sharingFolder, date('YmdHis')); unlink($tmpfile); $svgFiles = "";