FileManager.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Service\File;
  4. use ApiPlatform\Api\IriConverterInterface;
  5. use ApiPlatform\Api\UrlGeneratorInterface as UrlGeneratorInterfaceApiPlatform;
  6. use ApiPlatform\Metadata\Get;
  7. use App\Entity\Access\Access;
  8. use App\Entity\Core\File;
  9. use App\Entity\Organization\Organization;
  10. use App\Entity\Person\Person;
  11. use App\Enum\Core\FileTypeEnum;
  12. use App\Enum\Core\FileVisibilityEnum;
  13. use App\Repository\Core\FileRepository;
  14. use App\Service\File\Exception\FileNotFoundException;
  15. use App\Service\File\Factory\ImageFactory;
  16. use App\Service\File\Storage\FileStorageInterface;
  17. use App\Service\File\Storage\LocalStorage;
  18. use App\Service\ServiceIterator\StorageIterator;
  19. use Doctrine\ORM\EntityManagerInterface;
  20. /**
  21. * Le gestionnaire de fichiers permet d'effectuer de nombreuses opérations sur les fichiers stockés dans les différents
  22. * FileStorage (lecture, écriture, suppression...).
  23. */
  24. class FileManager
  25. {
  26. public function __construct(
  27. protected readonly IriConverterInterface $iriConverter,
  28. protected readonly StorageIterator $storageIterator,
  29. protected readonly ImageFactory $imageFactory,
  30. protected readonly LocalStorage $localStorage,
  31. protected readonly EntityManagerInterface $entityManager,
  32. protected readonly FileRepository $fileRepository,
  33. ) {
  34. }
  35. /**
  36. * Retourne le storage dans lequel le fichier demandé est supposé se trouver.
  37. *
  38. * @throws FileNotFoundException
  39. */
  40. public function getStorageFor(File $file): FileStorageInterface
  41. {
  42. return $this->storageIterator->getStorageFor($file);
  43. }
  44. /**
  45. * Lit le fichier et retourne son contenu.
  46. *
  47. * @throws FileNotFoundException
  48. */
  49. public function read(File $file): string
  50. {
  51. $storage = $this->getStorageFor($file);
  52. return $storage->read($file);
  53. }
  54. /**
  55. * Lit le fichier Image et retourne une URL.
  56. *
  57. * @throws FileNotFoundException
  58. */
  59. public function getImageUrl(File $file, string $size, bool $relativePath = false): string
  60. {
  61. $storage = $this->getStorageFor($file);
  62. return $storage->getImageUrl($file, $size, $relativePath);
  63. }
  64. /**
  65. * Prepare a File record with a PENDING status.
  66. * This record will hold all the data needed to create the file, except its content.
  67. *
  68. * @param Organization|Access|Person $owner Owner of the file, either an organization, a person or both (access)
  69. * @param string $filename The file's name (mandatory)
  70. * @param FileTypeEnum $type The type of the new file
  71. * @param Access $createdBy Id of the access responsible for this creation
  72. * @param bool $isTemporary Is it a temporary file that can be deleted after some time
  73. * @param string|null $mimeType Mimetype of the file, if not provided, the method will try to guess it from its file name's extension
  74. * @param bool $flushFile Should the newly created file be flushed after having been persisted?
  75. */
  76. public function prepareFile(
  77. Organization|Access|Person $owner,
  78. string $filename,
  79. FileTypeEnum $type,
  80. Access $createdBy,
  81. bool $isTemporary = false,
  82. FileVisibilityEnum $visibility = FileVisibilityEnum::NOBODY,
  83. ?string $mimeType = null,
  84. bool $flushFile = true,
  85. ): File {
  86. return $this
  87. ->localStorage
  88. ->prepareFile($owner, $filename, $type, $createdBy, $isTemporary, $visibility, $mimeType, $flushFile);
  89. }
  90. /**
  91. * Write the $content into the file storage and update the given File object's size, slug, status (READY)...
  92. *
  93. * @throws FileNotFoundException
  94. */
  95. public function write(File $file, string $content, Access $author): File
  96. {
  97. return $this
  98. ->localStorage
  99. ->write($file, $content, $author);
  100. }
  101. /**
  102. * Convenient method to successively prepare and write a file.
  103. */
  104. public function makeFile(
  105. Organization|Access|Person $owner,
  106. string $filename,
  107. FileTypeEnum $type,
  108. string $content,
  109. Access $author,
  110. bool $isTemporary = false,
  111. FileVisibilityEnum $visibility = FileVisibilityEnum::NOBODY,
  112. ?string $mimeType = null,
  113. ?string $config = null,
  114. ): File {
  115. return $this
  116. ->localStorage
  117. ->makeFile($owner, $filename, $type, $content, $author, $isTemporary, $visibility, $mimeType);
  118. }
  119. /**
  120. * Get the IRI to download this file (eg: /api/download/1).
  121. */
  122. public function getDownloadIri(File $file): string
  123. {
  124. return $this->iriConverter->getIriFromResource(
  125. File::class,
  126. UrlGeneratorInterfaceApiPlatform::ABS_PATH,
  127. new Get(),
  128. ['fileId' => $file->getId()]
  129. );
  130. }
  131. /**
  132. * Permanently delete the organization's files from each storage, and remove any reference
  133. * in the DB.
  134. */
  135. public function deleteOrganizationFiles(int $organizationId): void
  136. {
  137. foreach ($this->storageIterator->getStorages() as $storageService) {
  138. $storageService->deleteOrganizationFiles($organizationId);
  139. }
  140. $this->fileRepository->deleteByOrganization($organizationId);
  141. }
  142. /**
  143. * Permanently delete the person's files from each storage, and remove any reference
  144. * * in the DB.
  145. */
  146. public function deletePersonFiles(int $personId): void
  147. {
  148. foreach ($this->storageIterator->getStorages() as $storageService) {
  149. $storageService->deletePersonFiles($personId);
  150. }
  151. $this->fileRepository->deleteByPerson($personId);
  152. }
  153. }