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