Welcome to mirror list, hosted at ThFree Co, Russian Federation.

CDNContainer.php « Resource « ObjectStore « OpenCloud « lib « php-opencloud « 3rdparty « files_external « apps - github.com/nextcloud/server.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 9b6367c87e0eeb404cf2dc2d45ab9554db6ef804 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
<?php
/**
 * PHP OpenCloud library.
 * 
 * @copyright Copyright 2013 Rackspace US, Inc. See COPYING for licensing information.
 * @license   https://www.apache.org/licenses/LICENSE-2.0 Apache 2.0
 * @version   1.6.0
 * @author    Glen Campbell <glen.campbell@rackspace.com>
 * @author    Jamie Hannaford <jamie.hannaford@rackspace.com>
 */

namespace OpenCloud\ObjectStore\Resource;

use OpenCloud\Common\Service as AbstractService;
use OpenCloud\Common\Lang;
use OpenCloud\Common\Exceptions;
use OpenCloud\ObjectStore\AbstractService as AbstractObjectService;

/**
 * A container that has been CDN-enabled. Each CDN-enabled container has a unique 
 * Uniform Resource Locator (URL) that can be combined with its object names and 
 * openly distributed in web pages, emails, or other applications.
 */
class CDNContainer extends AbstractStorageObject
{
    /**
     * The name of the container. 
     * 
     * The only restrictions on container names is that they cannot contain a 
     * forward slash (/) and must be less than 256 bytes in length. Please note 
     * that the length restriction applies to the name after it has been URL 
     * encoded. For example, a container named Course Docs would be URL encoded
     * as Course%20Docs - which is 13 bytes in length rather than the expected 11.
     * 
     * @var string 
     */
    public $name;
    
    /**
     * Count of how many objects exist in the container.
     * 
     * @var int 
     */
    public $count = 0;
    
    /**
     * The total bytes used in the container.
     * 
     * @var int 
     */
    public $bytes = 0;
    
    /**
     * The service object.
     * 
     * @var AbstractService 
     */
    private $service;
    
    /**
     * URL of the container.
     * 
     * @var string 
     */
    private $containerUrl;

    /**
     * Creates the container object
     *
     * Creates a new container object or, if the $cdata object is a string,
     * retrieves the named container from the object store. If $cdata is an
     * array or an object, then its values are used to set this object.
     *
     * @param OpenCloud\ObjectStore $service - the ObjectStore service
     * @param mixed $cdata - if supplied, the name of the object
     */
    public function __construct(AbstractService $service, $cdata = null)
    {
        $this->getLogger()->info('Initializing CDN Container Service...');

        parent::__construct();

        $this->service = $service;

        // Populate data if set
        $this->populate($cdata);
    }
    
    /**
     * Allow other objects to know what the primary key is.
     * 
     * @return string
     */
    public function primaryKeyField()
    {
        return 'name';
    }
    
    /**
     * Returns the Service associated with the Container
     */
    public function getService()
    {
        return $this->service;
    }
    
    /**
     * Returns the URL of the container
     *
     * @return string
	 * @param string $subresource not used; required for compatibility
     * @throws NoNameError
     */
    public function url($subresource = '')
    {
        if (strlen($this->name) == 0) {
            throw new Exceptions\NoNameError(
            	Lang::translate('Container does not have an identifier')
            );
        }
        
        return Lang::noslash($this->getService()->url(rawurlencode($this->name)));
    }

    /**
     * Creates a new container with the specified attributes
     *
     * @param array $params array of parameters
     * @return boolean TRUE on success; FALSE on failure
     * @throws ContainerCreateError
     */
    public function create($params = array())
    {
        // Populate object and check container name
        $this->populate($params);
        $this->isValidName($this->name);
        
        // Dispatch
        $this->containerUrl = $this->url();
        $response = $this->getService()->request($this->url(), 'PUT', $this->metadataHeaders());

        // Check return code
        // @codeCoverageIgnoreStart
        if ($response->httpStatus() > 202) {
            throw new Exceptions\ContainerCreateError(sprintf(
                Lang::translate('Problem creating container [%s] status [%d] response [%s]'),
                $this->url(),
                $response->httpStatus(),
                $response->httpBody()
            ));
        }
        // @codeCoverageIgnoreEnd

        return true;
    }

