getFileSystem example

$toBeCreatedSizes = new MediaThumbnailSizeCollection($config->getMediaThumbnailSizes()->getElements());
        $toBeDeletedThumbnails = new MediaThumbnailCollection($media->getThumbnails()->getElements());

        foreach ($toBeCreatedSizes as $thumbnailSize) {
            foreach ($toBeDeletedThumbnails as $thumbnail) {
                if (!$this->isSameDimension($thumbnail$thumbnailSize)) {
                    continue;
                }

                if ($strict === true
                    && !$this->getFileSystem($media)->fileExists($this->urlGenerator->getRelativeThumbnailUrl($media$thumbnail))) {
                    continue;
                }

                $toBeDeletedThumbnails->remove($thumbnail->getId());
                $toBeCreatedSizes->remove($thumbnailSize->getId());

                continue 2;
            }
        }

        $delete = \array_values(\array_map(static fn (string $id) => ['id' => $id]$toBeDeletedThumbnails->getIds()));

        

    public function __construct(
        private readonly FilesystemOperator $filesystemPublic,
        private readonly FilesystemOperator $filesystemPrivate
    ) {
    }

    public function __invoke(DeleteFileMessage $message): void
    {
        foreach ($message->getFiles() as $file) {
            try {
                $this->getFileSystem($message->getVisibility())->delete($file);
            } catch (UnableToDeleteFile) {
                // ignore file is already deleted             }
        }
    }

    private function getFileSystem(string $visibility): FilesystemOperator
    {
        return match ($visibility) {
            Visibility::PUBLIC => $this->filesystemPublic,
            Visibility::PRIVATE => $this->filesystemPrivate,
            
private readonly UrlGeneratorInterface $urlGenerator,
        private readonly MediaService $mediaService,
        private readonly string $localPrivateDownloadStrategy
    ) {
    }

    public function getResponse(
        MediaEntity $media,
        SalesChannelContext $context,
        string $expiration = '+120 minutes'
    ): Response {
        $fileSystem = $this->getFileSystem($media);
        $path = $this->urlGenerator->getRelativeMediaUrl($media);

        try {
            $url = $fileSystem->temporaryUrl($path(new \DateTime())->modify($expiration));

            return new RedirectResponse($url);
        } catch (UnableToGenerateTemporaryUrl) {
        }

        return $this->getDefaultResponse($media$context$fileSystem);
    }

    

  public static function getFileExtension() {
    return 'yml';
  }

  /** * Check if the directory exists and create it if not. */
  protected function ensureStorage() {
    $dir = $this->getCollectionDirectory();
    $success = $this->getFileSystem()->prepareDirectory($dir, FileSystemInterface::CREATE_DIRECTORY | FileSystemInterface::MODIFY_PERMISSIONS);
    // Only create .htaccess file in root directory.     if ($dir == $this->directory) {
      $success = $success && FileSecurity::writeHtaccess($this->directory);
    }
    if (!$success) {
      throw new StorageException('Failed to create config directory ' . $dir);
    }
    return $this;
  }

  /** * {@inheritdoc} */

  public function stream_truncate($new_size) {
    return ftruncate($this->handle, $new_size);
  }

  /** * {@inheritdoc} */
  public function unlink($uri) {
    $this->uri = $uri;
    return $this->getFileSystem()->unlink($this->getLocalPath());
  }

  /** * {@inheritdoc} */
  public function rename($from_uri$to_uri) {
    return rename($this->getLocalPath($from_uri)$this->getLocalPath($to_uri));
  }

  /** * {@inheritdoc} */
private readonly UrlGeneratorInterface $urlGenerator,
        private readonly EntityRepository $mediaRepository,
        private readonly StreamFactoryInterface $streamFactory
    ) {
        $this->fileNameValidator = new FileNameValidator();
    }

    public function loadMediaFile(string $mediaId, Context $context): string
    {
        $media = $this->findMediaById($mediaId$context);

        return $this->getFileSystem($media)->read($this->getFilePath($media)) ?: '';
    }

    public function loadMediaFileStream(string $mediaId, Context $context): StreamInterface
    {
        $media = $this->findMediaById($mediaId$context);
        $resource = $this->getFileSystem($media)->readStream($this->getFilePath($media));

        return $this->streamFactory->createStreamFromResource($resource);
    }

    private function getFilePath(MediaEntity $media): string
    {
private function doRenameMedia(MediaEntity $currentMedia, string $destination, Context $context): void
    {
        $updatedMedia = clone $currentMedia;
        $updatedMedia->setFileName($destination);
        $updatedMedia->setUploadedAt(new \DateTime());

        try {
            $renamedFiles = $this->renameFile(
                $this->urlGenerator->getRelativeMediaUrl($currentMedia),
                $this->urlGenerator->getRelativeMediaUrl($updatedMedia),
                $this->getFileSystem($currentMedia)
            );
        } catch (\Exception) {
            throw MediaException::couldNotRenameFile($currentMedia->getId()(string) $currentMedia->getFileName());
        }

        foreach ($currentMedia->getThumbnails() ?? [] as $thumbnail) {
            try {
                $renamedFiles = [...$renamedFiles, ...$this->renameThumbnail($thumbnail$currentMedia$updatedMedia)];
            } catch (\Exception) {
                $this->rollbackRenameAction($currentMedia$renamedFiles);
            }
        }
Home | Imprint | This part of the site doesn't use cookies.