Skip to content
This repository has been archived by the owner on Jan 10, 2025. It is now read-only.

Commit

Permalink
Merge pull request #19 from openeuropa/OPENEUROPA-1844
Browse files Browse the repository at this point in the history
OPENEUROPA-1844: Fixing AV Portal stream wrapper
  • Loading branch information
upchuk authored Apr 24, 2019
2 parents 497ecff + 152f427 commit 365bc89
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 168 deletions.
9 changes: 8 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,16 @@
"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",
"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"
},
Expand All @@ -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": {
Expand Down
1 change: 1 addition & 0 deletions media_avportal.info.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ package: Media
core: 8.x
dependencies:
- drupal:media
- remote_stream_wrapper:remote_stream_wrapper
199 changes: 32 additions & 167 deletions src/StreamWrapper/AvPortalPhotoStreamWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -35,7 +36,7 @@ class AvPortalPhotoStreamWrapper implements StreamWrapperInterface {
*
* @var \Drupal\Core\Config\ImmutableConfig
*/
protected $config;
protected $configuration;

/**
* The resource handle.
Expand All @@ -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');
}

/**
Expand Down Expand Up @@ -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();
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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;
}

/**
Expand All @@ -334,4 +192,11 @@ protected function getTarget($uri = NULL) {
return trim($target, '\/');
}

/**
* {@inheritdoc}
*/
public function preventUnmanagedFileImageStyleGeneration() {
return FALSE;
}

}
1 change: 1 addition & 0 deletions tests/src/Kernel/AvPortalStreamWrapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class AvPortalStreamWrapperTest extends KernelTestBase {
*/
protected static $modules = [
'system',
'remote_stream_wrapper',
'media_avportal',
'media_avportal_mock',
];
Expand Down

0 comments on commit 365bc89

Please sign in to comment.