1: <?php
2: 3: 4: 5: 6: 7: 8: 9:
10:
11: namespace OpenCloud\ObjectStore\Upload;
12:
13: use Guzzle\Http\Message\Response;
14: use Guzzle\Http\Url;
15: use OpenCloud\Common\Constants\Header;
16: use OpenCloud\Common\Constants\Mime;
17:
18: 19: 20: 21: 22:
23: class TransferPart
24: {
25: 26: 27:
28: protected $partNumber;
29:
30: 31: 32:
33: protected $eTag;
34:
35: 36: 37:
38: protected $contentLength;
39:
40: 41: 42:
43: protected $path;
44:
45: 46: 47: 48:
49: public function setContentLength($contentLength)
50: {
51: $this->contentLength = $contentLength;
52: return $this;
53: }
54:
55: 56: 57:
58: public function getContentLength()
59: {
60: return $this->contentLength;
61: }
62:
63: 64: 65: 66:
67: public function setETag($etag)
68: {
69: $this->etag = $etag;
70: return $this;
71: }
72:
73: 74: 75:
76: public function getETag()
77: {
78: return $this->etag;
79: }
80:
81: 82: 83: 84:
85: public function setPartNumber($partNumber)
86: {
87: $this->partNumber = $partNumber;
88: return $this;
89: }
90:
91: 92: 93:
94: public function getPartNumber()
95: {
96: return $this->partNumber;
97: }
98:
99: 100: 101: 102:
103: public function setPath($path)
104: {
105: $this->path = $path;
106: return $this;
107: }
108:
109: 110: 111:
112: public function getPath()
113: {
114: return $this->path;
115: }
116:
117: 118: 119: 120: 121: 122: 123: 124: 125:
126: public static function createRequest($part, $number, $client, $options)
127: {
128: $name = sprintf('%s/%s/%d', $options['objectName'], $options['prefix'], $number);
129: $url = clone $options['containerUrl'];
130: $url->addPath($name);
131:
132: $headers = array(
133: Header::CONTENT_LENGTH => $part->getContentLength(),
134: Header::CONTENT_TYPE => $part->getContentType()
135: );
136:
137: if ($options['doPartChecksum'] === true) {
138: $headers['ETag'] = $part->getContentMd5();
139: }
140:
141: $request = $client->put($url, $headers, $part);
142:
143: if (isset($options['progress'])) {
144: $request->getCurlOptions()->add('progress', true);
145: if (is_callable($options['progress'])) {
146: $request->getCurlOptions()->add('progressCallback', $options['progress']);
147: }
148: }
149:
150: return $request;
151: }
152:
153: 154: 155: 156: 157: 158: 159:
160: public static function fromResponse(Response $response, $partNumber = 1)
161: {
162: $responseUri = Url::factory($response->getEffectiveUrl());
163:
164: $object = new self();
165:
166: $object->setPartNumber($partNumber)
167: ->setContentLength($response->getHeader(Header::CONTENT_LENGTH))
168: ->setETag($response->getHeader(Header::ETAG))
169: ->setPath($responseUri->getPath());
170:
171: return $object;
172: }
173:
174: }
175: