1 <?php
2 /**
3 * Copyright 2012-2014 Rackspace US, Inc.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 namespace OpenCloud\ObjectStore\Upload;
19
20 use Guzzle\Http\EntityBody;
21 use Guzzle\Http\ReadLimitEntityBody;
22 use OpenCloud\Common\Constants\Size;
23
24 /**
25 * A transfer type which executes consecutively - i.e. it will upload an entire EntityBody and then move on to the next
26 * in a linear fashion. There is no concurrency here.
27 *
28 * @codeCoverageIgnore
29 */
30 class ConsecutiveTransfer extends AbstractTransfer
31 {
32 public function transfer()
33 {
34 while (!$this->entityBody->isConsumed()) {
35 if ($this->entityBody->getContentLength() && $this->entityBody->isSeekable()) {
36 // Stream directly from the data
37 $body = new ReadLimitEntityBody($this->entityBody, $this->partSize, $this->entityBody->ftell());
38 } else {
39 // If not-seekable, read the data into a new, seekable "buffer"
40 $body = EntityBody::factory();
41 $output = true;
42 while ($body->getContentLength() < $this->partSize && $output !== false) {
43 // Write maximum of 10KB at a time
44 $length = min(10 * Size::KB, $this->partSize - $body->getContentLength());
45 $output = $body->write($this->entityBody->read($length));
46 }
47 }
48
49 if ($body->getContentLength() == 0) {
50 break;
51 }
52
53 $request = TransferPart::createRequest(
54 $body,
55 $this->transferState->count() + 1,
56 $this->client,
57 $this->options
58 );
59
60 $response = $request->send();
61
62 $this->transferState->addPart(TransferPart::fromResponse($response));
63 }
64 }
65 }
66