1: <?php
2: 3: 4: 5: 6: 7: 8: 9:
10:
11: namespace OpenCloud\ObjectStore;
12:
13: use Guzzle\Http\EntityBody;
14: use OpenCloud\Common\Constants\Header;
15: use OpenCloud\Common\Constants\Mime;
16: use OpenCloud\Common\Exceptions;
17: use OpenCloud\Common\Exceptions\InvalidArgumentError;
18: use OpenCloud\Common\Http\Client;
19: use OpenCloud\Common\Http\Message\Formatter;
20: use OpenCloud\Common\Service\ServiceBuilder;
21: use OpenCloud\ObjectStore\Resource\Container;
22: use OpenCloud\ObjectStore\Constants\UrlType;
23:
24: 25: 26:
27: class Service extends AbstractService
28: {
29: const DEFAULT_NAME = 'cloudFiles';
30: const DEFAULT_TYPE = 'object-store';
31:
32: 33: 34: 35: 36:
37: private $cdnService;
38:
39: public function __construct(Client $client, $type = null, $name = null, $region = null, $urlType = null)
40: {
41: parent::__construct($client, $type, $name, $region, $urlType);
42:
43: try {
44: $this->cdnService = ServiceBuilder::factory($client, 'OpenCloud\ObjectStore\CDNService', array(
45: 'region' => $region
46: ));
47: } catch (Exceptions\EndpointError $e) {}
48: }
49:
50: 51: 52:
53: public function getCdnService()
54: {
55: return $this->cdnService;
56: }
57:
58: 59: 60: 61:
62: public function getContainer($data = null)
63: {
64: return new Container($this, $data);
65: }
66:
67: 68: 69: 70: 71: 72: 73:
74: public function createContainer($name, array $metadata = array())
75: {
76: $this->checkContainerName($name);
77:
78: $containerHeaders = Container::stockHeaders($metadata);
79:
80: $response = $this->getClient()
81: ->put($this->getUrl($name), $containerHeaders)
82: ->send();
83:
84: if ($response->getStatusCode() == 201) {
85: return Container::fromResponse($response, $this);
86: }
87:
88: return false;
89: }
90:
91: 92: 93: 94: 95: 96: 97:
98: public function checkContainerName($name)
99: {
100: if (strlen($name) == 0) {
101: $error = 'Container name cannot be blank';
102: }
103:
104: if (strpos($name, '/') !== false) {
105: $error = 'Container name cannot contain "/"';
106: }
107:
108: if (strlen($name) > self::MAX_CONTAINER_NAME_LENGTH) {
109: $error = 'Container name is too long';
110: }
111:
112: if (isset($error)) {
113: throw new InvalidArgumentError($error);
114: }
115:
116: return true;
117: }
118:
119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131:
132: public function ($path = '', $archive, $archiveType = UrlType::TAR_GZ)
133: {
134: $entity = EntityBody::factory($archive);
135:
136: $acceptableTypes = array(
137: UrlType::TAR,
138: UrlType::TAR_GZ,
139: UrlType::TAR_BZ2
140: );
141:
142: if (!in_array($archiveType, $acceptableTypes)) {
143: throw new InvalidArgumentError(sprintf(
144: 'The archive type must be one of the following: [%s]. You provided [%s].',
145: implode($acceptableTypes, ','),
146: print_r($archiveType, true)
147: ));
148: }
149:
150: $url = $this->getUrl()->addPath($path)->setQuery(array('extract-archive' => $archiveType));
151: $response = $this->getClient()->put($url, array(Header::CONTENT_TYPE => ''), $entity)->send();
152:
153: $body = Formatter::decode($response);
154:
155: if (!empty($body->Errors)) {
156: throw new Exception\BulkOperationException((array) $body->Errors);
157: }
158:
159: return $response;
160: }
161:
162: 163: 164: 165: 166: 167: 168: 169:
170: public function bulkDelete(array $paths)
171: {
172: $entity = EntityBody::factory(implode(PHP_EOL, $paths));
173:
174: $url = $this->getUrl()->setQuery(array('bulk-delete' => true));
175:
176: $response = $this->getClient()
177: ->delete($url, array(Header::CONTENT_TYPE => Mime::TEXT), $entity)
178: ->send();
179:
180: try {
181: $body = Formatter::decode($response);
182: if (!empty($body->Errors)) {
183: throw new Exception\BulkOperationException((array) $body->Errors);
184: }
185: } catch (Exceptions\JsonError $e) {}
186:
187: return $response;
188: }
189:
190: }