PluginProbe
WP-Stateless – Google Cloud Storage / 3.2.0
WP-Stateless – Google Cloud Storage v3.2.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.2.0, at lib/classes/class-gs-stream-wrapper.php

134 lines 4.0 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 * @var string Protocol used to open this stream
19 */
20 private $protocol;
21
22 /**
23 * @var Bucket Reference to the bucket the opened file
24 * lives in or will live in.
25 */
26 private $bucket;
27
28 /**
29 * @var string Name of the file opened by this stream.
30 */
31 private $file;
32
33 /**
34 * Callback handler for retrieving information about a file
35 *
36 * @param string $path The URI to the file
37 * @param int $flags Bitwise mask of options
38 * @return array|bool
39 */
40 public function url_stat($path, $flags) {
41 $this->_openPath($path);
42 // if root dir
43 if (empty($this->file)) {
44 $stats = [];
45 // equivalent to 40777 and 40444 in octal
46 if ($is_writable = $this->bucket->isWritable()) {
47 $stats['mode'] = $is_writable
48 ? self::DIRECTORY_WRITABLE_MODE
49 : self::DIRECTORY_READABLE_MODE;
50 return $this->makeStatArray($stats);
51 }
52 }
53 return parent::url_stat($path, $flags);
54 }
55
56 /**
57 * Parse the URL and set protocol, filename and bucket.
58 *
59 * @param string $path URL to open
60 * @return StorageClient
61 */
62 private function _openPath($path) {
63 $url = (array) parse_url($path) + [
64 'scheme' => '',
65 'path' => '',
66 'host' => ''
67 ];
68 $this->protocol = $url['scheme'];
69 $this->file = ltrim($url['path'], '/');
70 $client = self::getClient($this->protocol);
71 $this->bucket = $client->bucket($url['host']);
72 return $client;
73 }
74
75 /**
76 * Register a StreamWrapper for reading and writing to Google Storage
77 *
78 * @param StorageClient $client The StorageClient configuration to use.
79 * @param string $protocol The name of the protocol to use. **Defaults to**
80 * `gs`.
81 * @throws \RuntimeException
82 */
83 public static function register(StorageClient $client, $protocol = null) {
84 $protocol = $protocol ?: self::DEFAULT_PROTOCOL;
85 // we are calling parents register function because only it can set parents pirvate ::$clients property.
86 // we are only calling it to set $clients property.
87 parent::register($client, $protocol);
88 // unregistering the wrapper so that we can register wrapper with our class.
89 stream_wrapper_unregister($protocol);
90 // registering wrapper with our wpCloud\StatelessMedia\StreamWrapper
91 return stream_wrapper_register($protocol, StreamWrapper::class, STREAM_IS_URL);
92 }
93
94 /**
95 * Returns the associative array that a `stat()` response expects using the
96 * provided stats. Defaults the remaining fields to 0.
97 *
98 * @param array $stats Sparse stats entries to set.
99 * @return array
100 */
101 private function makeStatArray($stats) {
102 return array_merge(
103 array_fill_keys([
104 'dev',
105 'ino',
106 'mode',
107 'nlink',
108 'uid',
109 'gid',
110 'rdev',
111 'size',
112 'atime',
113 'mtime',
114 'ctime',
115 'blksize',
116 'blocks'
117 ], 0),
118 $stats
119 );
120 }
121
122 /**
123 * @param $path
124 * @param $option
125 * @param $value
126 * @return bool
127 */
128 public function stream_metadata($path, $option, $value) {
129 return false;
130 }
131 }
132 }
133 }
134