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

140 lines 4.1 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 try {
42 $this->_openPath($path);
43
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
56 return parent::url_stat($path, $flags);
57 } catch (\Exception $e) {
58 return false;
59 }
60 }
61
62 /**
63 * Parse the URL and set protocol, filename and bucket.
64 *
65 * @param string $path URL to open
66 * @return StorageClient
67 */
68 private function _openPath($path) {
69 $url = (array) parse_url($path) + [
70 'scheme' => '',
71 'path' => '',
72 'host' => ''
73 ];
74 $this->protocol = $url['scheme'];
75 $this->file = ltrim($url['path'], '/');
76 $client = self::getClient($this->protocol);
77 $this->bucket = $client->bucket($url['host']);
78 return $client;
79 }
80
81 /**
82 * Register a StreamWrapper for reading and writing to Google Storage
83 *
84 * @param StorageClient $client The StorageClient configuration to use.
85 * @param string $protocol The name of the protocol to use. **Defaults to**
86 * `gs`.
87 * @throws \RuntimeException
88 */
89 public static function register(StorageClient $client, $protocol = null) {
90 $protocol = $protocol ?: self::DEFAULT_PROTOCOL;
91 // we are calling parents register function because only it can set parents pirvate ::$clients property.
92 // we are only calling it to set $clients property.
93 parent::register($client, $protocol);
94 // unregistering the wrapper so that we can register wrapper with our class.
95 stream_wrapper_unregister($protocol);
96 // registering wrapper with our wpCloud\StatelessMedia\StreamWrapper
97 return stream_wrapper_register($protocol, StreamWrapper::class, STREAM_IS_URL);
98 }
99
100 /**
101 * Returns the associative array that a `stat()` response expects using the
102 * provided stats. Defaults the remaining fields to 0.
103 *
104 * @param array $stats Sparse stats entries to set.
105 * @return array
106 */
107 private function makeStatArray($stats) {
108 return array_merge(
109 array_fill_keys([
110 'dev',
111 'ino',
112 'mode',
113 'nlink',
114 'uid',
115 'gid',
116 'rdev',
117 'size',
118 'atime',
119 'mtime',
120 'ctime',
121 'blksize',
122 'blocks'
123 ], 0),
124 $stats
125 );
126 }
127
128 /**
129 * @param $path
130 * @param $option
131 * @param $value
132 * @return bool
133 */
134 public function stream_metadata($path, $option, $value) {
135 return false;
136 }
137 }
138 }
139 }
140