|
|
@@ -5,8 +5,7 @@ namespace App\Service\Export;
|
|
|
|
|
|
use App\ApiResources\Export\ExportRequest;
|
|
|
use App\Entity\Core\File;
|
|
|
-use App\Enum\Core\FileStatusEnum;
|
|
|
-use App\Enum\Export\ExportFormatEnum;
|
|
|
+use App\Enum\Core\FileTypeEnum;
|
|
|
use App\Repository\Access\AccessRepository;
|
|
|
use App\Repository\Core\FileRepository;
|
|
|
use App\Service\Export\Model\ExportModelInterface;
|
|
|
@@ -14,7 +13,6 @@ use App\Service\ServiceIterator\EncoderIterator;
|
|
|
use App\Service\Storage\FileStorage;
|
|
|
use App\Service\Utils\StringsUtils;
|
|
|
use Doctrine\ORM\EntityManagerInterface;
|
|
|
-use Exception;
|
|
|
use Psr\Log\LoggerInterface;
|
|
|
use Symfony\Contracts\Service\Attribute\Required;
|
|
|
use Twig\Environment;
|
|
|
@@ -66,7 +64,6 @@ abstract class BaseExporter implements ExporterInterface
|
|
|
*
|
|
|
* @param ExportRequest $exportRequest
|
|
|
* @return File
|
|
|
- * @throws Exception
|
|
|
*/
|
|
|
public function export(ExportRequest $exportRequest): File
|
|
|
{
|
|
|
@@ -76,48 +73,64 @@ abstract class BaseExporter implements ExporterInterface
|
|
|
// Génère le html à partir du template et du service
|
|
|
$html = $this->render($model);
|
|
|
|
|
|
+ $f = fopen("test.html", "w+");
|
|
|
+ fwrite($f, $html);
|
|
|
+ fclose($f);
|
|
|
+
|
|
|
// Encode le html au format voulu
|
|
|
$content = $this->encode($html, $exportRequest->getFormat());
|
|
|
|
|
|
- // Créé le fichier dans le storage adapté
|
|
|
- $filename = $this->getFileBasename($model);
|
|
|
- if (!preg_match('/^.+\.' . $exportRequest->getFormat() . '$/i', $filename)) {
|
|
|
- $filename .= '.' . $exportRequest->getFormat();
|
|
|
+ $requesterId = $exportRequest->getRequesterId();
|
|
|
+ $requester = $this->accessRepository->find($requesterId);
|
|
|
+ if ($requester === null) {
|
|
|
+ throw new \RuntimeException('Unable to determine the user; abort.');
|
|
|
}
|
|
|
|
|
|
- $path = $this->store($filename, $content);
|
|
|
-
|
|
|
// Met à jour ou créé l'enregistrement du fichier en base
|
|
|
if ($exportRequest->getFileId() !== null) {
|
|
|
$file = $this->fileRepository->find($exportRequest->getFileId());
|
|
|
} else {
|
|
|
// Todo: voir si ce else est nécessaire une fois tous les exports implémentés
|
|
|
- $file = new File();
|
|
|
-
|
|
|
- $requesterId = $exportRequest->getRequesterId();
|
|
|
- $organization = $this->accessRepository->find($requesterId)?->getOrganization();
|
|
|
+ $organization = $requester->getOrganization();
|
|
|
if ($organization === null) {
|
|
|
throw new \RuntimeException('Unable to determine the organization of the curent user; abort.');
|
|
|
}
|
|
|
- $file->setOrganization($organization);
|
|
|
- $file->setVisibility('NOBODY');
|
|
|
- $file->setFolder('DOCUMENTS');
|
|
|
- $file->setCreateDate(new \DateTime());
|
|
|
- $file->setCreatedBy($requesterId);
|
|
|
+ $file = $this->prepareFile($exportRequest, false);
|
|
|
}
|
|
|
|
|
|
- $file->setType($this->getFileType());
|
|
|
- $file->setMimeType(ExportFormatEnum::getMimeType($exportRequest->getFormat()));
|
|
|
- $file->setName($filename);
|
|
|
- $file->setPath($path);
|
|
|
- $file->setSlug($path);
|
|
|
- $file->setStatus(FileStatusEnum::READY()->getValue());
|
|
|
+ return $this->storage->writeFile($file, $content, $requester);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Create a pending file record in the database
|
|
|
+ *
|
|
|
+ * @param ExportRequest $exportRequest
|
|
|
+ * @param bool $flushFile
|
|
|
+ * @return File
|
|
|
+ */
|
|
|
+ public function prepareFile(ExportRequest $exportRequest, bool $flushFile = true): File {
|
|
|
|
|
|
- $this->entityManager->persist($file);
|
|
|
- $this->entityManager->flush();
|
|
|
+ $requesterId = $exportRequest->getRequesterId();
|
|
|
+ $requester = $this->accessRepository->find($requesterId);
|
|
|
+ if ($requester === null) {
|
|
|
+ throw new \RuntimeException('Unable to determine the current user; abort.');
|
|
|
+ }
|
|
|
|
|
|
- // Retourne l'objet File ainsi créé
|
|
|
- return $file;
|
|
|
+ $filename = $this->getFileBasename($exportRequest);
|
|
|
+ if (!preg_match('/^.+\.' . $exportRequest->getFormat() . '$/i', $filename)) {
|
|
|
+ $filename .= '.' . $exportRequest->getFormat();
|
|
|
+ }
|
|
|
+
|
|
|
+ return $this->storage->prepareFile(
|
|
|
+ $requester,
|
|
|
+ $filename,
|
|
|
+ $this->getFileType(),
|
|
|
+ $requester,
|
|
|
+ true,
|
|
|
+ 'NOBODY',
|
|
|
+ FileStorage::getMimeTypeFromExt($exportRequest->getFormat()),
|
|
|
+ $flushFile
|
|
|
+ );
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -125,7 +138,6 @@ abstract class BaseExporter implements ExporterInterface
|
|
|
*
|
|
|
* @param ExportRequest $exportRequest
|
|
|
* @return ExportModelInterface
|
|
|
- * @throws Exception
|
|
|
*/
|
|
|
protected function buildModel(ExportRequest $exportRequest): ExportModelInterface
|
|
|
{
|
|
|
@@ -165,7 +177,6 @@ abstract class BaseExporter implements ExporterInterface
|
|
|
*
|
|
|
* @param ExportModelInterface $model
|
|
|
* @return string Rendu HTML
|
|
|
- * @throws Exception
|
|
|
*/
|
|
|
protected function render(ExportModelInterface $model): string
|
|
|
{
|
|
|
@@ -176,7 +187,7 @@ abstract class BaseExporter implements ExporterInterface
|
|
|
);
|
|
|
}
|
|
|
catch (\Twig\Error\LoaderError | \Twig\Error\RuntimeError | \Twig\Error\SyntaxError $e) {
|
|
|
- throw new \Exception('error during template rendering : ' . $e);
|
|
|
+ throw new \RuntimeException('error during template rendering : ' . $e);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -186,7 +197,6 @@ abstract class BaseExporter implements ExporterInterface
|
|
|
* @param string $html
|
|
|
* @param string $format @see ExportFormatEnum
|
|
|
* @return string
|
|
|
- * @throws Exception
|
|
|
*/
|
|
|
protected function encode(string $html, string $format): string
|
|
|
{
|
|
|
@@ -196,10 +206,10 @@ abstract class BaseExporter implements ExporterInterface
|
|
|
/**
|
|
|
* Retourne le nom du fichier exporté
|
|
|
*
|
|
|
- * @param ExportModelInterface $model
|
|
|
+ * @param ExportRequest $exportRequest
|
|
|
* @return string
|
|
|
*/
|
|
|
- protected function getFileBasename(ExportModelInterface $model): string
|
|
|
+ protected function getFileBasename(ExportRequest $exportRequest): string
|
|
|
{
|
|
|
return $this->getBasename();
|
|
|
}
|
|
|
@@ -207,20 +217,9 @@ abstract class BaseExporter implements ExporterInterface
|
|
|
/**
|
|
|
* Retourne le type de fichier tel qu'il apparait au niveau du champ File.type
|
|
|
*
|
|
|
- * @return string
|
|
|
+ * @return FileTypeEnum
|
|
|
*/
|
|
|
- protected function getFileType(): string {
|
|
|
- return 'UNKNOWN';
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * Créé le fichier
|
|
|
- *
|
|
|
- * @return mixed
|
|
|
- * @throws Exception
|
|
|
- */
|
|
|
- protected function store(string $name, string $content): string
|
|
|
- {
|
|
|
- return $this->storage->write($name, $content);
|
|
|
+ protected function getFileType(): FileTypeEnum {
|
|
|
+ return FileTypeEnum::UNKNOWN();
|
|
|
}
|
|
|
}
|