-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9ea0b3c
commit 6b94193
Showing
43 changed files
with
1,795 additions
and
1,602 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
vendor/ | ||
composer.lock | ||
.idea/ | ||
.phpcs* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,31 @@ | ||
<?php declare(strict_types=1); | ||
/** | ||
* @link https://github.com/monarc-project for the canonical source repository | ||
* @copyright Copyright (c) 2016-2023 Luxembourg House of Cybersecurity LHC.lu - Licensed under GNU Affero GPL v3 | ||
* @copyright Copyright (c) 2016-2024 Luxembourg House of Cybersecurity LHC.lu - Licensed under GNU Affero GPL v3 | ||
* @license MONARC is licensed under GNU Affero General Public License version 3 | ||
*/ | ||
|
||
namespace Monarc\FrontOffice\Export\Controller; | ||
|
||
use Monarc\Core\Controller\Handler\AbstractRestfulControllerRequestHandler; | ||
use Monarc\FrontOffice\Export\Service\AnrInstanceExportService; | ||
use Monarc\FrontOffice\Export\Controller\Traits\ExportResponseControllerTrait; | ||
use Monarc\FrontOffice\Export\Service\InstanceExportService; | ||
use Monarc\FrontOffice\Entity\Anr; | ||
|
||
class ApiAnrInstancesExportController extends AbstractRestfulControllerRequestHandler | ||
{ | ||
private AnrInstanceExportService $anrInstanceExportService; | ||
use ExportResponseControllerTrait; | ||
|
||
public function __construct(AnrInstanceExportService $anrInstanceExportService) | ||
public function __construct(private InstanceExportService $anrInstanceExportService) | ||
{ | ||
$this->anrInstanceExportService = $anrInstanceExportService; | ||
} | ||
|
||
public function create($data) | ||
{ | ||
// TODO: add the $data validator $data['id'] => instanceId is required. | ||
|
||
/** @var Anr $anr */ | ||
$anr = $this->getRequest()->getAttribute('anr'); | ||
$result = $this->anrInstanceExportService->export($anr, $data); | ||
|
||
$output = $this->anrInstanceExportService->export($data); | ||
|
||
if (empty($data['password'])) { | ||
$contentType = 'application/json; charset=utf-8'; | ||
$extension = '.json'; | ||
} else { | ||
$contentType = 'text/plain; charset=utf-8'; | ||
$extension = '.bin'; | ||
} | ||
|
||
$this->getResponse() | ||
->getHeaders() | ||
->clearHeaders() | ||
->addHeaderLine('Content-Type', $contentType) | ||
->addHeaderLine('Content-Disposition', 'attachment; filename="' . | ||
(empty($data['filename']) ? $data['id'] : $data['filename']) . $extension . '"'); | ||
|
||
$this->getResponse() | ||
->setContent($output); | ||
|
||
return $this->getResponse(); | ||
return $this->prepareExportResponse($result['filename'], $result['content'], !empty($data['password'])); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,34 @@ | ||
<?php declare(strict_types=1); | ||
/** | ||
* @link https://github.com/monarc-project for the canonical source repository | ||
* @copyright Copyright (c) 2016-2023 Luxembourg House of Cybersecurity LHC.lu - Licensed under GNU Affero GPL v3 | ||
* @copyright Copyright (c) 2016-2024 Luxembourg House of Cybersecurity LHC.lu - Licensed under GNU Affero GPL v3 | ||
* @license MONARC is licensed under GNU Affero General Public License version 3 | ||
*/ | ||
|
||
namespace Monarc\FrontOffice\Export\Controller; | ||
|
||
use Monarc\Core\Controller\Handler\AbstractRestfulControllerRequestHandler; | ||
use Monarc\Core\Controller\Handler\ControllerRequestResponseHandlerTrait; | ||
use Monarc\FrontOffice\Entity\Anr; | ||
use Monarc\FrontOffice\Export\Service\AnrObjectExportService; | ||
use Monarc\FrontOffice\Export\Controller\Traits\ExportResponseControllerTrait; | ||
use Monarc\FrontOffice\Export\Service\ObjectExportService; | ||
|
||
class ApiAnrObjectsExportController extends AbstractRestfulControllerRequestHandler | ||
{ | ||
use ControllerRequestResponseHandlerTrait; | ||
use ExportResponseControllerTrait; | ||
|
||
private AnrObjectExportService $anrObjectExportService; | ||
|
||
public function __construct(AnrObjectExportService $anrObjectExportService) | ||
public function __construct(private ObjectExportService $anrObjectExportService) | ||
{ | ||
$this->anrObjectExportService = $anrObjectExportService; | ||
} | ||
|
||
/** | ||
* @param array $data | ||
*/ | ||
public function create($data) | ||
{ | ||
// TODO: add a validator. | ||
if (empty($data['id'])) { | ||
throw new \Monarc\Core\Exception\Exception('Object to export is required', 412); | ||
} | ||
|
||
/** @var Anr $anr */ | ||
$anr = $this->getRequest()->getAttribute('anr'); | ||
$result = $this->anrObjectExportService->export($anr, $data); | ||
|
||
// TODO: ... | ||
$data['anr'] = $anr->getId(); | ||
$output = $this->anrObjectExportService->export($anr, $data); | ||
|
||
$response = $this->getResponse(); | ||
$response->setContent($output); | ||
|
||
$headers = $response->getHeaders(); | ||
$filename = empty($data['filename']) ? $data['id'] : $data['filename']; | ||
$headers->clearHeaders() | ||
->addHeaderLine('Content-Type', 'application/json; charset=utf-8') | ||
->addHeaderLine('Content-Disposition', 'attachment; filename="' . $filename . '.json"'); | ||
|
||
return $response; | ||
return $this->prepareExportResponse($result['filename'], $result['content'], !empty($data['password'])); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
src/Export/Controller/Traits/ExportResponseControllerTrait.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php declare(strict_types=1); | ||
/** | ||
* @link https://github.com/monarc-project for the canonical source repository | ||
* @copyright Copyright (c) 2016-2024 Luxembourg House of Cybersecurity LHC.lu - Licensed under GNU Affero GPL v3 | ||
* @license MONARC is licensed under GNU Affero General Public License version 3 | ||
*/ | ||
|
||
namespace Monarc\FrontOffice\Export\Controller\Traits; | ||
|
||
use Laminas\Diactoros\Response; | ||
use Psr\Http\Message\ResponseInterface; | ||
|
||
use function strlen; | ||
|
||
trait ExportResponseControllerTrait | ||
{ | ||
private function prepareExportResponse(string $filename, string $output, bool $isEncrypted): ResponseInterface | ||
{ | ||
$dataType = 'application/json'; | ||
$contentType = 'application/json; charset=utf-8'; | ||
$extension = '.json'; | ||
if ($isEncrypted) { | ||
$dataType = 'text/plain'; | ||
$contentType = 'text/plain; charset=utf-8'; | ||
$extension = '.bin'; | ||
} | ||
$stream = fopen('data://' . $dataType . ',' . $output, 'rb+'); | ||
|
||
return new Response($stream, 200, [ | ||
'Content-Type' => $contentType, | ||
'Content-Length' => strlen($output), | ||
'Content-Disposition' => 'attachment; filename="' . $filename . $extension . '"', | ||
]); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.