filesystem = $filesystemMap->get(static::FS_KEY); } /** * Return true if the file exists in the file storage * * @param File $file * @return bool */ public function exists(File $file): bool { return $this->filesystem->has($file->getSlug()); } /** * Lists all the non-temporary files of the given owner * * @param Organization|Access|Person $owner * @param FileTypeEnum|null $type * @return list */ public function listByOwner ( Organization | Access | Person $owner, ?FileTypeEnum $type = null ): array { return $this->filesystem->listKeys( $this->getPrefix($owner, false, $type) ); } /** * Reads the given file and returns its content as a string * * @param File $file * @return string */ public function read(File $file): string { return $this->filesystem->read($file->getSlug()); } /** * Retoune l'URL de l'image, à la bonne taille, contenu dans la File * @param File $file * @param string $size * @param bool $relativePath * @return string */ public function getImageUrl(File $file, string $size, bool $relativePath): string { $filterName = $this->getFilterFromSizeAndConfig($size, !empty($file->getConfig())); $path = $file->getPath(); if (!$this->cacheManager->isStored($path, $filterName)) { $this->imageFactory->createImageContent($file, $filterName); } $url = $this->cacheManager->resolve($path, $filterName); return $relativePath ? $this->urlBuilder->getRelativeUrl($url) : $url; } /** * Retourne le filtre Liip correspondant à la taille désirée * @param bool $configExist * @param string $size * @return string */ private function getFilterFromSizeAndConfig(string $size, bool $configExist): string{ switch($size){ case FileSizeEnum::SM : $filter = $configExist ? self::CROP_SM : self::SM_FOLDER; break; case FileSizeEnum::MD : default: $filter = $configExist ? self::CROP_MD : self::MD_FOLDER; break; case FileSizeEnum::LG : $filter = $configExist ? self::CROP_LG : self::LG_FOLDER; break; } return $filter; } /** * 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 { [$organization, $person] = $this->getOrganizationAndPersonFromOwner($owner); $file = (new File()) ->setName($filename) ->setOrganization($organization) ->setPerson($person) ->setSlug(null) ->setType($type) ->setVisibility($visibility) ->setIsTemporaryFile($isTemporary) ->setMimeType($mimeType ?? $this->fileUtils->guessMimeTypeFromFilename($filename)) ->setCreateDate(new DateTime()) ->setCreatedBy($createdBy->getId()) ->setStatus(FileStatusEnum::PENDING); $this->entityManager->persist($file); if ($flushFile) { $this->entityManager->flush(); } return $file; } /** * Write the $content into the file storage and update the given File object's size, slug, status (READY)... * * @param File $file The file object that is about to be written * @param string $content The content of the file * @param Access $author The access responsible for the creation / update of the file * @return File */ public function write(File $file, string $content, Access $author): File { if (empty($file->getName())) { throw new RuntimeException('File has no filename'); } $isNewFile = $file->getSlug() === null; if ($isNewFile) { // Try to get the Access owner from the organization_id and person_id $access = null; if ($file->getOrganization() !== null && $file->getPerson() !== null) { $access = $this->accessRepository->findOneBy( ['organization' => $file->getOrganization(), 'person' => $file->getPerson()] ); } $prefix = $this->getPrefix( $access ?? $file->getOrganization() ?? $file->getPerson(), $file->getIsTemporaryFile(), $file->getType() ); $uid = date('Ymd_His') . '_' . Uuid::uuid(5); $key = Path::join($prefix, $uid, $file->getName()); } else { $key = $file->getSlug(); } if (!$isNewFile && !$this->filesystem->has($key)) { throw new RuntimeException('The file `' . $key . '` does not exist in the file storage'); } $size = $this->filesystem->write($key, $content, true); $file->setSize($size) ->setStatus(FileStatusEnum::READY); if ($isNewFile) { $file->setSlug($key) ->setCreateDate(new DateTime()) ->setCreatedBy($author->getId()); } else { $file->setUpdateDate(new DateTime()) ->setUpdatedBy($author->getId()); } $this->entityManager->flush(); return $file; } /** * 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 * @param string|null $config * @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, string $config = null ): File { $file = $this->prepareFile( $owner, $filename, $type, $author, $isTemporary, $visibility, $mimeType, false ); if (!empty($config)) { // TODO: Déplacer dans le prepareFile? $file->setConfig($config); } return $this->write($file, $content, $author); } /** * Uupdate the status of the File * * @param File $file * @param Access $author * @return File */ public function softDelete(File $file, Access $author): File { $file->setStatus(FileStatusEnum::DELETED) ->setSize(0) ->setUpdatedBy($author->getId()); return $file; } /** * Delete the given file from the filesystem * * @param File $file */ public function hardDelete(File $file): void { $deleted = $this->filesystem->delete($file->getSlug()); if (!$deleted) { throw new RuntimeException('File `' . $file->getSlug() . '` could\'nt be deleted'); } } /** * If an organization owns the file, the prefix will be '(_temp_/)organization/{id}(/{type})'. * If a person owns it, the prefix will be '(_temp_/)person/{id}(/{type})' * If access owns it, the prefix will be '(_temp_/)organization/{organization_id}/{access_id}(/{type})' * * With {id} being the id of the organization or of the person. * * If the file is temporary, '_temp_/' is prepended to the prefix. * If a file type is given, this type is appended to the prefix (low case) * * @param Organization|Access|Person $owner * @param bool $isTemporary * @param FileTypeEnum|null $type * @return string */ protected function getPrefix( Organization | Access | Person $owner, bool $isTemporary, FileTypeEnum $type = null ): string { if ($owner instanceof Access) { $prefix = Path::join('organization', $owner->getOrganization()?->getId(), $owner->getId()); } else if ($owner instanceof Organization) { $prefix = Path::join('organization', $owner->getId()); } else { $prefix = Path::join('person', $owner->getId()); } if ($isTemporary) { $prefix = Path::join('temp', $prefix); } if ($type !== null && $type !== FileTypeEnum::NONE) { $prefix = Path::join($prefix, strtolower($type->value)); } return $prefix; } /** * Return an array [$organization, $person] from a given owner * * @param Organization|Access|Person $owner * @return list */ #[Pure] protected function getOrganizationAndPersonFromOwner(Organization | Access | Person $owner): array { if ($owner instanceof Access) { return [$owner->getOrganization(), $owner->getPerson()]; } if ($owner instanceof Organization) { return [$owner, null]; } return [null, $owner]; } /** * @param File $file * @return bool */ public function support(File $file): bool { return $file->getHost() === FileHostEnum::AP2I; } }