CodeExplorer fetchBlob example
$this->fileLoader =
$this->
getContainer()->
get(FileLoader::
class);
$this->fileFetcher =
$this->
getContainer()->
get(FileFetcher::
class);
$this->fileSaver =
$this->
getContainer()->
get(FileSaver::
class);
$this->mediaRepository =
$this->
getContainer()->
get('media.repository'
);
} public function testLoadMediaFile(): 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,
$this->fileLoader->
loadMediaFile($mediaId,
$context));
$this->mediaRepository->
delete([['id' =>
$mediaId]],
$context);
} public function testLoadMediaFileStream(): void
{ },
$downloadFiles)) ->
build();
} $this->salesChannelContext->
getContext()->
scope(Context::SYSTEM_SCOPE,
function DContext
$context) use ($products): void
{ $this->productRepository->
create($products,
$context);
$downloads =
array_merge(...
array_column($products, 'downloads'
));
foreach ($downloads as $download) { $media =
$download['media'
];
$mediaFile =
$this->fileFetcher->
fetchBlob($media['id'
],
$media['fileExtension'
], ''
);
$this->fileSaver->
persistFileToMedia($mediaFile,
$media['fileName'
],
$media['id'
],
$context);
} });
foreach ($ids->
prefixed('product-'
) as $id) { $cart =
$this->
addProduct($id, 1,
$cart,
$this->cartService,
$this->salesChannelContext
);
} return $cart;
} public function saveFile( string
$blob,
string
$extension,
string
$contentType,
string
$filename,
Context
$context,
?string
$folder = null,
?string
$mediaId = null,
bool
$private = true
): string
{ $mediaFile =
$this->fileFetcher->
fetchBlob($blob,
$extension,
$contentType);
if (!
$mediaId) { $mediaId =
$this->
createMediaInFolder($folder ?? '',
$context,
$private);
} $this->fileSaver->
persistFileToMedia($mediaFile,
$filename,
$mediaId,
$context);
return $mediaId;
} public function loadFile(string
$mediaId, Context
$context): string
{