uploadProgressCallback = $options['uploadProgressCallback']; } elseif (isset($options['uploadProgressCallback'])) { throw new \InvalidArgumentException('$options.uploadProgressCallback must be a callable.'); } } /** * Gets the resume URI. * * @return string */ public function getResumeUri() { if (!$this->resumeUri) { return $this->createResumeUri(); } return $this->resumeUri; } /** * Resumes a download using the provided URI. * * @param string $resumeUri * @return array * @throws GoogleException */ public function resume($resumeUri) { if (!$this->data->isSeekable()) { throw new GoogleException('Cannot resume upload on a stream which cannot be seeked.'); } $this->resumeUri = $resumeUri; $response = $this->getStatusResponse(); if ($response->getBody()->getSize() > 0) { return $this->decodeResponse($response); } $this->rangeStart = $this->getRangeStart($response->getHeaderLine('Range')); return $this->upload(); } /** * Triggers the upload process. * * Errors are of form [`google.rpc.Status`](https://cloud.google.com/apis/design/errors#error_model), * and may be obtained via {@see Google\Cloud\Core\Exception\ServiceException::getMetadata()}. * * @return array * @throws ServiceException */ public function upload() { $rangeStart = $this->rangeStart; $response = null; $resumeUri = $this->getResumeUri(); $size = $this->data->getSize() ?: '*'; do { $data = new LimitStream( $this->data, $this->chunkSize ?: - 1, $rangeStart ); $currStreamLimitSize = $data->getSize(); $rangeEnd = $rangeStart + ($currStreamLimitSize - 1); $headers = $this->headers + [ 'Content-Length' => $currStreamLimitSize, 'Content-Type' => $this->contentType, 'Content-Range' => "bytes $rangeStart-$rangeEnd/$size", ]; $request = new Request( 'PUT', $resumeUri, $headers, $data ); try { $response = $this->requestWrapper->send($request, $this->requestOptions); } catch (GoogleException $ex) { throw new ServiceException( "Upload failed. Please use this URI to resume your upload: $this->resumeUri", $ex->getCode(), null, json_decode($ex->getMessage(), true) ?: [] ); } if (is_callable($this->uploadProgressCallback)) { call_user_func($this->uploadProgressCallback, $currStreamLimitSize); } $rangeStart = $this->getRangeStart($response->getHeaderLine('Range')); } while ($response->getStatusCode() === 308); return $this->decodeResponse($response); } /** * Currently only the MultiPartUploader supports async. * * Any calls to this will throw a generic Google Exception. * * @return PromiseInterface * @throws GoogleException * @experimental The experimental flag means that while we believe this method * or class is ready for use, it may change before release in backwards- * incompatible ways. Please use with caution, and test thoroughly when * upgrading. */ public function uploadAsync() { throw new GoogleException('Currently only the MultiPartUploader supports async.'); } /** * Fetch and decode the response body * * @param ResponseInterface $response * @return array */ protected function decodeResponse(ResponseInterface $response) { return $this->jsonDecode($response->getBody(), true); } /** * Creates the resume URI. * * @return string */ protected function createResumeUri() { $headers = $this->headers + [ 'X-Upload-Content-Type' => $this->contentType, 'X-Upload-Content-Length' => $this->data->getSize(), 'Content-Type' => 'application/json' ]; $body = $this->jsonEncode($this->metadata); $request = new Request( 'POST', $this->uri, $headers, $body ); $response = $this->requestWrapper->send($request, $this->requestOptions); return $this->resumeUri = $response->getHeaderLine('Location'); } /** * Gets the status of the upload. * * @return ResponseInterface */ protected function getStatusResponse() { $request = new Request( 'PUT', $this->resumeUri, ['Content-Range' => 'bytes */*'] ); return $this->requestWrapper->send($request, $this->requestOptions); } /** * Gets the starting range for the upload. * * @param string $rangeHeader * @return int|null */ protected function getRangeStart($rangeHeader) { if (!$rangeHeader) { return null; } return (int) explode('-', $rangeHeader)[1] + 1; } }