View source
<?php
namespace Drupal\media_entity_soundcloud\Plugin\media\Source;
use Drupal\Component\Serialization\Json;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\media\MediaInterface;
use Drupal\media\MediaSourceBase;
use GuzzleHttp\ClientInterface;
use GuzzleHttp\Exception\ClientException;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Field\FieldTypePluginManagerInterface;
class Soundcloud extends MediaSourceBase {
protected $soundcloud;
protected $configFactory;
protected $httpClient;
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, EntityFieldManagerInterface $entity_field_manager, FieldTypePluginManagerInterface $field_type_manager, ConfigFactoryInterface $config_factory, ClientInterface $httpClient) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $entity_type_manager, $entity_field_manager, $field_type_manager, $config_factory);
$this->httpClient = $httpClient;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('entity_type.manager'), $container
->get('entity_field.manager'), $container
->get('plugin.manager.field.field_type'), $container
->get('config.factory'), $container
->get('http_client'));
}
public function getMetadataAttributes() {
$attributes = [
'track_id' => $this
->t('The track id - not always available'),
'playlist_id' => $this
->t('The playlist (set) id - not always available'),
'source_id' => t('Compound of source type (track or playlist) and id so that it is unique among all SoundCloud media'),
'html' => $this
->t('HTML embed code'),
'thumbnail_uri' => t('URI of the thumbnail'),
];
return $attributes;
}
public function getMetadata(MediaInterface $media, $attribute_name) {
$content_url = $this
->getMediaUrl($media);
if ($content_url === FALSE) {
return FALSE;
}
$data = $this
->oEmbed($content_url);
if ($data === FALSE) {
return FALSE;
}
switch ($attribute_name) {
case 'html':
return $data['html'];
case 'thumbnail_uri':
if (isset($data['thumbnail_url'])) {
$destination = $this->configFactory
->get('media_entity_soundcloud.settings')
->get('thumbnail_destination');
$local_uri = $destination . '/' . pathinfo($data['thumbnail_url'], PATHINFO_BASENAME);
if (!file_exists($local_uri)) {
file_prepare_directory($destination, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS);
$image = file_get_contents($data['thumbnail_url']);
file_unmanaged_save_data($image, $local_uri, FILE_EXISTS_REPLACE);
}
return $local_uri;
}
return parent::getMetadata($media, $attribute_name);
case 'track_id':
case 'playlist_id':
case 'source_id':
preg_match('/src="([^"]+)"/', $data['html'], $src_matches);
if (!count($src_matches)) {
return FALSE;
}
preg_match('#/(tracks|playlists)/(\\d+)#', urldecode($src_matches[1]), $matches);
if (!count($matches)) {
return FALSE;
}
if ($attribute_name == 'source_id') {
return $matches[1] . '/' . $matches[2];
}
elseif ($attribute_name == 'track_id' && $matches[1] == 'tracks' || $attribute_name == 'playlist_id' && $matches[1] == 'playlists') {
return $matches[2];
}
return FALSE;
default:
return parent::getMetadata($media, $attribute_name);
}
}
protected function getMediaUrl(MediaInterface $media) {
$source_field = $this
->getSourceFieldDefinition($media->bundle->entity);
$field_name = $source_field
->getName();
if ($media
->hasField($field_name)) {
$property_name = $source_field
->getFieldStorageDefinition()
->getMainPropertyName();
return $media->{$field_name}->{$property_name};
}
return FALSE;
}
protected function oEmbed($url) {
$this->soundcloud =& drupal_static(__FUNCTION__ . hash('md5', $url));
if (!isset($this->soundcloud)) {
$url = 'https://soundcloud.com/oembed?format=json&url=' . $url;
try {
$response = $this->httpClient
->get($url);
$this->soundcloud = Json::decode((string) $response
->getBody());
} catch (ClientException $e) {
$this->soundcloud = FALSE;
}
}
return $this->soundcloud;
}
}