setUploader($uploader); } if (array_key_exists('chunkSize', $options)) { $this->chunkSize = $options['chunkSize']; } $this->stream = new \DeliciousBrains\WP_Offload_Media\Gcp\GuzzleHttp\Psr7\BufferStream($this->chunkSize); } /** * Close the stream. Uploads any remaining data. */ public function close() { if ($this->uploader && $this->hasWritten) { $this->uploader->upload(); $this->uploader = null; } } /** * Write to the stream. If we pass the chunkable size, upload the available chunk. * * @param string $data Data to write * @return int The number of bytes written * @throws \RuntimeException */ public function write($data) { if (!isset($this->uploader)) { throw new \RuntimeException("No uploader set."); } // Ensure we have a resume uri here because we need to create the streaming // upload before we have data (size of 0). $this->uploader->getResumeUri(); $this->hasWritten = true; if (!$this->stream->write($data)) { $this->uploader->upload($this->getChunkedWriteSize()); } return strlen($data); } /** * Set the uploader for this class. You may need to set this after initialization * if the uploader depends on this stream. * * @param AbstractUploader $uploader The new uploader to use. */ public function setUploader($uploader) { $this->uploader = $uploader; } private function getChunkedWriteSize() { return (int) floor($this->getSize() / $this->chunkSize) * $this->chunkSize; } }