Refactor and simplify document management functionality to adapt to StoredObject versioning

This commit includes several updates to the document management functionality within ChillWopiBundle and ChillDocGeneratorBundle, refactoring for simplicity and improved readability.
This commit is contained in:
Julien Fastré 2024-07-09 22:25:52 +02:00
parent 3978ea9a47
commit e21db73b84
Signed by: julienfastre
GPG Key ID: BDE2190974723FCB
3 changed files with 15 additions and 16 deletions

View File

@ -54,12 +54,15 @@ class LoadDocGeneratorTemplate extends AbstractFixture
]; ];
foreach ($templates as $template) { foreach ($templates as $template) {
$newStoredObj = (new StoredObject()) $newStoredObj = (new StoredObject());
->setFilename($template['file']['filename'])
->setKeyInfos(json_decode($template['file']['key'], true)) $newStoredObj
->setIv(json_decode($template['file']['iv'], true))
->setCreatedAt(new \DateTime('today')) ->setCreatedAt(new \DateTime('today'))
->setType($template['file']['type']); ->registerVersion(
json_decode($template['file']['key'], true),
json_decode($template['file']['iv'], true),
$template['file']['type'],
);
$manager->persist($newStoredObj); $manager->persist($newStoredObj);

View File

@ -134,13 +134,11 @@ class Generator implements GeneratorInterface
$content = Yaml::dump($data, 6); $content = Yaml::dump($data, 6);
/* @var StoredObject $destinationStoredObject */ /* @var StoredObject $destinationStoredObject */
$destinationStoredObject $destinationStoredObject
->setType('application/yaml')
->setFilename(sprintf('%s_yaml', uniqid('doc_', true)))
->setStatus(StoredObject::STATUS_READY) ->setStatus(StoredObject::STATUS_READY)
; ;
try { try {
$this->storedObjectManager->write($destinationStoredObject, $content); $this->storedObjectManager->write($destinationStoredObject, $content, 'application/yaml');
} catch (StoredObjectManagerException $e) { } catch (StoredObjectManagerException $e) {
$destinationStoredObject->addGenerationErrors($e->getMessage()); $destinationStoredObject->addGenerationErrors($e->getMessage());
@ -174,13 +172,11 @@ class Generator implements GeneratorInterface
/* @var StoredObject $destinationStoredObject */ /* @var StoredObject $destinationStoredObject */
$destinationStoredObject $destinationStoredObject
->setType($template->getFile()->getType())
->setFilename(sprintf('%s_odt', uniqid('doc_', true)))
->setStatus(StoredObject::STATUS_READY) ->setStatus(StoredObject::STATUS_READY)
; ;
try { try {
$this->storedObjectManager->write($destinationStoredObject, $generatedResource); $this->storedObjectManager->write($destinationStoredObject, $generatedResource, $template->getFile()->getType());
} catch (StoredObjectManagerException $e) { } catch (StoredObjectManagerException $e) {
$destinationStoredObject->addGenerationErrors($e->getMessage()); $destinationStoredObject->addGenerationErrors($e->getMessage());

View File

@ -50,17 +50,15 @@ final readonly class ChillDocumentManager implements DocumentManagerInterface
// Mime types / extension handling. // Mime types / extension handling.
$mimeTypes = new MimeTypes(); $mimeTypes = new MimeTypes();
$mimeTypes->getMimeTypes($data['extension']); $types = $mimeTypes->getMimeTypes($data['extension']);
$document->setType(reset($mimeTypes)); $mimeType = array_values($types)[0] ?? '';
$document->setFilename($data['name']);
$this->entityManager->persist($document); $this->entityManager->persist($document);
$this->entityManager->flush(); $this->entityManager->flush();
// TODO : Ask proper mapping. // TODO : Ask proper mapping.
// Available: basename, name, extension, content, size // Available: basename, name, extension, content, size
$this->setContent($document, $data['content']); $this->storedObjectManager->write($document, $data['content'], $mimeType);
return $document; return $document;
} }
@ -194,5 +192,7 @@ final readonly class ChillDocumentManager implements DocumentManagerInterface
private function setContent(StoredObject $storedObject, string $content): void private function setContent(StoredObject $storedObject, string $content): void
{ {
$this->storedObjectManager->write($storedObject, $content); $this->storedObjectManager->write($storedObject, $content);
$this->entityManager->flush();
} }
} }