FileManager.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Service\File;
  4. use ApiPlatform\Api\IriConverterInterface;
  5. use ApiPlatform\Api\UrlGeneratorInterface;
  6. use ApiPlatform\Metadata\Get;
  7. use App\ApiResources\Core\File\DownloadRequest;
  8. use App\Entity\Access\Access;
  9. use App\Entity\Core\File;
  10. use App\Entity\Organization\Organization;
  11. use App\Entity\Person\Person;
  12. use App\Enum\Core\FileHostEnum;
  13. use App\Enum\Core\FileTypeEnum;
  14. use App\Service\File\Exception\FileNotFoundException;
  15. use App\Service\File\Storage\ApiLegacyStorage;
  16. use App\Service\File\Storage\FileStorageInterface;
  17. use App\Service\File\Storage\LocalStorage;
  18. use Mimey\MimeTypes;
  19. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  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. private LocalStorage $localStorage,
  28. private ApiLegacyStorage $apiLegacyStorage,
  29. protected IriConverterInterface $iriConverter
  30. ) {}
  31. /**
  32. * Retourne le storage dans lequel le fichier demandé est supposé se trouver
  33. * // TODO: voir si ce ne serait pas le boulot d'un ServiceIterator, histoire de rester dans le pattern général
  34. *
  35. * @param File $file
  36. * @return FileStorageInterface
  37. * @throws FileNotFoundException
  38. */
  39. public function getStorageFor(File $file): FileStorageInterface
  40. {
  41. if ($file->getHost() === FileHostEnum::API1()->getValue()) {
  42. return $this->apiLegacyStorage;
  43. }
  44. if ($file->getHost() === FileHostEnum::AP2I()->getValue()) {
  45. return $this->localStorage;
  46. }
  47. throw new FileNotFoundException('File ' . $file->getId() . ' was not found (unknown host: ' . $file->getHost() . ')');
  48. }
  49. /**
  50. * Lit le fichier et retourne son contenu
  51. *
  52. * @param File $file
  53. * @return string
  54. * @throws FileNotFoundException
  55. */
  56. public function read(File $file): string
  57. {
  58. $storage = $this->getStorageFor($file);
  59. return $storage->read($file);
  60. }
  61. /**
  62. * Prepare a File record with a PENDING status.
  63. * This record will hold all the data needed to create the file, except its content.
  64. *
  65. * @param Organization|Access|Person $owner Owner of the file, either an organization, a person or both (access)
  66. * @param string $filename The file's name (mandatory)
  67. * @param FileTypeEnum $type The type of the new file
  68. * @param Access $createdBy Id of the access responsible for this creation
  69. * @param bool $isTemporary Is it a temporary file that can be deleted after some time
  70. * @param string|null $mimeType Mimetype of the file, if not provided, the method will try to guess it from its file name's extension
  71. * @param string $visibility
  72. * @param bool $flushFile Should the newly created file be flushed after having been persisted?
  73. * @return File
  74. */
  75. public function prepareFile(
  76. Organization | Access | Person $owner,
  77. string $filename,
  78. FileTypeEnum $type,
  79. Access $createdBy,
  80. bool $isTemporary = false,
  81. string $visibility = 'NOBODY',
  82. string $mimeType = null,
  83. bool $flushFile = true
  84. ): File
  85. {
  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. * @param File $file
  94. * @param string $content
  95. * @param Access $author
  96. * @return File
  97. * @throws FileNotFoundException
  98. */
  99. public function write(File $file, string $content, Access $author): File {
  100. return $this
  101. ->localStorage
  102. ->write($file, $content, $author);
  103. }
  104. /**
  105. * Convenient method to successively prepare and write a file
  106. *
  107. * @param Organization|Access|Person $owner
  108. * @param string $filename
  109. * @param FileTypeEnum $type
  110. * @param string $content
  111. * @param Access $author
  112. * @param bool $isTemporary
  113. * @param string|null $mimeType
  114. * @param string $visibility
  115. * @return File
  116. */
  117. public function makeFile (
  118. Organization | Access | Person $owner,
  119. string $filename,
  120. FileTypeEnum $type,
  121. string $content,
  122. Access $author,
  123. bool $isTemporary = false,
  124. string $visibility = 'NOBODY',
  125. string $mimeType = null
  126. ): File {
  127. return $this
  128. ->localStorage
  129. ->makeFile($owner, $filename, $type, $content, $author, $isTemporary, $visibility, $mimeType);
  130. }
  131. /**
  132. * Get the IRI to download this file (eg: /api/download/1)
  133. *
  134. * @param File $file
  135. * @return string
  136. */
  137. public function getDownloadIri(File $file): string
  138. {
  139. return $this->iriConverter->getIriFromResource(
  140. DownloadRequest::class,
  141. UrlGeneratorInterface::ABS_PATH,
  142. new Get(),
  143. ['fileId' => $file->getId()]
  144. );
  145. }
  146. /**
  147. * Return the mimetype corresponding to the givent file extension
  148. *
  149. * @param string $ext
  150. * @return string|null
  151. */
  152. public static function getMimeTypeFromExt(string $ext): ?string
  153. {
  154. return (new MimeTypes)->getMimeType(ltrim($ext, '.'));
  155. }
  156. /**
  157. * Try to guess the mimetype from the filename
  158. *
  159. * Return null if it did not manage to guess it.
  160. *
  161. * @param string $filename
  162. * @return string|null
  163. */
  164. public static function guessMimeTypeFromFilename(string $filename): ?string
  165. {
  166. $ext = pathinfo($filename, PATHINFO_EXTENSION);
  167. if (empty($ext)) {
  168. return null;
  169. }
  170. return self::getMimeTypeFromExt($ext);
  171. }
  172. }