/** * {@inheritdoc} */ public function create($sku, ProductAttributeMediaGalleryEntryInterface $entry, $storeId = 0) { try { $this->storeManager->getStore($storeId); } catch (\Exception $exception) { throw new NoSuchEntityException(__('There is no store with provided ID.')); } /** @var $entry ProductAttributeMediaGalleryEntryInterface */ $entryContent = $entry->getContent(); if (!$this->contentValidator->isValid($entryContent)) { throw new InputException(__('The image content is not valid.')); } $product = $this->productRepository->get($sku); $existingMediaGalleryEntries = $product->getMediaGalleryEntries(); $existingEntryIds = []; if ($existingMediaGalleryEntries == null) { $existingMediaGalleryEntries = [$entry]; } else { foreach ($existingMediaGalleryEntries as $existingEntries) { $existingEntryIds[$existingEntries->getId()] = $existingEntries->getId(); } $existingMediaGalleryEntries[] = $entry; } $product->setMediaGalleryEntries($existingMediaGalleryEntries); try { $product = $this->productRepository->save($product); } catch (InputException $inputException) { throw $inputException; } catch (\Exception $e) { throw new StateException(__('Cannot save product.')); } foreach ($product->getMediaGalleryEntries() as $entry) { if (!isset($existingEntryIds[$entry->getId()])) { return $entry->getId(); } } throw new StateException(__('Failed to save new media gallery entry.')); }
/** * {@inheritdoc} */ public function create($product) { try { $this->storeManager->getStore($product->getStoreId()); } catch (\Exception $exception) { throw new NoSuchEntityException(__('There is no store with provided ID.')); } /** @var $entry ProductAttributeMediaGalleryEntryInterface */ $entry = $product->getCustomAttribute('media_gallery')->getValue(); $entryContent = $entry->getContent(); if (!$this->contentValidator->isValid($entryContent)) { throw new InputException(__('The image content is not valid.')); } $product = $this->productRepository->get($product->getSku()); $fileContent = @base64_decode($entryContent->getEntryData(), true); $mediaTmpPath = $this->mediaConfig->getBaseTmpMediaPath(); $mediaDirectory = $this->filesystem->getDirectoryWrite(DirectoryList::MEDIA); $mediaDirectory->create($mediaTmpPath); $fileName = $entryContent->getName() . '.' . $this->mimeTypeExtensionMap[$entryContent->getMimeType()]; $relativeFilePath = $mediaTmpPath . DIRECTORY_SEPARATOR . $fileName; $absoluteFilePath = $mediaDirectory->getAbsolutePath($relativeFilePath); $mediaDirectory->writeFile($relativeFilePath, $fileContent); /** @var $productMediaGallery \Magento\Catalog\Model\Product\Attribute\Backend\Media */ $productMediaGallery = $this->getGalleryAttributeBackend($product); $imageFileUri = $productMediaGallery->addImage($product, $absoluteFilePath, $entry->getTypes(), true, $entry->isDisabled()); // Update additional fields that are still empty after addImage call $productMediaGallery->updateImage($product, $imageFileUri, ['label' => $entry->getLabel(), 'position' => $entry->getPosition(), 'disabled' => $entry->isDisabled()]); try { $this->productRepository->save($product); } catch (\Exception $e) { throw new StateException(__('Cannot save product.')); } // Remove all temporary files $mediaDirectory->delete($relativeFilePath); // File could change its name during the move from tmp dir return $this->entryResolver->getEntryIdByFilePath($product, $productMediaGallery->getRenamedImage($imageFileUri)); }
/** * @param ProductInterface $product * @param array $newEntry * @return $this * @throws InputException * @throws StateException * @throws \Magento\Framework\Exception\LocalizedException */ protected function processNewMediaGalleryEntry(ProductInterface $product, array $newEntry) { /** @var ProductAttributeMediaGalleryEntryContentInterface $contentDataObject */ $contentDataObject = $newEntry['content']; if (!$this->contentValidator->isValid($contentDataObject)) { throw new InputException(__('The image content is not valid.')); } $fileContent = @base64_decode($contentDataObject->getEntryData(), true); $fileName = $contentDataObject->getName(); $mimeType = $contentDataObject->getMimeType(); /** @var \Magento\Catalog\Model\Product\Media\Config $mediaConfig */ $mediaConfig = $product->getMediaConfig(); $mediaTmpPath = $mediaConfig->getBaseTmpMediaPath(); $mediaDirectory = $this->fileSystem->getDirectoryWrite(DirectoryList::MEDIA); $mediaDirectory->create($mediaTmpPath); $fileName = $fileName . '.' . $this->mimeTypeExtensionMap->getMimeTypeExtension($mimeType); $relativeFilePath = $mediaTmpPath . DIRECTORY_SEPARATOR . $fileName; $absoluteFilePath = $mediaDirectory->getAbsolutePath($relativeFilePath); $mediaDirectory->writeFile($relativeFilePath, $fileContent); /** @var \Magento\Catalog\Model\Product\Attribute\Backend\Media $galleryAttributeBackend */ $galleryAttributeBackend = $product->getGalleryAttributeBackend(); if ($galleryAttributeBackend == null) { throw new StateException(__('Requested product does not support images.')); } $imageFileUri = $galleryAttributeBackend->addImage($product, $absoluteFilePath, isset($newEntry['types']) ? $newEntry['types'] : [], true, isset($newEntry['disabled']) ? $newEntry['disabled'] : true); // Update additional fields that are still empty after addImage call $galleryAttributeBackend->updateImage($product, $imageFileUri, ['label' => $newEntry['label'], 'position' => $newEntry['position'], 'disabled' => $newEntry['disabled']]); return $this; }