    /**
     * Updates the metadata for a container
     *
     * @return boolean TRUE on success; FALSE on failure
     * @throws ContainerCreateError
     */
    public function update()
    {
        $response = $this->getService()->request($this->url(), 'POST', $this->metadataHeaders());

        // check return code
        // @codeCoverageIgnoreStart
        if ($response->httpStatus() > 204) {
            throw new Exceptions\ContainerCreateError(sprintf(
                Lang::translate('Problem updating container [%s] status [%d] response [%s]'),
                $this->Url(),
                $response->httpStatus(),
                $response->httpBody()
            ));
        }
        // @codeCoverageIgnoreEnd
        
        return true;
    }

    /**
     * Deletes the specified container
     *
     * @return boolean TRUE on success; FALSE on failure
     * @throws ContainerDeleteError
     */
    public function delete()
    {
        $response = $this->getService()->request($this->url(), 'DELETE');

        // validate the response code
        // @codeCoverageIgnoreStart
        if ($response->httpStatus() == 404) {
            throw new Exceptions\ContainerNotFoundError(sprintf(
                Lang::translate('Container [%s] not found'),
                $this->name
            ));
        }

        if ($response->httpStatus() == 409) {
            throw new Exceptions\ContainerNotEmptyError(sprintf(
                Lang::translate('Container [%s] must be empty before deleting'),
                $this->name
            ));
        }

        if ($response->httpStatus() >= 300) {
            throw new Exceptions\ContainerDeleteError(sprintf(
                Lang::translate('Problem deleting container [%s] status [%d] response [%s]'),
                $this->url(),
                $response->httpStatus(),
                $response->httpBody()
            ));
            return false;
        }
        // @codeCoverageIgnoreEnd

        return true;
    }

    /**
     * Loads the object from the service
     *
     * @return void
     */
    public function refresh($name = null, $url = null)
    {
        $response = $this->getService()->request(
        	$this->url($name), 'HEAD', array('Accept' => '*/*')
        );

        // validate the response code
        // @codeCoverageIgnoreStart
        if ($response->HttpStatus() == 404) {
            throw new Exceptions\ContainerNotFoundError(sprintf(
                'Container [%s] (%s) not found',
                $this->name,
                $this->url()
            ));
        }

        if ($response->HttpStatus() >= 300) {
            throw new Exceptions\HttpError(sprintf(
                'Error retrieving Container, status [%d] response [%s]',
                $response->httpStatus(),
                $response->httpBody()
            ));
        }

		// check for headers (not metadata)
		foreach($response->headers() as $header => $value) {
			switch($header) {
                case 'X-Container-Object-Count':
                    $this->count = $value;
                    break;
                case 'X-Container-Bytes-Used':
                    $this->bytes = $value;
                    break;
			}
		}
        // @codeCoverageIgnoreEnd

        // parse the returned object
        $this->getMetadata($response);
    }

    /**
     * Validates that the container name is acceptable
     *
     * @param string $name the container name to validate
     * @return boolean TRUE if ok; throws an exception if not
     * @throws ContainerNameError
     */
    public function isValidName($name)
    {
        if (strlen($name) == 0) {
            throw new Exceptions\ContainerNameError(
            	'Container name cannot be blank'
            );
        }

        if (strpos($name, '/') !== false) {
            throw new Exceptions\ContainerNameError(
            	'Container name cannot contain "/"'
            );
        }

        if (strlen($name) > AbstractObjectService::MAX_CONTAINER_NAME_LEN) {
            throw new Exceptions\ContainerNameError(
            	'Container name is too long'
            );
        }

        return true;
    }

}