5 * Contains \Drupal\video\StreamWrapper\VideoRemoteStreamWrapper.
8 namespace Drupal\video\StreamWrapper;
10 use Drupal\Core\StreamWrapper\ReadOnlyStream;
11 use Drupal\Core\StreamWrapper\StreamWrapperInterface;
12 use Drupal\Component\Utility\UrlHelper;
15 * Defines a video read only stream wrapper class.
17 * Provides support for reading remotely accessible files with the Drupal file
20 abstract class VideoRemoteStreamWrapper extends ReadOnlyStream {
22 protected static $base_url = NULL;
23 protected $parameters = array();
28 public static function getType() {
29 return StreamWrapperInterface::READ;
33 * Finds and returns the base URL for read only stream.
35 * The external base URL
37 public static function baseUrl() {
38 return self::$base_url;
44 public function getDirectoryPath() {
45 return static::basePath();
51 public function getExternalUrl() {
52 $path = str_replace('\\', '/', $this->getTarget());
53 return static::baseUrl() . '/' . UrlHelper::encodePath($path);
57 * Returns the base path for stream wrapper.
59 public static function basePath(\SplString $site_path = NULL) {
60 return static::baseUrl();
66 function setUri($uri) {
71 * Returns the local writable target of the resource within the stream.
73 * This function should be used in place of calls to realpath() or similar
74 * functions when attempting to determine the location of a file. While
75 * functions like realpath() may return the location of a read-only file, this
76 * method may return a URI or path suitable for writing that is completely
77 * separate from the URI used for reading.
83 * Returns a string representing a location suitable for writing of a file,
84 * or FALSE if unable to write to the file such as with read-only streams.
86 protected function getTarget($uri = NULL) {
91 list(, $target) = explode('://', $uri, 2);
93 // Remove erroneous leading or trailing, forward-slashes and backslashes.
94 return trim($target, '\/');
98 * Returns the canonical absolute path of the URI, if possible.
101 * (optional) The stream wrapper URI to be converted to a canonical
102 * absolute path. This may point to a directory or another type of file.
104 * @return string|bool
105 * If $uri is not set, returns the canonical absolute path of the URI
106 * previously set by the
107 * Drupal\Core\StreamWrapper\StreamWrapperInterface::setUri() function.
108 * If $uri is set and valid for this class, returns its canonical absolute
109 * path, as determined by the realpath() function. If $uri is set but not
110 * valid, returns FALSE.
112 protected function getLocalPath($uri = NULL) {
116 $path = $this->getDirectoryPath() . '/' . $this->getTarget($uri);
118 // In PHPUnit tests, the base path for local streams may be a virtual
119 // filesystem stream wrapper URI, in which case this local stream acts like
120 // a proxy. realpath() is not supported by vfsStream, because a virtual
121 // file system does not have a real filepath.
122 if (strpos($path, 'vfs://') === 0) {
126 $realpath = realpath($path);
128 // This file does not yet exist.
129 $realpath = realpath(dirname($path)) . '/' . drupal_basename($path);
131 $directory = realpath($this->getDirectoryPath());
132 if (!$realpath || !$directory || strpos($realpath, $directory) !== 0) {
141 function realpath() {
142 return $this->getLocalPath();
146 * Gets the name of the directory from a given path.
148 * This method is usually accessed through drupal_dirname(), which wraps
149 * around the PHP dirname() function because it does not support stream
156 * A string containing the directory name.
158 * @see drupal_dirname()
160 public function dirname($uri = NULL) {
161 list($scheme) = explode('://', $uri, 2);
162 $target = $this->getTarget($uri);
163 $dirname = dirname($target);
165 if ($dirname == '.') {
169 return $scheme . '://' . $dirname;
173 * Support for closedir().
178 * @see http://php.net/manual/streamwrapper.dir-closedir.php
180 public function dir_closedir() {
181 closedir($this->handle);
182 // We do not really have a way to signal a failure as closedir() does not
183 // have a return value.
188 * Support for opendir().
191 * A string containing the URI to the directory to open.
192 * @param int $options
193 * Unknown (parameter is not documented in PHP Manual).
198 * @see http://php.net/manual/streamwrapper.dir-opendir.php
200 public function dir_opendir($uri, $options) {
202 $this->handle = opendir($this->getLocalPath());
204 return (bool) $this->handle;
208 * Support for readdir().
211 * The next filename, or FALSE if there are no more files in the directory.
213 * @see http://php.net/manual/streamwrapper.dir-readdir.php
215 public function dir_readdir() {
216 return readdir($this->handle);
220 * Support for rewinddir().
225 * @see http://php.net/manual/streamwrapper.dir-rewinddir.php
227 public function dir_rewinddir() {
228 rewinddir($this->handle);
229 // We do not really have a way to signal a failure as rewinddir() does not
230 // have a return value and there is no way to read a directory handler
231 // without advancing to the next file.
238 public function stream_cast($cast_as) {
239 return $this->handle ? $this->handle : FALSE;
243 * Support for fclose().
246 * TRUE if stream was successfully closed.
248 * @see http://php.net/manual/streamwrapper.stream-close.php
250 public function stream_close() {
251 return fclose($this->handle);
255 * Support for feof().
258 * TRUE if end-of-file has been reached.
260 * @see http://php.net/manual/streamwrapper.stream-eof.php
262 public function stream_eof() {
263 return feof($this->handle);
267 * Support for fread(), file_get_contents() etc.
270 * Maximum number of bytes to be read.
272 * @return string|bool
273 * The string that was read, or FALSE in case of an error.
275 * @see http://php.net/manual/streamwrapper.stream-read.php
277 public function stream_read($count) {
278 return fread($this->handle, $count);
284 public function stream_seek($offset, $whence = SEEK_SET) {
285 // fseek returns 0 on success and -1 on a failure.
286 // stream_seek 1 on success and 0 on a failure.
287 return !fseek($this->handle, $offset, $whence);
293 * Since Windows systems do not allow it and it is not needed for most use
294 * cases anyway, this method is not supported on local files and will trigger
295 * an error and return false. If needed, custom subclasses can provide
296 * OS-specific implementations for advanced use cases.
298 public function stream_set_option($option, $arg1, $arg2) {
299 trigger_error('stream_set_option() not supported for local file based stream wrappers', E_USER_WARNING);
304 * Support for fstat().
307 * An array with file status, or FALSE in case of an error - see fstat()
308 * for a description of this array.
310 * @see http://php.net/manual/streamwrapper.stream-stat.php
312 public function stream_stat() {
313 return fstat($this->handle);
317 * Support for ftell().
320 * The current offset in bytes from the beginning of file.
322 * @see http://php.net/manual/streamwrapper.stream-tell.php
324 public function stream_tell() {
325 return ftell($this->handle);
329 * Support for stat().
332 * A string containing the URI to get information about.
334 * A bit mask of STREAM_URL_STAT_LINK and STREAM_URL_STAT_QUIET.
337 * An array with file status, or FALSE in case of an error - see fstat()
338 * for a description of this array.
340 * @see http://php.net/manual/streamwrapper.url-stat.php
342 public function url_stat($uri, $flags) {
344 $path = $this->getLocalPath();
345 // Suppress warnings if requested or if the file or directory does not
346 // exist. This is consistent with PHP's plain filesystem stream wrapper.
347 if ($flags & STREAM_URL_STAT_QUIET || !file_exists($path)) {