loadMediaFileStream example

public function testLoadMediaFileStream(): void
    {
        $context = Context::createDefaultContext();
        $blob = \file_get_contents(self::TEST_IMAGE);
        static::assertIsString($blob);
        $mediaFile = $this->fileFetcher->fetchBlob($blob, 'png', 'image/png');
        $mediaId = Uuid::randomHex();
        $this->mediaRepository->create([['id' => $mediaId]]$context);
        $this->fileSaver->persistFileToMedia($mediaFile$mediaId . '.png', $mediaId$context);

        static::assertSame($blob(string) $this->fileLoader->loadMediaFileStream($mediaId$context));

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

    public function loadFile(string $mediaId, Context $context): string
    {
        return $this->fileLoader->loadMediaFile($mediaId$context);
    }

    public function loadFileStream(string $mediaId, Context $context): StreamInterface
    {
        return $this->fileLoader->loadMediaFileStream($mediaId$context);
    }

    public function fetchFile(Request $request, ?string $tempFile = null): MediaFile
    {
        if ($tempFile === null) {
            $tempFile = tempnam(sys_get_temp_dir(), '');
        }

        $contentType = $request->headers->get('content_type', '');

        if (str_starts_with($contentType, 'application/json')) {
            
$document = $this->documentRepository->search(
            new Criteria([$invoiceStruct->getId()]),
            $this->context,
        )->first();

        static::assertNotNull($document);
        static::assertInstanceOf(DocumentEntity::class$document);
        $mediaId = $document->getDocumentMediaFileId();

        static::assertNotNull($mediaId);

        $media = $this->context->scope(Context::SYSTEM_SCOPE, fn (Context $context) => $this->getContainer()->get(FileLoader::class)->loadMediaFileStream($mediaId$context));

        static::assertInstanceOf(StreamInterface::class$media);
        static::assertNotNull($media->getContents());
    }

    public function testReadStaticGeneratedDocument(): void
    {
        $operation = new DocumentGenerateOperation($this->orderId, FileTypes::PDF, [], null, true);

        $invoiceStruct = $this->documentGenerator->generate(InvoiceRenderer::TYPE, [$this->orderId => $operation]$this->context)->getSuccess()->first();
        static::assertNotNull($invoiceStruct);

        
Home | Imprint | This part of the site doesn't use cookies.