Create reference for media.

@param MediaInterface $media

@return bool
197939
public function createReference(MediaInterface $media)
    {
        $allowed = false;
        foreach ($this->contexts as $context) {
            if (in_array($media->getExtension(), $context)) {
                $allowed = true;
                break;
            }
        }

        if ($allowed === false) {
            $this->error = array('error.extensionNotAllowed', array('%extension%' => $media->getExtension()));

            return false;
        }

        // secure extension for reference
        if (!in_array($media->getExtension(), $this->trustedExtensions)) {
            $media->setExtension($media->getExtension().'.txt');
            $media->setType($media::OTHER);
        }

        do {
            $media->setReference(sha1(microtime(true).mt_rand()).'.'.$media->getExtension());
        } while ($this->filesystemManipulator->has($this->originalDir.'/'.$media->getReference()));

        return true;
    }
171450
public function generateFrontUrl(MediaInterface $media)
    {
        $path = null;

        if ($media->getType() === MediaTypes::IMAGE) {
            $path = $this->cacheManager->getBrowserPath($media->getPath(), 'media_front');
        } elseif (in_array($media->getType(), [MediaTypes::VIDEO, MediaTypes::AUDIO, MediaTypes::FLASH])) {
            $path = $this->urlGenerator->generate('ekyna_media_player', ['key' => $media->getPath()], true);
        } else {
            $path = $this->urlGenerator->generate('ekyna_media_download', ['key' => $media->getPath()], true);
        }

        return $path;
    }
1182287
public function renderMedia(MediaInterface $media, array $options)
    {
        $options = $this->validateOptions($options, __METHOD__);

        return $this->render(
            'OpenOrchestraMediaBundle:RenderMedia:image.html.twig',
            array(
                'media_url' => $this->getMediaFormatUrl($media, $options['format']),
                'media_alt' => $options['alt'],
                'id'        => $options['id'],
                'class'     => $options['class'],
                'style'     => $options['style'],
                'legend'    => $options['legend']
            )
        );
    }
154805
public function generateThumbnail(MediaInterface $media)
    {
        $fileName = $media->getFilesystemName();
        $thumbnailName = '';

        $extractedImagePath = $this->videoManager->extractImageFromVideo(
            $this->tmpDir . DIRECTORY_SEPARATOR . $fileName,
            1
        );

        $thumbnailPath = $this->imageManager->generateAlternative($extractedImagePath, $this->thumbnailFormat);

        if ('' !== $thumbnailPath) {
            $thumbnailName = self::THUMBNAIL_PREFIX . '-' . pathinfo($fileName, PATHINFO_FILENAME) . '.jpg';
            $this->mediaStorageManager->uploadFile($thumbnailName, $thumbnailPath);
        }

        if (trim($extractedImagePath, DIRECTORY_SEPARATOR) !== trim($this->tmpDir, DIRECTORY_SEPARATOR)) {
            $this->fileSystem->remove(array($extractedImagePath));
        }

        $media->setThumbnail($thumbnailName);
    }
1256404
public function prepareMedia(Media $media)
    {
        $handler = $this->getHandler($media);
        $handler->prepareMedia($media);

        return $this;
    }
422283
public function generateThumbUrl(MediaInterface $media)
    {
        $path = null;

        if ($media->getType() === MediaTypes::IMAGE) {
            $path = $this->cacheManager->getBrowserPath($media->getPath(), 'media_thumb');
        } else {
            $path = $this->generateFileThumb($media);
        }

        if (null === $path) {
            $path = '/bundles/ekynamedia/img/file.jpg';
        }

        return $path;
    }
1286880
public function addMedia(\Amulen\MediaBundle\Entity\Media $medias)
    {
        $this->medias[] = $medias;

        return $this;
    }
897458
public function renderMedia(MediaInterface $media, array $options)
    {
        $options = $this->validateOptions($options, __METHOD__);

        return $this->render(
            'OpenOrchestraMediaBundle:RenderMedia:default.html.twig',
            array(
                'media_url' => $this->getFileUrl($media->getFilesystemName()),
                'media_name' => $media->getName(),
                'id' => $options['id'],
                'class' => $options['class'],
                'style' => $options['style']
            )
        );
    }
