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',
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 = "";