From 47855bc62e402a44af39116f3341de704800a043 Mon Sep 17 00:00:00 2001 From: upchuk Date: Wed, 24 Apr 2019 07:44:39 +0200 Subject: [PATCH 1/2] OPENEUROPA-1844: Using the remote stream wrapper module for the AV Portal photo stream wrapper. --- composer.json | 7 + media_avportal.info.yml | 1 + .../AvPortalPhotoStreamWrapper.php | 199 +++--------------- 3 files changed, 40 insertions(+), 167 deletions(-) diff --git a/composer.json b/composer.json index 268ed21..8109117 100644 --- a/composer.json +++ b/composer.json @@ -12,10 +12,12 @@ "require-dev": { "composer/installers": "~1.5", "drupal-composer/drupal-scaffold": "~2.5.2", + "cweagans/composer-patches": "^1.6", "drupal/config_devel": "~1.2", "drupal/console": "~1.0", "drupal/ctools": "^3.0", "drupal/drupal-extension": "~4.0", + "drupal/remote_stream_wrapper": "^1.2", "drush/drush": "~9.0", "nikic/php-parser": "^3.1.5", "openeuropa/code-review": "~1.0@beta", @@ -45,6 +47,11 @@ } }, "extra": { + "patches": { + "drupal/remote_stream_wrapper": { + "https://www.drupal.org/project/remote_stream_wrapper/issues/3050183": "https://www.drupal.org/files/issues/2019-04-23/3050183-2.patch" + } + }, "composer-exit-on-patch-failure": true, "enable-patching": true, "installer-paths": { diff --git a/media_avportal.info.yml b/media_avportal.info.yml index f2cfa84..74ea5a8 100644 --- a/media_avportal.info.yml +++ b/media_avportal.info.yml @@ -5,3 +5,4 @@ package: Media core: 8.x dependencies: - drupal:media + - remote_stream_wrapper:remote_stream_wrapper diff --git a/src/StreamWrapper/AvPortalPhotoStreamWrapper.php b/src/StreamWrapper/AvPortalPhotoStreamWrapper.php index c289a4b..6e51872 100644 --- a/src/StreamWrapper/AvPortalPhotoStreamWrapper.php +++ b/src/StreamWrapper/AvPortalPhotoStreamWrapper.php @@ -6,13 +6,14 @@ use Drupal\Core\StreamWrapper\StreamWrapperInterface; use Drupal\Core\StringTranslation\StringTranslationTrait; +use Drupal\remote_stream_wrapper\StreamWrapper\HttpStreamWrapper; /** * Stream wrapper for the remote AV Portal photos. * * @codingStandardsIgnoreStart PSR1.Methods.CamelCapsMethodName */ -class AvPortalPhotoStreamWrapper implements StreamWrapperInterface { +class AvPortalPhotoStreamWrapper extends HttpStreamWrapper { use StringTranslationTrait; @@ -35,7 +36,7 @@ class AvPortalPhotoStreamWrapper implements StreamWrapperInterface { * * @var \Drupal\Core\Config\ImmutableConfig */ - protected $config; + protected $configuration; /** * The resource handle. @@ -50,7 +51,7 @@ class AvPortalPhotoStreamWrapper implements StreamWrapperInterface { public function __construct() { // Dependency injection does not work with stream wrappers. $this->client = \Drupal::service('media_avportal.client'); - $this->config = \Drupal::configFactory()->get('media_avportal.settings'); + $this->configuration = \Drupal::configFactory()->get('media_avportal.settings'); } /** @@ -105,7 +106,7 @@ public function getExternalUrl() { return NULL; } - return $this->config->get('photos_base_uri') . $resource->getPhotoUri(); + return $this->configuration->get('photos_base_uri') . $resource->getPhotoUri(); } /** @@ -130,161 +131,9 @@ public function stream_open($path, $mode, $options, &$opened_path) { return FALSE; } - $parsed = parse_url($url); - if (!isset($parsed['scheme'])) { - $url = 'https:' . $url; - } - - $this->handle = ($options && STREAM_REPORT_ERRORS) ? fopen($url, $mode) : @fopen($url, $mode); + $url = $this->getFullExternalUrl($url); - return (bool) $this->handle; - } - - /** - * {@inheritdoc} - */ - public function dir_closedir() { - return FALSE; - } - - /** - * {@inheritdoc} - */ - public function dir_opendir($path, $options) { - return FALSE; - } - - /** - * {@inheritdoc} - */ - public function dir_readdir() { - return FALSE; - } - - /** - * {@inheritdoc} - */ - public function dir_rewinddir() { - return FALSE; - } - - /** - * {@inheritdoc} - */ - public function mkdir($path, $mode, $options) { - return FALSE; - } - - /** - * {@inheritdoc} - */ - public function rename($path_from, $path_to) { - return FALSE; - } - - /** - * {@inheritdoc} - */ - public function rmdir($path, $options) { - return FALSE; - } - - /** - * {@inheritdoc} - */ - public function stream_cast($cast_as) { - return FALSE; - } - - /** - * {@inheritdoc} - */ - public function stream_close() { - return fclose($this->handle); - } - - /** - * {@inheritdoc} - */ - public function stream_eof() { - return feof($this->handle); - } - - /** - * {@inheritdoc} - */ - public function stream_flush() { - return FALSE; - } - - /** - * {@inheritdoc} - */ - public function stream_lock($operation) { - return FALSE; - } - - /** - * {@inheritdoc} - */ - public function stream_metadata($path, $option, $value) { - return FALSE; - } - - /** - * {@inheritdoc} - */ - public function stream_read($count) { - return fread($this->handle, $count); - } - - /** - * {@inheritdoc} - */ - public function stream_seek($offset, $whence = SEEK_SET) { - return FALSE; - } - - /** - * {@inheritdoc} - */ - public function stream_set_option($option, $arg1, $arg2) { - return FALSE; - } - - /** - * {@inheritdoc} - */ - public function stream_stat() { - return FALSE; - } - - /** - * {@inheritdoc} - */ - public function stream_tell() { - return FALSE; - } - - /** - * {@inheritdoc} - */ - public function stream_truncate($new_size) { - return FALSE; - } - - /** - * {@inheritdoc} - */ - public function stream_write($data) { - return FALSE; - } - - /** - * {@inheritdoc} - */ - public function unlink($path) { - return FALSE; + return parent::stream_open($url, $mode, $options, $opened_path); } /** @@ -300,19 +149,28 @@ public function url_stat($path, $flags) { return FALSE; } - // We don't have any information about the file since it is remote. So we - // just return an array with a single value to indicate that the file is - // actually there. - return [ - 0 => TRUE, - ]; + $url = $this->configuration->get('photos_base_uri') . $resource->getPhotoUri(); + $url = $this->getFullExternalUrl($url); + + return parent::url_stat($url, $flags); } /** - * {@inheritdoc} + * Appends the scheme to the external URL retrieved by getExternalUrl(). + * + * @param string $url + * The external URL. + * + * @return string + * The full external URL. */ - public function dirname($uri = NULL) { - return FALSE; + protected function getFullExternalUrl(string $url): string { + $parsed = parse_url($url); + if (!isset($parsed['scheme'])) { + $url = 'https:' . $url; + } + + return $url; } /** @@ -334,4 +192,11 @@ protected function getTarget($uri = NULL) { return trim($target, '\/'); } + /** + * {@inheritdoc} + */ + public function preventUnmanagedFileImageStyleGeneration() { + return FALSE; + } + } From 152f427e43212b30f29cb77e6b206d7619ac1d9c Mon Sep 17 00:00:00 2001 From: upchuk Date: Wed, 24 Apr 2019 09:17:53 +0200 Subject: [PATCH 2/2] OPENEUROPA-1844: Fixing tests. --- composer.json | 2 +- tests/src/Kernel/AvPortalStreamWrapperTest.php | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 8109117..5272d4f 100644 --- a/composer.json +++ b/composer.json @@ -21,7 +21,7 @@ "drush/drush": "~9.0", "nikic/php-parser": "^3.1.5", "openeuropa/code-review": "~1.0@beta", - "openeuropa/drupal-core-require-dev": "^8.6", + "openeuropa/drupal-core-require-dev": "^8.6.4", "openeuropa/task-runner": "~1.0@beta", "phpunit/phpunit": "~6.0" }, diff --git a/tests/src/Kernel/AvPortalStreamWrapperTest.php b/tests/src/Kernel/AvPortalStreamWrapperTest.php index 07cba50..d95b421 100644 --- a/tests/src/Kernel/AvPortalStreamWrapperTest.php +++ b/tests/src/Kernel/AvPortalStreamWrapperTest.php @@ -17,6 +17,7 @@ class AvPortalStreamWrapperTest extends KernelTestBase { */ protected static $modules = [ 'system', + 'remote_stream_wrapper', 'media_avportal', 'media_avportal_mock', ];