| 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 Google\Cloud\Core\Upload\AbstractUploader; |
| 21 |
use GuzzleHttp\Psr7\StreamDecoratorTrait; |
| 22 |
use GuzzleHttp\Psr7\BufferStream; |
| 23 |
use Psr\Http\Message\StreamInterface; |
| 24 |
|
| 25 |
/** |
| 26 |
* A Stream implementation that uploads in chunks to a provided uploader when |
| 27 |
* we reach a certain chunkSize. Upon `close`, we will upload the remaining chunk. |
| 28 |
*/ |
| 29 |
class WriteStream implements StreamInterface |
| 30 |
{ |
| 31 |
use StreamDecoratorTrait; |
| 32 |
|
| 33 |
private $uploader; |
| 34 |
private $stream; |
| 35 |
private $chunkSize = 262144; |
| 36 |
private $hasWritten = false; |
| 37 |
|
| 38 |
/** |
| 39 |
* Create a new WriteStream instance |
| 40 |
* |
| 41 |
* @param AbstractUploader $uploader The uploader to use. |
| 42 |
* @param array $options [optional] { |
| 43 |
* Configuration options. |
| 44 |
* |
| 45 |
* @type int $chunkSize The size of the buffer above which we attempt to |
| 46 |
* upload data |
| 47 |
* } |
| 48 |
*/ |
| 49 |
public function __construct(AbstractUploader $uploader = null, $options = []) |
| 50 |
{ |
| 51 |
if ($uploader) { |
| 52 |
$this->setUploader($uploader); |
| 53 |
} |
| 54 |
if (array_key_exists('chunkSize', $options)) { |
| 55 |
$this->chunkSize = $options['chunkSize']; |
| 56 |
} |
| 57 |
$this->stream = new BufferStream($this->chunkSize); |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Close the stream. Uploads any remaining data. |
| 62 |
*/ |
| 63 |
public function close() |
| 64 |
{ |
| 65 |
if ($this->uploader && $this->hasWritten) { |
| 66 |
$this->uploader->upload(); |
| 67 |
$this->uploader = null; |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Write to the stream. If we pass the chunkable size, upload the available chunk. |
| 73 |
* |
| 74 |
* @param string $data Data to write |
| 75 |
* @return int The number of bytes written |
| 76 |
* @throws \RuntimeException |
| 77 |
*/ |
| 78 |
public function write($data) |
| 79 |
{ |
| 80 |
if (!isset($this->uploader)) { |
| 81 |
throw new \RuntimeException("No uploader set."); |
| 82 |
} |
| 83 |
|
| 84 |
// Ensure we have a resume uri here because we need to create the streaming |
| 85 |
// upload before we have data (size of 0). |
| 86 |
$this->uploader->getResumeUri(); |
| 87 |
$this->hasWritten = true; |
| 88 |
|
| 89 |
if (!$this->stream->write($data)) { |
| 90 |
$this->uploader->upload($this->getChunkedWriteSize()); |
| 91 |
} |
| 92 |
return strlen($data); |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Set the uploader for this class. You may need to set this after initialization |
| 97 |
* if the uploader depends on this stream. |
| 98 |
* |
| 99 |
* @param AbstractUploader $uploader The new uploader to use. |
| 100 |
*/ |
| 101 |
public function setUploader($uploader) |
| 102 |
{ |
| 103 |
$this->uploader = $uploader; |
| 104 |
} |
| 105 |
|
| 106 |
private function getChunkedWriteSize() |
| 107 |
{ |
| 108 |
return (int) floor($this->getSize() / $this->chunkSize) * $this->chunkSize; |
| 109 |
} |
| 110 |
} |
| 111 |
|