mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-08-25 00:53:48 +00:00
feat: Add new StoredObjectManager
service.
To read and write onto `StoredObject` document using a common interface.
This commit is contained in:
133
src/Bundle/ChillDocStoreBundle/Service/StoredObjectManager.php
Normal file
133
src/Bundle/ChillDocStoreBundle/Service/StoredObjectManager.php
Normal file
@@ -0,0 +1,133 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Chill\DocStoreBundle\Service;
|
||||
|
||||
use Base64Url\Base64Url;
|
||||
use ChampsLibres\AsyncUploaderBundle\TempUrl\TempUrlGeneratorInterface;
|
||||
use Chill\DocStoreBundle\Entity\StoredObject;
|
||||
use Chill\DocStoreBundle\Exception\StoredObjectManagerException;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
|
||||
use Symfony\Contracts\HttpClient\HttpClientInterface;
|
||||
use Throwable;
|
||||
|
||||
use const OPENSSL_RAW_DATA;
|
||||
|
||||
final class StoredObjectManager implements StoredObjectManagerInterface
|
||||
{
|
||||
private const ALGORITHM = 'AES-256-CBC';
|
||||
|
||||
private HttpClientInterface $client;
|
||||
|
||||
private TempUrlGeneratorInterface $tempUrlGenerator;
|
||||
|
||||
public function __construct(
|
||||
HttpClientInterface $client,
|
||||
TempUrlGeneratorInterface $tempUrlGenerator
|
||||
) {
|
||||
$this->client = $client;
|
||||
$this->tempUrlGenerator = $tempUrlGenerator;
|
||||
}
|
||||
|
||||
public function read(StoredObject $document): string
|
||||
{
|
||||
try {
|
||||
$response = $this
|
||||
->client
|
||||
->request(
|
||||
Request::METHOD_GET,
|
||||
$this
|
||||
->tempUrlGenerator
|
||||
->generate(
|
||||
Request::METHOD_GET,
|
||||
$document->getFilename()
|
||||
)
|
||||
->url
|
||||
);
|
||||
} catch (Throwable $e) {
|
||||
throw StoredObjectManagerException::errorDuringHttpRequest($e);
|
||||
}
|
||||
|
||||
if ($response->getStatusCode() !== Response::HTTP_OK) {
|
||||
throw StoredObjectManagerException::invalidStatusCode($response->getStatusCode());
|
||||
}
|
||||
|
||||
try {
|
||||
$data = $response->getContent();
|
||||
} catch (Throwable $e) {
|
||||
throw StoredObjectManagerException::unableToGetResponseContent($e);
|
||||
}
|
||||
|
||||
if (false === $this->hasKeysAndIv($document)) {
|
||||
return $data;
|
||||
}
|
||||
|
||||
$clearData = openssl_decrypt(
|
||||
$data,
|
||||
self::ALGORITHM,
|
||||
// TODO: Why using this library and not use base64_decode() ?
|
||||
Base64Url::decode($document->getKeyInfos()['k']),
|
||||
OPENSSL_RAW_DATA,
|
||||
pack('C*', ...$document->getIv())
|
||||
);
|
||||
|
||||
if (false === $clearData) {
|
||||
throw StoredObjectManagerException::unableToDecrypt(openssl_error_string());
|
||||
}
|
||||
|
||||
return $clearData;
|
||||
}
|
||||
|
||||
public function write(StoredObject $document, string $clearContent): void
|
||||
{
|
||||
$encryptedContent = $this->hasKeysAndIv($document)
|
||||
? openssl_encrypt(
|
||||
$clearContent,
|
||||
self::ALGORITHM,
|
||||
// TODO: Why using this library and not use base64_decode() ?
|
||||
Base64Url::decode($document->getKeyInfos()['k']),
|
||||
OPENSSL_RAW_DATA,
|
||||
pack('C*', ...$document->getIv())
|
||||
)
|
||||
: $clearContent;
|
||||
|
||||
try {
|
||||
$response = $this
|
||||
->client
|
||||
->request(
|
||||
Request::METHOD_PUT,
|
||||
$this
|
||||
->tempUrlGenerator
|
||||
->generate(
|
||||
Request::METHOD_PUT,
|
||||
$document->getFilename()
|
||||
)
|
||||
->url,
|
||||
[
|
||||
'body' => $encryptedContent,
|
||||
]
|
||||
);
|
||||
} catch (TransportExceptionInterface $exception) {
|
||||
throw StoredObjectManagerException::errorDuringHttpRequest($exception);
|
||||
}
|
||||
|
||||
if ($response->getStatusCode() !== Response::HTTP_CREATED) {
|
||||
throw StoredObjectManagerException::invalidStatusCode($response->getStatusCode());
|
||||
}
|
||||
}
|
||||
|
||||
private function hasKeysAndIv(StoredObject $storedObject): bool
|
||||
{
|
||||
return ([] !== $storedObject->getKeyInfos()) && ([] !== $storedObject->getIv());
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user