Skip to content
Snippets Groups Projects
Verified Commit 15244667 authored by Florian Azizian's avatar Florian Azizian
Browse files

FEAT #8754 add image on document

parent 2bc016a9
No related branches found
No related tags found
No related merge requests found
......@@ -8,7 +8,6 @@
"Document\\" : "src/app/document/",
"History\\" : "src/app/history/",
"Resource\\" : "src/app/resource/",
"SignatureBook\\" : "src/app/signatureBook/",
"Status\\" : "src/app/status/",
"User\\" : "src/app/user/"
}
......
......@@ -43,6 +43,7 @@ $app->get('/attachments/{id}', \Attachment\controllers\AttachmentController::cla
//Documents
$app->get('/documents', \Document\controllers\DocumentController::class . ':get');
$app->get('/documents/{id}', \Document\controllers\DocumentController::class . ':getById');
$app->put('/documents/{id}/images', \Document\controllers\DocumentController::class . ':addImages');
//Users
$app->post('/users/{id}/signatures', \User\controllers\UserController::class . ':createSignature');
......
......@@ -14,6 +14,7 @@
namespace Document\controllers;
use SrcCore\models\CoreConfigModel;
use Attachment\models\AttachmentModel;
use Convert\models\AdrModel;
use Docserver\controllers\DocserverController;
......@@ -24,6 +25,8 @@ use Slim\Http\Response;
use SrcCore\models\ValidatorModel;
use Status\models\StatusModel;
use User\models\UserModel;
use History\controllers\HistoryController;
use setasign\Fpdi\TcpdfFpdi;
class DocumentController
{
......@@ -92,6 +95,110 @@ class DocumentController
return $response->withJson(['document' => $document]);
}
public function addImages(Request $request, Response $response, array $args)
{
$data = $request->getParams();
ValidatorModel::notEmpty($data, ['signatures']);
foreach ($data['signatures'] as $signature) {
foreach (['fullPath', 'height', 'width', 'positionX', 'positionY', 'page'] as $value) {
if (empty($signature[$value])) {
return $response->withStatus(400)->withJson(['errors' => $value . ' is empty']);
}
}
}
if (!DocumentController::hasRightById(['id' => $args['id'], 'login' => $GLOBALS['login']])) {
return $response->withStatus(403)->withJson(['errors' => 'Document out of perimeter']);
}
$adr = AdrModel::getDocumentsAdr([
'select' => ['path', 'filename'],
'where' => ['main_document_id = ?', 'type = ?'],
'data' => [$args['id'], 'DOC']
]);
if (empty($adr)) {
return $response->withStatus(400)->withJson(['errors' => 'Document does not exist in database']);
}
$docserver = DocserverModel::getByType(['type' => 'DOC', 'select' => ['path']]);
if (empty($docserver['path']) || !file_exists($docserver['path'])) {
return $response->withStatus(400)->withJson(['errors' => 'Docserver does not exist']);
}
$pathToDocument = $docserver['path'] . $adr[0]['path'] . $adr[0]['filename'];
if (!file_exists($pathToDocument)) {
return $response->withStatus(404)->withJson(['errors' => 'Document not found on docserver']);
}
$tmpPath = CoreConfigModel::getTmpPath();
$tmpFilename = $tmpPath . $GLOBALS['login'] . '_' . rand() . '_' . $adr[0]['filename'];
copy($pathToDocument, $tmpFilename);
$pdf = new TcpdfFpdi('P', 'pt');
$nbPages = $pdf->setSourceFile($tmpFilename);
$pdf->setPrintHeader(false);
for ($i = 1; $i <= $nbPages; $i++) {
$page = $pdf->importPage($i);
$size = $pdf->getTemplateSize($page);
$pdf->AddPage($size['orientation'], $size);
$pdf->useImportedPage($page);
foreach ($data['signatures'] as $signature) {
if ($signature['page'] == $i) {
if (preg_match('/^data:image\/(\w+);base64,/', $signature['fullPath'], $extension)) {
$data = substr($signature['fullPath'], strpos($signature['fullPath'], ',') + 1);
$extension = strtolower($extension[1]);
if ($extension != 'png') {
return $response->withStatus(400)->withJson(['errors' => 'Invalid image type']);
}
} else {
$data = $signature['fullPath'];
}
$image = base64_decode($data);
if ($image === false) {
return $response->withStatus(400)->withJson(['errors' => 'base64_decode failed']);
}
$imageTmpPath = $tmpPath . $GLOBALS['login'] . '_' . rand() . '_writing.png';
file_put_contents($imageTmpPath, $image);
$pdf->Image($imageTmpPath, $signature['positionX'], $signature['positionY']);
}
}
}
$fileContent = $pdf->Output('', 'S');
$storeInfos = DocserverController::storeResourceOnDocServer([
'encodedFile' => base64_encode($fileContent),
'format' => 'pdf',
'docserverType' => 'DOC'
]);
AdrModel::createDocumentAdr([
'documentId' => $args['id'],
'type' => 'SIGNEDDOC',
'path' => $storeInfos['path'],
'filename' => $storeInfos['filename'],
'fingerprint' => $storeInfos['fingerprint']
]);
HistoryController::add([
'tableName' => 'main_documents',
'recordId' => $args['id'],
'eventType' => 'UP',
'info' => _SIGNED_DOCUMENT,
'moduleId' => 'document',
'eventId' => 'documentup',
]);
return $response->withJson(['success' => 'success']);
}
public static function hasRightById(array $args)
{
ValidatorModel::notEmpty($args, ['id', 'login']);
......
......@@ -70,10 +70,11 @@ class HistoryController
$aArgs['level'] = 'DEBUG';
}
LogsController::add($aArgs);
// TODO LOG
// LogsController::add($aArgs);
if (empty($aArgs['userId'])) {
$aArgs['userId'] = $GLOBALS['userId'];
$aArgs['userId'] = $GLOBALS['login'];
}
HistoryModel::create([
......@@ -86,13 +87,6 @@ class HistoryController
'eventId' => $aArgs['eventId'],
]);
NotificationsEventsController::fillEventStack([
"eventId" => $aArgs['eventId'],
"tableName" => $aArgs['tableName'],
"recordId" => $aArgs['recordId'],
"userId" => $aArgs['userId'],
"info" => $aArgs['info'],
]);
}
public function getByUserId(Request $request, Response $response, array $aArgs)
......
......@@ -12,4 +12,4 @@
* @author dev@maarch.org
*/
define('_EX', 'Ex');
define('_SIGNED_DOCUMENT', 'Signed document');
......@@ -12,4 +12,4 @@
* @author dev@maarch.org
*/
define('_EX', 'Ex');
define('_SIGNED_DOCUMENT', 'Document signé');
......@@ -374,13 +374,9 @@ class ClassLoader
$first = $class[0];
if (isset($this->prefixLengthsPsr4[$first])) {
$subPath = $class;
while (false !== $lastPos = strrpos($subPath, '\\')) {
$subPath = substr($subPath, 0, $lastPos);
$search = $subPath.'\\';
if (isset($this->prefixDirsPsr4[$search])) {
foreach ($this->prefixDirsPsr4[$search] as $dir) {
$length = $this->prefixLengthsPsr4[$first][$search];
foreach ($this->prefixLengthsPsr4[$first] as $prefix => $length) {
if (0 === strpos($class, $prefix)) {
foreach ($this->prefixDirsPsr4[$prefix] as $dir) {
if (file_exists($file = $dir . DIRECTORY_SEPARATOR . substr($logicalPathPsr4, $length))) {
return $file;
}
......
Copyright (c) Nils Adermann, Jordi Boggiano
Copyright (c) 2016 Nils Adermann, Jordi Boggiano
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
......
......@@ -14,7 +14,6 @@ return array(
'Status\\' => array($baseDir . '/src/app/status'),
'SrcCore\\' => array($baseDir . '/src/core'),
'Slim\\' => array($vendorDir . '/slim/slim/Slim'),
'SignatureBook\\' => array($baseDir . '/src/app/signatureBook'),
'Respect\\Validation\\' => array($vendorDir . '/respect/validation/library'),
'Resource\\' => array($baseDir . '/src/app/resource'),
'Psr\\Http\\Message\\' => array($vendorDir . '/psr/http-message/src'),
......
......@@ -36,7 +36,6 @@ class ComposerStaticInitf21aebccfa6df888200dcb099aa69fbd
'Status\\' => 7,
'SrcCore\\' => 8,
'Slim\\' => 5,
'SignatureBook\\' => 14,
),
'R' =>
array (
......@@ -124,10 +123,6 @@ class ComposerStaticInitf21aebccfa6df888200dcb099aa69fbd
array (
0 => __DIR__ . '/..' . '/slim/slim/Slim',
),
'SignatureBook\\' =>
array (
0 => __DIR__ . '/../..' . '/src/app/signatureBook',
),
'Respect\\Validation\\' =>
array (
0 => __DIR__ . '/..' . '/respect/validation/library',
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment