| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183 |
- <?php
- declare(strict_types=1);
- namespace App\Service\File;
- use ApiPlatform\Api\IriConverterInterface;
- use ApiPlatform\Api\UrlGeneratorInterface;
- use ApiPlatform\Metadata\Get;
- use App\ApiResources\Core\File\DownloadRequest;
- use App\Entity\Access\Access;
- use App\Entity\Core\File;
- use App\Entity\Organization\Organization;
- use App\Entity\Person\Person;
- use App\Enum\Core\FileHostEnum;
- use App\Enum\Core\FileTypeEnum;
- use App\Service\File\Exception\FileNotFoundException;
- use App\Service\File\Storage\ApiLegacyStorage;
- use App\Service\File\Storage\FileStorageInterface;
- use App\Service\File\Storage\LocalStorage;
- use Mimey\MimeTypes;
- use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
- /**
- * Le gestionnaire de fichiers permet d'effectuer de nombreuses opérations sur les fichiers stockés dans les différents
- * FileStorage (lecture, écriture, suppression...)
- */
- class FileManager
- {
- public function __construct(
- private LocalStorage $localStorage,
- private ApiLegacyStorage $apiLegacyStorage,
- protected IriConverterInterface $iriConverter
- ) {}
- /**
- * Retourne le storage dans lequel le fichier demandé est supposé se trouver
- * // TODO: voir si ce ne serait pas le boulot d'un ServiceIterator, histoire de rester dans le pattern général
- *
- * @param File $file
- * @return FileStorageInterface
- * @throws FileNotFoundException
- */
- public function getStorageFor(File $file): FileStorageInterface
- {
- if ($file->getHost() === FileHostEnum::API1()->getValue()) {
- return $this->apiLegacyStorage;
- }
- if ($file->getHost() === FileHostEnum::AP2I()->getValue()) {
- return $this->localStorage;
- }
- throw new FileNotFoundException('File ' . $file->getId() . ' was not found (unknown host: ' . $file->getHost() . ')');
- }
- /**
- * Lit le fichier et retourne son contenu
- *
- * @param File $file
- * @return string
- * @throws FileNotFoundException
- */
- public function read(File $file): string
- {
- $storage = $this->getStorageFor($file);
- return $storage->read($file);
- }
- /**
- * Prepare a File record with a PENDING status.
- * This record will hold all the data needed to create the file, except its content.
- *
- * @param Organization|Access|Person $owner Owner of the file, either an organization, a person or both (access)
- * @param string $filename The file's name (mandatory)
- * @param FileTypeEnum $type The type of the new file
- * @param Access $createdBy Id of the access responsible for this creation
- * @param bool $isTemporary Is it a temporary file that can be deleted after some time
- * @param string|null $mimeType Mimetype of the file, if not provided, the method will try to guess it from its file name's extension
- * @param string $visibility
- * @param bool $flushFile Should the newly created file be flushed after having been persisted?
- * @return File
- */
- public function prepareFile(
- Organization | Access | Person $owner,
- string $filename,
- FileTypeEnum $type,
- Access $createdBy,
- bool $isTemporary = false,
- string $visibility = 'NOBODY',
- string $mimeType = null,
- bool $flushFile = true
- ): File
- {
- return $this
- ->localStorage
- ->prepareFile($owner, $filename, $type, $createdBy, $isTemporary, $visibility, $mimeType, $flushFile);
- }
- /**
- * Write the $content into the file storage and update the given File object's size, slug, status (READY)...
- *
- * @param File $file
- * @param string $content
- * @param Access $author
- * @return File
- * @throws FileNotFoundException
- */
- public function write(File $file, string $content, Access $author): File {
- return $this
- ->localStorage
- ->write($file, $content, $author);
- }
- /**
- * Convenient method to successively prepare and write a file
- *
- * @param Organization|Access|Person $owner
- * @param string $filename
- * @param FileTypeEnum $type
- * @param string $content
- * @param Access $author
- * @param bool $isTemporary
- * @param string|null $mimeType
- * @param string $visibility
- * @return File
- */
- public function makeFile (
- Organization | Access | Person $owner,
- string $filename,
- FileTypeEnum $type,
- string $content,
- Access $author,
- bool $isTemporary = false,
- string $visibility = 'NOBODY',
- string $mimeType = null
- ): File {
- return $this
- ->localStorage
- ->makeFile($owner, $filename, $type, $content, $author, $isTemporary, $visibility, $mimeType);
- }
- /**
- * Get the IRI to download this file (eg: /api/download/1)
- *
- * @param File $file
- * @return string
- */
- public function getDownloadIri(File $file): string
- {
- return $this->iriConverter->getIriFromResource(
- DownloadRequest::class,
- UrlGeneratorInterface::ABS_PATH,
- new Get(),
- ['fileId' => $file->getId()]
- );
- }
- /**
- * Return the mimetype corresponding to the givent file extension
- *
- * @param string $ext
- * @return string|null
- */
- public static function getMimeTypeFromExt(string $ext): ?string
- {
- return (new MimeTypes)->getMimeType(ltrim($ext, '.'));
- }
- /**
- * Try to guess the mimetype from the filename
- *
- * Return null if it did not manage to guess it.
- *
- * @param string $filename
- * @return string|null
- */
- public static function guessMimeTypeFromFilename(string $filename): ?string
- {
- $ext = pathinfo($filename, PATHINFO_EXTENSION);
- if (empty($ext)) {
- return null;
- }
- return self::getMimeTypeFromExt($ext);
- }
- }
|