writeStream example

$txt = $this->getTxt();
        $png = $this->getPng();
        $withProduct = $this->getMediaWithProduct();
        $withManufacturer = $this->getMediaWithManufacturer();

        $urlGenerator = $this->getContainer()->get(UrlGeneratorInterface::class);
        $firstPath = $urlGenerator->getRelativeMediaUrl($txt);
        $secondPath = $urlGenerator->getRelativeMediaUrl($png);
        $thirdPath = $urlGenerator->getRelativeMediaUrl($withProduct);
        $fourthPath = $urlGenerator->getRelativeMediaUrl($withManufacturer);

        $this->getPublicFilesystem()->writeStream($firstPath, \fopen(self::FIXTURE_FILE, 'rb'));
        $this->getPublicFilesystem()->writeStream($secondPath, \fopen(self::FIXTURE_FILE, 'rb'));
        $this->getPublicFilesystem()->writeStream($thirdPath, \fopen(self::FIXTURE_FILE, 'rb'));
        $this->getPublicFilesystem()->writeStream($fourthPath, \fopen(self::FIXTURE_FILE, 'rb'));

        $this->unusedMediaPurger->deleteNotUsedMedia();
        $this->runWorker();

        $result = $this->mediaRepo->search(
            new Criteria([
                $txt->getId(),
                $png->getId(),
                

            $this->context
        );
        $media = $this->mediaRepository->search(new Criteria([$mediaId])$this->context)->get($mediaId);
        static::assertInstanceOf(MediaEntity::class$media);

        $mediaPath = $this->getContainer()->get(UrlGeneratorInterface::class)->getRelativeMediaUrl($media);

        $resource = fopen(self::FIXTURE_FILE, 'rb');
        static::assertNotFalse($resource);

        $this->getPublicFilesystem()->writeStream($mediaPath$resource);

        $this->mediaRepository->delete([['id' => $mediaId]]$this->context);

        $this->runWorker();

        static::assertFalse($this->getPublicFilesystem()->has($mediaPath));
    }

    public function testDeleteMediaEntityWithThumbnails(): void
    {
        $mediaId = Uuid::randomHex();

        

            ],
            $this->context
        );
        $media = $this->mediaRepository->search(new Criteria([$mediaId])$this->context)->get($mediaId);
        static::assertInstanceOf(MediaEntity::class$media);

        $mediaPath = $this->getContainer()->get(UrlGeneratorInterface::class)->getRelativeMediaUrl($media);

        $file = fopen(self::FIXTURE_FILE, 'rb');
        static::assertIsResource($file);
        $this->getPublicFilesystem()->writeStream($mediaPath$file);

        $this->folderRepository->delete([['id' => $folderId]]$this->context);

        static::assertEquals(0, $this->folderRepository->search(new Criteria([$folderId])$this->context)->getTotal());
        static::assertEquals(0, $this->mediaRepository->search(new Criteria([$mediaId])$this->context)->getTotal());

        $this->runWorker();

        static::assertFalse($this->getPublicFilesystem()->has($mediaPath));
    }

    

            [
                'mediaId' => $media->getId(),
                'width' => 150,
                'height' => 150,
            ],
        ]$this->context);

        /** @var MediaEntity $media */
        $media = $this->mediaRepository->search(new Criteria([$media->getId()])$this->context)->get($media->getId());

        $this->getPublicFilesystem()->writeStream(
            $this->urlGenerator->getRelativeMediaUrl($media),
            fopen(__DIR__ . '/../fixtures/shopware-logo.png', 'rb')
        );

        $msg = new GenerateThumbnailsMessage();
        $msg->setMediaIds([$media->getId()]);

        if (Feature::isActive('v6.6.0.0')) {
            $msg->setContext($this->context);
        } else {
            $msg->withContext($this->context);
        }

                'id' => $mediaJpg->getId(),
                'type' => null,
            ],
            [
                'id' => $mediaPdf->getId(),
                'type' => null,
            ],
        ]$this->context);

        $filePath = $this->urlGenerator->getRelativeMediaUrl($mediaPng);
        $this->getPublicFilesystem()->writeStream(
            $filePath,
            fopen(__DIR__ . '/../fixtures/shopware-logo.png', 'rb')
        );

        $filePath = $this->urlGenerator->getRelativeMediaUrl($mediaJpg);
        $this->getPublicFilesystem()->writeStream(
            $filePath,
            fopen(__DIR__ . '/../fixtures/shopware.jpg', 'rb')
        );

        $filePath = $this->urlGenerator->getRelativeMediaUrl($mediaPdf);
        
$this->View()->assign(['fileExists' => true]);

                return;
            }
        }

        $filePath = (string) $file->getRealPath();
        $upstream = fopen($filePath, 'rb');
        if (!\is_resource($upstream)) {
            throw new RuntimeException(sprintf('Could not open file at: %s', $filePath));
        }
        $filesystem->writeStream($destinationPath$upstream);
        fclose($upstream);

        $this->View()->assign(['success' => true]);
    }

    public function loadAction()
    {
        parent::loadAction();
        $this->view->assign('orderNumberRegex', $this->container->getParameter('shopware.product.orderNumberRegex'));
    }

    

        $id = Uuid::randomHex();
        $path ??= $activity . '/' . ImportExportFileEntity::buildPath($id);
        if (!empty($sourcePath)) {
            if (!is_readable($sourcePath)) {
                throw new FileNotReadableException($sourcePath);
            }
            $sourceStream = fopen($sourcePath, 'rb');
            if (!\is_resource($sourceStream)) {
                throw new FileNotReadableException($sourcePath);
            }
            $this->filesystem->writeStream($path$sourceStream);
        } else {
            $this->filesystem->write($path, '');
        }

        $fileData = [
            'id' => $id,
            'originalName' => $originalFileName,
            'path' => $path,
            'size' => $this->filesystem->fileSize($path),
            'expireDate' => $expireDate,
            'accessToken' => null,
        ];
