public function testGetMimeTypeExtension()
 {
     $this->assertEquals("jpg", $this->model->getMimeTypeExtension("image/jpeg"));
     $this->assertEquals("jpg", $this->model->getMimeTypeExtension("image/jpg"));
     $this->assertEquals("png", $this->model->getMimeTypeExtension("image/png"));
     $this->assertEquals("gif", $this->model->getMimeTypeExtension("image/gif"));
     $this->assertEquals("", $this->model->getMimeTypeExtension("unknown"));
     $this->assertEquals("", $this->model->getMimeTypeExtension(null));
 }
 /**
  * @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;
 }