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 / WriteStream.php

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

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