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); } }