refactoring

This commit is contained in:
Jb Lm 2022-03-31 18:38:12 +02:00
parent 1b4c730f40
commit 7732ed4447
1 changed files with 14 additions and 29 deletions

43
app.php
View File

@ -249,48 +249,33 @@ $f3->route('POST /organize',
$f3->route('GET /signature/@hash/pdf', $f3->route('GET /signature/@hash/pdf',
function($f3) { function($f3) {
$targetDir = $f3->get('STORAGE').$f3->get('PARAMS.hash'); $sharingFolder = $f3->get('STORAGE').$f3->get('PARAMS.hash');
$files = array_diff(scandir($targetDir), array('..', '.')); $files = scandir($sharingFolder);
$original = null; $originalFile = $sharingFolder.'/original.pdf';
$originalFilename = null; $finalFile = $sharingFolder.'/'.$f3->get('PARAMS.hash').'.pdf';
$layers = []; $layers = [];
foreach($files as $file) { foreach($files as $file) {
if (strpos($file, '.pdf') === false || strpos($file, 'signe.pdf') !== false) {
continue;
}
if(strpos($file, 'svg.pdf') !== false) { if(strpos($file, 'svg.pdf') !== false) {
$layers[] = $targetDir.'/'.$file; $layers[] = $sharingFolder.'/'.$file;
continue;
} }
$original = $targetDir.'/'.$file;
$originalFilename = $file;
}
if (!$original) {
$f3->error(404);
} }
if (!$layers) { 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); $bufferFile = str_replace('.pdf', '_tmp.pdf', $originalFile);
$newFilename = str_replace('.pdf', '_signe.pdf', $originalFilename); shell_exec(sprintf("cp %s %s", $originalFile, $finalFile));
shell_exec(sprintf("pdftk %s multibackground %s output %s", $layers[0], $original, $newFile)); foreach($layers as $layer) {
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", $layer, $finalFile, $bufferFile));
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)));
} }
Web::instance()->send($newFile, null, 0, TRUE, $newFilename); Web::instance()->send($finalFile, null, 0, TRUE, $f3->get('PARAMS.hash').'.pdf');
if($f3->get('DEBUG')) {
return;
}
unlink($newFile);
} }
); );
$f3->route('POST /signature/@hash/save', $f3->route('POST /signature/@hash/save',
function($f3) { function($f3) {
$targetDir = $f3->get('STORAGE').$f3->get('PARAMS.hash').'/'; $sharingFolder = $f3->get('STORAGE').$f3->get('PARAMS.hash').'/';
$f3->set('UPLOADS', $targetDir); $f3->set('UPLOADS', $sharingFolder);
$tmpfile = tempnam($targetDir, 'pdfsignature_sign'); $tmpfile = tempnam($sharingFolder, date('YmdHis'));
unlink($tmpfile); unlink($tmpfile);
$svgFiles = ""; $svgFiles = "";