store encrypted content

This commit is contained in:
2024-12-20 21:23:02 +01:00
parent c65f1d495d
commit 0c628c39db
4 changed files with 104 additions and 16 deletions

View File

@@ -31,7 +31,7 @@ class StoredObjectManager implements StoredObjectManagerInterface
ParameterBagInterface $parameterBag,
private readonly KeyGenerator $keyGenerator,
) {
$this->baseDir = $parameterBag->get('chill_doc_store')['local_storage']['base_dir'];
$this->baseDir = $parameterBag->get('chill_doc_store')['local_storage']['storage_path'];
$this->filesystem = new Filesystem();
}
@@ -65,9 +65,7 @@ class StoredObjectManager implements StoredObjectManagerInterface
return false;
}
$path = $this->buildPath($version->getFilename());
return $this->filesystem->exists($path);
return $this->existsContent($version->getFilename());
}
public function read(StoredObject|StoredObjectVersion $document): string
@@ -78,15 +76,7 @@ class StoredObjectManager implements StoredObjectManagerInterface
throw StoredObjectManagerException::storedObjectDoesNotContainsVersion();
}
$path = $this->buildPath($version->getFilename());
if (!file_exists($path)) {
throw StoredObjectManagerException::unableToFindDocumentOnDisk($path);
}
if (false === $content = file_get_contents($path)) {
throw StoredObjectManagerException::unableToReadDocumentOnDisk($path);
}
$content = $this->readContent($version->getFilename());
if (!$this->isVersionEncrypted($version)) {
return $content;
@@ -134,7 +124,29 @@ class StoredObjectManager implements StoredObjectManagerInterface
throw StoredObjectManagerException::unableToEncryptDocument((string) openssl_error_string());
}
$fullPath = $this->buildPath($version->getFilename());
$this->writeContent($version->getFilename(), $encryptedContent);
return $version;
}
public function readContent(string $filename): string
{
$path = $this->buildPath($filename);
if (!file_exists($path)) {
throw StoredObjectManagerException::unableToFindDocumentOnDisk($path);
}
if (false === $content = file_get_contents($path)) {
throw StoredObjectManagerException::unableToReadDocumentOnDisk($path);
}
return $content;
}
public function writeContent(string $filename, string $encryptedContent): void
{
$fullPath = $this->buildPath($filename);
$dir = Path::getDirectory($fullPath);
if (!$this->filesystem->exists($dir)) {
@@ -146,8 +158,13 @@ class StoredObjectManager implements StoredObjectManagerInterface
if (false === $result) {
throw StoredObjectManagerException::unableToStoreDocumentOnDisk();
}
}
return $version;
public function existsContent(string $filename): bool
{
$path = $this->buildPath($filename);
return $this->filesystem->exists($path);
}
private function buildPath(string $filename): string