View source
<?php
namespace Drupal\media_entity_instagram\Plugin\media\Source;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Field\FieldTypePluginManagerInterface;
use Drupal\Core\File\FileSystemInterface;
use Drupal\media\MediaInterface;
use Drupal\media\MediaSourceBase;
use Drupal\media\MediaSourceFieldConstraintsInterface;
use Drupal\media_entity_instagram\InstagramEmbedFetcher;
use GuzzleHttp\Client;
use Symfony\Component\DependencyInjection\ContainerInterface;
class Instagram extends MediaSourceBase implements MediaSourceFieldConstraintsInterface {
protected $fetcher;
protected $httpClient;
protected $fileSystem;
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, EntityFieldManagerInterface $entity_field_manager, ConfigFactoryInterface $config_factory, FieldTypePluginManagerInterface $field_type_manager, InstagramEmbedFetcher $fetcher, Client $httpClient, FileSystemInterface $fileSystem) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $entity_type_manager, $entity_field_manager, $field_type_manager, $config_factory);
$this->fetcher = $fetcher;
$this->httpClient = $httpClient;
$this->fileSystem = $fileSystem;
}
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('config.factory'), $container
->get('plugin.manager.field.field_type'), $container
->get('media_entity_instagram.instagram_embed_fetcher'), $container
->get('http_client'), $container
->get('file_system'));
}
public static $validationRegexp = [
'@((http|https):){0,1}//(www\\.){0,1}instagram\\.com/p/(?<shortcode>[a-z0-9_-]+)@i' => 'shortcode',
'@((http|https):){0,1}//(www\\.){0,1}instagr\\.am/p/(?<shortcode>[a-z0-9_-]+)@i' => 'shortcode',
'@((http|https):){0,1}//(www\\.){0,1}instagram\\.com/tv/(?<shortcode>[a-z0-9_-]+)@i' => 'shortcode',
'@((http|https):){0,1}//(www\\.){0,1}instagr\\.am/tv/(?<shortcode>[a-z0-9_-]+)@i' => 'shortcode',
];
public function getMetadataAttributes() {
return [
'shortcode' => $this
->t('Instagram shortcode'),
'id' => $this
->t('Media ID'),
'type' => $this
->t('Media type: image or video'),
'thumbnail' => $this
->t('Link to the thumbnail'),
'thumbnail_local' => $this
->t("Copies thumbnail locally and return it's URI"),
'thumbnail_local_uri' => $this
->t('Returns local URI of the thumbnail'),
'username' => $this
->t('Author of the post'),
'caption' => $this
->t('Caption'),
];
}
public function getMetadata(MediaInterface $media, $attribute_name) {
if ($attribute_name == 'default_name') {
$username = $this
->getMetadata($media, 'username');
$id = $this
->getMetadata($media, 'id');
if ($username && $id) {
return $username . ' - ' . $id;
}
else {
$code = $this
->getMetadata($media, 'shortcode');
if (!empty($code)) {
return $code;
}
}
return parent::getMetadata($media, 'default_name');
}
elseif ($attribute_name == 'thumbnail_uri') {
return $this
->getMetadata($media, 'thumbnail_local');
}
$matches = $this
->matchRegexp($media);
if (!$matches['shortcode']) {
return FALSE;
}
if ($attribute_name == 'shortcode') {
return $matches['shortcode'];
}
if ($instagram = $this->fetcher
->fetchInstagramEmbed($matches['shortcode'])) {
switch ($attribute_name) {
case 'id':
if (isset($instagram['media_id'])) {
return $instagram['media_id'];
}
return FALSE;
case 'type':
if (isset($instagram['type'])) {
return $instagram['type'];
}
return FALSE;
case 'thumbnail':
return 'http://instagram.com/p/' . $matches['shortcode'] . '/media/?size=m';
case 'thumbnail_local':
$local_uri = $this
->getMetadata($media, 'thumbnail_local_uri');
if ($local_uri) {
if (file_exists($local_uri)) {
return $local_uri;
}
else {
$directory = dirname($local_uri);
$this->fileSystem
->prepareDirectory($directory, FileSystemInterface::CREATE_DIRECTORY | FileSystemInterface::MODIFY_PERMISSIONS);
$image_url = $this
->getMetadata($media, 'thumbnail');
$response = $this->httpClient
->get($image_url);
if ($response
->getStatusCode() == 200) {
return $this->fileSystem
->saveData($response
->getBody(), $local_uri, FileSystemInterface::EXISTS_REPLACE);
}
}
}
return FALSE;
case 'thumbnail_local_uri':
if (isset($instagram['thumbnail_url'])) {
return $this->configFactory
->get('media_entity_instagram.settings')
->get('local_images') . '/' . $matches['shortcode'] . '.' . pathinfo(parse_url($instagram['thumbnail_url'], PHP_URL_PATH), PATHINFO_EXTENSION);
}
return FALSE;
case 'username':
if (isset($instagram['author_name'])) {
return $instagram['author_name'];
}
return FALSE;
case 'caption':
if (isset($instagram['title'])) {
return $instagram['title'];
}
return FALSE;
}
}
return FALSE;
}
public function getSourceFieldConstraints() {
return [
'InstagramEmbedCode' => [],
];
}
protected function matchRegexp(MediaInterface $media) {
$matches = [];
if (isset($this->configuration['source_field'])) {
$source_field = $this->configuration['source_field'];
if ($media
->hasField($source_field)) {
$property_name = $media->{$source_field}
->first()
->mainPropertyName();
foreach (static::$validationRegexp as $pattern => $key) {
if (preg_match($pattern, $media->{$source_field}->{$property_name}, $matches)) {
return $matches;
}
}
}
}
return FALSE;
}
}