1069042
public function getImages(MediaInterface $media, array $filters)
    {
        $key = $media->getImagesCacheKey();
        if (!$images = $this->cache->fetch($key)) {
            $provider = $this->getProvider($media);

            $reference = $provider->getThumb($media);

            $images = [];
            foreach ($filters as $filter) {
                if ($media->getContentType() == 'image/svg+xml') {
                    $images[$filter] = $provider->getUrl($media);
                } else {
                    $images[$filter] = $this->cacheManager->getBrowserPath($reference, $filter);
                }
            }

            $this->cache->save($key, $images, 0);
        }

        return $images;
    }
1300909
protected function getMetadata(MediaInterface $media, $url)
    {
        try {
            $response = $this->browser->get($url);
        } catch (\RuntimeException $e) {
            throw new \RuntimeException('Unable to retrieve the video information for :'.$url, null, $e);
        }

        $metadata = json_decode($response->getContent(), true);

        if (!$metadata) {
            throw new \RuntimeException('Unable to decode the video information for :'.$url);
        }

        return $metadata;
    }
991739
public function getMedia(ConnectionInterface $con = null)
    {
        if ($this->aMedia === null && ($this->media_id != 0)) {
            $this->aMedia = ChildMediaQuery::create()->findPk($this->media_id, $con);
            /* The following can be used additionally to
                guarantee the related object contains a reference
                to this object.  This level of coupling may, however, be
                undesirable since it could result in an only partially populated collection
                in the referenced object.
                $this->aMedia->addM2Ps($this);
             */
        }

        return $this->aMedia;
    }
232940
private function cleanTranslations(MediaInterface $media)
    {
        foreach ($media->getTranslations() as $trans) {
            if (0 == strlen($trans->getTitle()) && 0 == strlen($trans->getDescription())) {
                $media->removeTranslation($trans);
            }
        }
    }
162077
protected function getMetadata(MediaInterface $media, $url)
    {
        try {
            $metadata = file_get_contents($url);
        } catch (\RuntimeException $e) {
            throw new \RuntimeException('Unable to retrieve the video information for :'.$url, null, $e);
        }

        $metadata = json_decode($metadata, true);

        if (!$metadata) {
            throw new \RuntimeException('Unable to decode the video information for :'.$url);
        }

        return $metadata;
    }
640405
public static function createFromContent(
        string $content,
        ?InternetMediaTypeInterface $contentType = null
    ): WebResourceInterface {
        $className = get_called_class();

        return new $className(WebResourceProperties::create([
            WebResourceProperties::ARG_CONTENT => $content,
            WebResourceProperties::ARG_CONTENT_TYPE => $contentType,
        ]));
    }
602989
public function save(MediaInterface $media): MediaInterface
    {
        $data = $this->serializer()->serialize($media);

        if ($media->getId() !== 0) {
            $this->shopware()
                 ->put('/api/media/'.(string)$media->getId(), [], $data);
        } else {
            $response = $this->shopware()->post('/api/media', [], $data);
            $data     = json_decode((string)$response->getBody(), true);
            $media->setId((int)$data['data']['id']);
        }

        return $media;
    }
1198871
protected function generateBinaryFromRequest(MediaInterface $media)
    {
        if (!$media->getContentType()) {
            throw new \RuntimeException(
                'You must provide the content type value for your media before setting the binary content'
            );
        }

        $request = $media->getBinaryContent();

        if (!$request instanceof Request) {
            throw new \RuntimeException('Expected Request in binary content');
        }

        $content = $request->getContent();

        // create unique id for media reference
        $guesser = ExtensionGuesser::getInstance();
        $extension = $guesser->guess($media->getContentType());

        if (!$extension) {
            throw new \RuntimeException(
                sprintf('Unable to guess extension for content type %s', $media->getContentType())
            );
        }

        $handle = tmpfile();
        fwrite($handle, $content);
        $file = new ApiMediaFile($handle);
        $file->setExtension($extension);
        $file->setMimetype($media->getContentType());

        $media->setBinaryContent($file);
    }
693369
public function setMedia($media)
	{
		$counter = 0;
		$attachment = $media->getMedia();
		if (is_resource($attachment)) {
			$this->addMultipart("attachment$counter", $attachment);
			$media->setMedia("attach://attachment$counter");
			$counter++;
		}

		$this->media = json_encode($media->toObject());
	}