Ich erstelle ein Produkt mit ca. 50 - 200 Varianten. Hierzu werden Bilder aus einem externen System gezogen und abgelegt als media und zugewiesen. Mein aktuelles Problem ist dass in der development Umgebung manche Thumbnails einfach nicht erzeugt werden. Kennt jemand dieses Verhalten? Oder liegt das an meiner Methode die Medien anzulegen? Anbei wie es aktuell läuft:
apiClient = $apiClient;
$this->mediaService = $mediaService;
$this->fileFetcher = $fileFetcher;
$this->mediaRepository = $mediaRepository;
$this->mediaFolderRepository = $mediaFolderRepository;
$this->fileSaver = $fileSaver;
}
public function getMediaFile($url) {
$id = md5('SNW_' . $url, false);
$medias = $this->mediaRepository->search((new Criteria())->addFilter(new EqualsFilter('fileName', $id)),Context::createDefaultContext());
if ($medias->count()){
return ['mediaId' => $medias->first()->getId(), 'position' => $position];
}else{
// get fileinfos
$fileExtension = pathinfo($url, PATHINFO_EXTENSION);
$fileName = basename($url, '.'.$fileExtension);
try{
$mediaFile = $this->fetchFileFromURL($url, $fileExtension);
}catch(\Exception $ex){
return false;
}
$mediaId = $this->saveFile(
$mediaFile,
$fileName,
Context::createDefaultContext(),
'product',
$id
);
return ['mediaId' => $mediaId, 'position' => $position];
}
}
public function saveFile(
MediaFile $mediaFile,
string $filename,
Context $context,
?string $folder = null,
?string $mediaId = null
): string {
$mediaId = $this->createMediaInFolder($folder, $context, $mediaId);
$this->fileSaver->persistFileToMedia($mediaFile, $filename, $mediaId, $context);
return $mediaId;
}
public function createMediaInFolder(string $folder, Context $context, string $id): string
{
$this->mediaRepository->upsert(
[
[
'id' => $id,
'private' => false,
'mediaFolderId' => $this->getMediaDefaultFolderId($folder, $context),
],
],
$context
);
return $id;
}
private function getMediaDefaultFolderId(string $folder, Context $context): ?string
{
$criteria = new Criteria();
$criteria->addFilter(new EqualsFilter('media_folder.defaultFolder.entity', $folder));
$criteria->addAssociation('defaultFolder');
$criteria->setLimit(1);
$defaultFolder = $this->mediaFolderRepository->search($criteria, $context);
$defaultFolderId = null;
if ($defaultFolder->count() === 1) {
$defaultFolderId = $defaultFolder->first()->getId();
}
return $defaultFolderId;
}
private function fetchFileFromURL(string $url, string $extension): MediaFile
{
$request = new Request();
$request->query->set('url', $url);
$request->query->set('extension', $extension);
$request->request->set('url', $url);
$request->request->set('extension', $extension);
$request->headers->set('content-type', 'application/json');
return $this->mediaService->fetchFile($request);
}
}