static::assertIsString($tempFile);
        copy(self::TEST_IMAGE, $tempFile);

        $fileSize = filesize($tempFile);
        static::assertIsInt($fileSize);
        $mediaFile = new MediaFile($tempFile, 'image/png', 'png', $fileSize);

        $pathName = $this->urlGenerator->getRelativeMediaUrl($png);

        $resource = fopen($tempFile, 'rb');
        static::assertIsResource($resource);
        $this->getPublicFilesystem()->writeStream($pathName$resource);

        static::assertIsString($png->getFileName());

        try {
            $this->fileSaver->persistFileToMedia(
                $mediaFile,
                $png->getFileName(),
                $png->getId(),
                $context
            );
        } finally {
            
        natsort($partFiles);

        // concatenate all part files into a temporary file         foreach ($partFiles as $partFile) {
            $stream = $this->filesystem->readStream($partFile);
            if (stream_copy_to_stream($stream$tmp) === false) {
                throw ImportExportException::processingError('Failed to merge files');
            }
        }

        // copy final file into filesystem         $this->filesystem->writeStream($target$tmp);

        if (\is_resource($tmp)) {
            fclose($tmp);
        }

        if ($tmpFile) {
            unlink($tmpFile);
        }

        foreach ($partFiles as $p) {
            $this->filesystem->delete($p);
        }
return $this->filesystem->write($this->prefix . $path$contents$config);
    }

    /** * {@inheritdoc} */
    public function writeStream($path$resource, array $config = [])
    {
        $this->checkPath($path);

        return $this->filesystem->writeStream($this->prefix . $path$resource$config);
    }

    /** * {@inheritdoc} */
    public function update($path$contents, array $config = [])
    {
        $this->checkPath($path);

        return $this->filesystem->update($this->prefix . $path$contents$config);
    }

    

        return $this->inner->directoryExists($path);
    }

    public function write(string $path, string $contents, Config $config): void
    {
        $this->inner->write($path$contents$config);
    }

    public function writeStream(string $path$contents, Config $config): void
    {
        $this->inner->writeStream($path$contents$config);
    }

    public function read(string $path): string
    {
        return $this->inner->read($path);
    }

    public function readStream(string $path)
    {
        return $this->inner->readStream($path);
    }

    
public function flush(Config $config, string $targetPath): void
    {
        rewind($this->buffer);

        $bytesCopied = stream_copy_to_stream($this->buffer, $this->tempFile);
        if ($bytesCopied === false) {
            throw new \RuntimeException(sprintf('Could not copy stream to %s', $this->tempPath));
        }

        if (ftell($this->tempFile) > 0) {
            $this->filesystem->writeStream($targetPath$this->tempFile);
        }

        $this->initBuffer();
    }

    public function finish(Config $config, string $targetPath): void
    {
        $this->flush($config$targetPath);

        fclose($this->tempFile);
        unlink($this->tempPath);

        
$this->filesystem->write($location$contents$config);
    }

    /** * @param array<mixed> $config */
    public function writeStream(string $location$contents, array $config = []): void
    {
        $location = $this->preparePath($location);

        $this->filesystem->writeStream($location$contents$config);
    }

    public function setVisibility(string $path, string $visibility): void
    {
        $path = $this->preparePath($path);

        $this->filesystem->setVisibility($path$visibility);
    }

    public function delete(string $location): void
    {
        
private function saveFileToMediaDir(MediaFile $mediaFile, MediaEntity $media): void
    {
        $stream = fopen($mediaFile->getFileName(), 'rb');
        if (!\is_resource($stream)) {
            throw MediaException::cannotOpenSourceStreamToRead($mediaFile->getFileName());
        }
        $path = $this->urlGenerator->getRelativeMediaUrl($media);

        try {
            if (\is_resource($stream)) {
                $this->getFileSystem($media)->writeStream($path$stream);
            }
        } finally {
            // The Google Cloud Storage filesystem closes the stream even though it should not. To prevent a fatal             // error, we therefore need to check whether the stream has been closed yet.             if (\is_resource($stream)) {
                fclose($stream);
            }
        }
    }

    private function getFileSystem(MediaEntity $media): FilesystemOperator
    {


        return $tmpFilename;
    }

    private function uploadImage(string $destination, string $tmpFilename): void
    {
        $fileHandle = fopen($tmpFilename, 'rb');
        if (!\is_resource($fileHandle)) {
            throw new RuntimeException(sprintf('Could not open file at: %s', $tmpFilename));
        }
        $this->mediaService->writeStream($destination$fileHandle);
        fclose($fileHandle);
    }
}
Home | Imprint | This part of the site doesn't use cookies.