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