PluginProbe
Media Cloud Sync / 1.2.10
Media Cloud Sync v1.2.10
1.4.2 1.4.1 1.4.0 1.3.12 1.3.11 1.3.10 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.2.0 1.2.10 1.2.11 1.2.12 1.2.13 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 All 36 releases
media-cloud-sync / includes / sdk / google / google / cloud-storage / src / ReadStream.php

ReadStream.php in Media Cloud Sync 1.2.10, at includes/sdk/google/google/cloud-storage/src/ReadStream.php

87 lines 2.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Copyright 2017 Google Inc. All Rights Reserved.
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18 namespace Dudlewebs\WPMCS\Google\Cloud\Storage;
19
20 use Dudlewebs\WPMCS\GuzzleHttp\Psr7\StreamDecoratorTrait;
21 use Dudlewebs\WPMCS\Psr\Http\Message\StreamInterface;
22 /**
23 * A Stream implementation that wraps a GuzzleHttp download stream to
24 * provide `getSize()` from the response headers.
25 */
26 class ReadStream implements StreamInterface
27 {
28 use StreamDecoratorTrait;
29 private $stream;
30 /**
31 * Create a new ReadStream.
32 *
33 * @param StreamInterface $stream The stream interface to wrap
34 */
35 public function __construct(StreamInterface $stream)
36 {
37 $this->stream = $stream;
38 }
39 /**
40 * Return the full size of the buffer. If the underlying stream does
41 * not report it's size, try to fetch the size from the Content-Length
42 * response header.
43 *
44 * @return int The size of the stream.
45 */
46 public function getSize(): ?int
47 {
48 return $this->stream->getSize() ?: $this->getSizeFromMetadata();
49 }
50 /**
51 * Attempt to fetch the size from the Content-Length response header.
52 * If we cannot, return 0.
53 *
54 * @return int The Size of the stream
55 */
56 private function getSizeFromMetadata(): int
57 {
58 foreach ($this->stream->getMetadata('wrapper_data') as $value) {
59 if (substr($value, 0, 15) == "Content-Length:") {
60 return (int) substr($value, 16);
61 }
62 }
63 return 0;
64 }
65 /**
66 * Read bytes from the underlying buffer, retrying until we have read
67 * enough bytes or we cannot read any more. We do this because the
68 * internal C code for filling a buffer does not account for when
69 * we try to read large chunks from a user-land stream that does not
70 * return enough bytes.
71 *
72 * @param int $length The number of bytes to read.
73 * @return string Read bytes from the underlying stream.
74 */
75 public function read($length): string
76 {
77 $data = '';
78 do {
79 $moreData = $this->stream->read($length);
80 $data .= $moreData;
81 $readLength = strlen($moreData);
82 $length -= $readLength;
83 } while ($length > 0 && $readLength > 0);
84 return $data;
85 }
86 }
87