content = $content; $this->driveItemResourceDefinition = $driveItemResourceDefinition; $this->rangeSize = array_key_exists('range_size', $options) ? $options['range_size'] : null; } /** * Getter. * * @param string $name * The name. * * @return mixed * The value. * * @since 2.1.0 */ public function __get($name) { $uploadSession = $this->entity; switch ($name) { case 'expirationDateTime': return $uploadSession->getExpirationDateTime(); case 'nextExpectedRanges': return $uploadSession->getNextExpectedRanges(); case 'uploadUrl': return $uploadSession->getUploadUrl(); default: return parent::__get($name); } } /** * Uploads the content in multiple ranges and completes this session. * * @return \Krizalys\Onedrive\Proxy\DriveItemProxy * The drive item created. * * @since 2.1.0 * * @api * * @link https://docs.microsoft.com/en-us/onedrive/developer/rest-api/api/driveitem_createuploadsession?view=odsp-graph-online#upload-bytes-to-the-upload-session * Upload bytes to the upload session * * @todo Support retries on errors. */ public function complete() { $stream = $this->content instanceof Stream ? $this->content : Psr7\Utils::streamFor($this->content); if ($this->rangeSize !== null) { $rangeSize = $this->rangeSize; $rangeSize = $rangeSize - $rangeSize % self::RANGE_SIZE_MULTIPLE; $rangeSize = min($rangeSize, self::MAX_RANGE_SIZE); $rangeSize = max($rangeSize, self::MIN_RANGE_SIZE); } else { $rangeSize = self::RANGE_SIZE_MULTIPLE; } $size = $stream->getSize(); $offset = 0; while (!$stream->eof()) { $rangeStream = new LimitStream($stream, $rangeSize, $offset); $rangeSize = $rangeStream->getSize(); $body = $rangeStream->getContents(); $rangeFirst = $offset; $offset += $rangeSize; $rangeLast = $offset - 1; $headers = [ 'Content-Length' => $rangeSize, 'Content-Range' => "bytes $rangeFirst-$rangeLast/$size", ]; $response = $this ->graph ->createRequest('PUT', $this->uploadUrl) ->addHeaders($headers) ->attachBody($body) ->execute(); $status = $response->getStatus(); if ($status == 200 || $status == 201) { $driveItem = $response->getResponseAsObject(DriveItem::class); return new DriveItemProxy( $this->graph, $driveItem, $this->driveItemResourceDefinition ); } if ($status != 202) { throw new \Exception("Unexpected status code produced by 'PUT {$this->uploadUrl}': $status"); } } throw new \Exception('OneDrive did not create a drive item for the uploaded file'); } }