PluginProbe
WP-Stateless – Google Cloud Storage / 3.0
WP-Stateless – Google Cloud Storage v3.0
4.4.3 2.1.7 2.1.8 2.1.9 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 2.2.7 2.3.0 2.3.1 2.3.2 3.0 3.0.1 3.0.2 3.0.3 3.0.4 3.1.0 3.1.1 3.2.0 3.2.1 3.2.2 All 62 releases
wp-stateless / lib / classes / class-gs-stream-wrapper.php

class-gs-stream-wrapper.php in WP-Stateless – Google Cloud Storage 3.0, at lib/classes/class-gs-stream-wrapper.php

141 lines 4.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /* copied from lib\Google\vendor\google\cloud-storage\src\StreamWrapper.php */
3
4 /**
5 * A streamWrapper implementation for handling `gs://bucket/path/to/file.jpg`.
6 * Note that you can only open a file with mode 'r', 'rb', 'rb', 'w', 'wb', or 'wt'.
7 *
8 * See: http://php.net/manual/en/class.streamwrapper.php
9 */
10
11 namespace wpCloud\StatelessMedia {
12
13 use Google\Cloud\Storage\StorageClient;
14
15 if (!class_exists('wpCloud\StatelessMedia\StreamWrapper')) {
16 class StreamWrapper extends \Google\Cloud\Storage\StreamWrapper
17 {
18 /**
19 * @var string Protocol used to open this stream
20 */
21 private $protocol;
22
23 /**
24 * @var Bucket Reference to the bucket the opened file
25 * lives in or will live in.
26 */
27 private $bucket;
28
29 /**
30 * @var string Name of the file opened by this stream.
31 */
32 private $file;
33
34 /**
35 * Callback handler for retrieving information about a file
36 *
37 * @param string $path The URI to the file
38 * @param int $flags Bitwise mask of options
39 * @return array|bool
40 */
41 public function url_stat($path, $flags)
42 {
43 $this->_openPath($path);
44 // if root dir
45 if(empty($this->file)){
46 $stats = [];
47 // equivalent to 40777 and 40444 in octal
48 if($is_writable = $this->bucket->isWritable()){
49 $stats['mode'] = $is_writable
50 ? self::DIRECTORY_WRITABLE_MODE
51 : self::DIRECTORY_READABLE_MODE;
52 return $this->makeStatArray($stats);
53 }
54 }
55 return parent::url_stat($path, $flags);
56 }
57
58 /**
59 * Parse the URL and set protocol, filename and bucket.
60 *
61 * @param string $path URL to open
62 * @return StorageClient
63 */
64 private function _openPath($path)
65 {
66 $url = (array) parse_url($path) + [
67 'scheme' => '',
68 'path' => '',
69 'host' => ''
70 ];
71 $this->protocol = $url['scheme'];
72 $this->file = ltrim($url['path'], '/');
73 $client = self::getClient($this->protocol);
74 $this->bucket = $client->bucket($url['host']);
75 return $client;
76 }
77
78 /**
79 * Register a StreamWrapper for reading and writing to Google Storage
80 *
81 * @param StorageClient $client The StorageClient configuration to use.
82 * @param string $protocol The name of the protocol to use. **Defaults to**
83 * `gs`.
84 * @throws \RuntimeException
85 */
86 public static function register(StorageClient $client, $protocol = null)
87 {
88 $protocol = $protocol ?: self::DEFAULT_PROTOCOL;
89 // we are calling parents register function because only it can set parents pirvate ::$clients property.
90 // we are only calling it to set $clients property.
91 parent::register($client, $protocol);
92 // unregistering the wrapper so that we can register wrapper with our class.
93 stream_wrapper_unregister($protocol);
94 // registering wrapper with our wpCloud\StatelessMedia\StreamWrapper
95 return stream_wrapper_register($protocol, StreamWrapper::class, STREAM_IS_URL);
96 }
97
98 /**
99 * Returns the associative array that a `stat()` response expects using the
100 * provided stats. Defaults the remaining fields to 0.
101 *
102 * @param array $stats Sparse stats entries to set.
103 * @return array
104 */
105 private function makeStatArray($stats)
106 {
107 return array_merge(
108 array_fill_keys([
109 'dev',
110 'ino',
111 'mode',
112 'nlink',
113 'uid',
114 'gid',
115 'rdev',
116 'size',
117 'atime',
118 'mtime',
119 'ctime',
120 'blksize',
121 'blocks'
122 ], 0),
123 $stats
124 );
125 }
126
127 /**
128 * @param $path
129 * @param $option
130 * @param $value
131 * @return bool
132 */
133 public function stream_metadata($path, $option, $value)
134 {
135 return false;
136 }
137
138 }
139 }
140 }
141