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

Container.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: 3a56ebd9fcafbbd23de9502680191b9bf9b1be23 (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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
<?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\Exceptions;
use OpenCloud\Common\Lang;

/**
 * A container is a storage compartment for your data and provides a way for you 
 * to organize your data. You can think of a container as a folder in Windows® 
 * or a directory in UNIX®. The primary difference between a container and these 
 * other file system concepts is that containers cannot be nested.
 * 
 * A container can also be CDN-enabled (for public access), in which case you
 * will need to interact with a CDNContainer object instead of this one.
 */
class Container extends CDNContainer
{

    /**
     * CDN container (if set).
     * 
     * @var CDNContainer|null 
     */
    private $cdn;
    
    /**
     * Sets the CDN container.
     * 
     * @param OpenCloud\ObjectStore\Resource\CDNContainer $cdn
     */
    public function setCDN(CDNContainer $cdn)
    {
        $this->cdn = $cdn;
    }
    
    /**
     * Returns the CDN container.
     * 
     * @returns CDNContainer
     */
    public function getCDN()
    {
        if (!$this->cdn) {
            throw new Exceptions\CdnNotAvailableError(
            	Lang::translate('CDN-enabled container is not available')
            );
        }
        
        return $this->cdn;
    }
    
    /**
     * Backwards compatability.
     */
    public function CDN()
    {
        return $this->getCDN();
    }
    
    /**
     * Makes the container public via the CDN
     *
     * @api
     * @param integer $TTL the Time-To-Live for the CDN container; if NULL,
     *      then the cloud's default value will be used for caching.
     * @throws CDNNotAvailableError if CDN services are not available
     * @return CDNContainer
     */
    public function enableCDN($ttl = null)
    {
        $url = $this->getService()->CDN()->url() . '/' . rawurlencode($this->name);

        $headers = $this->metadataHeaders();

        if ($ttl) {
           
            // Make sure we're dealing with a real figure
            if (!is_integer($ttl)) {
                throw new Exceptions\CdnTtlError(sprintf(
                    Lang::translate('TTL value [%s] must be an integer'), 
                    $ttl
                ));
            }
            
            $headers['X-TTL'] = $ttl;
        }

        $headers['X-Log-Retention'] = 'True';
        $headers['X-CDN-Enabled']   = 'True';

        // PUT to the CDN container
        $response = $this->getService()->request($url, 'PUT', $headers);

        // check the response status
        // @codeCoverageIgnoreStart
        if ($response->httpStatus() > 202) {
            throw new Exceptions\CdnHttpError(sprintf(
                Lang::translate('HTTP error publishing to CDN, status [%d] response [%s]'),
                $response->httpStatus(),
                $response->httpBody()
            ));
        }
        // @codeCoverageIgnoreEnd

        // refresh the data
        $this->refresh();

        // return the CDN container object
        $cdn = new CDNContainer($this->getService()->getCDNService(), $this->name);
        $this->setCDN($cdn);
        
        return $cdn;
    }

    /**
     * Backwards compatability.
     */
    public function publishToCDN($ttl = null)
    {
        return $this->enableCDN($ttl);
    }

    /**
     * Disables the containers CDN function.
     *
     * Note that the container will still be available on the CDN until
     * its TTL expires.
     *
     * @api
     * @return void
     */
    public function disableCDN()
    {
        // Set necessary headers
        $headers['X-Log-Retention'] = 'False';
        $headers['X-CDN-Enabled']   = 'False';

        // PUT it to the CDN service
        $response = $this->getService()->request($this->CDNURL(), 'PUT', $headers);

        // check the response status
        // @codeCoverageIgnoreStart
        if ($response->httpStatus() != 201) {
            throw new Exceptions\CdnHttpError(sprintf(
                Lang::translate('HTTP error disabling CDN, status [%d] response [%s]'),
                $response->httpStatus(),
                $response->httpBody()
            ));
        }
        // @codeCoverageIgnoreEnd
        
        return true;
    }

    /**
     * Creates a static website from the container
     *
     * @api
     * @link http://docs.rackspace.com/files/api/v1/cf-devguide/content/Create_Static_Website-dle4000.html
     * @param string $index the index page (starting page) of the website
     * @return \OpenCloud\HttpResponse
     */
    public function createStaticSite($indexHtml)
    {
        $headers = array('X-Container-Meta-Web-Index' => $indexHtml);
        $response = $this->getService()->request($this->url(), 'POST', $headers);

        // check return code
        // @codeCoverageIgnoreStart
        if ($response->HttpStatus() > 204) {
            throw new Exceptions\ContainerError(sprintf(
                Lang::translate('Error creating static website for [%s], status [%d] response [%s]'),
                $this->name,
                $response->httpStatus(),
                $response->httpBody()
            ));
        }
        // @codeCoverageIgnoreEnd

        return $response;
    }

    /**
     * Sets the error page(s) for the static website
     *
     * @api
     * @link http://docs.rackspace.com/files/api/v1/cf-devguide/content/Set_Error_Pages_for_Static_Website-dle4005.html
     * @param string $name the name of the error page
     * @return \OpenCloud\HttpResponse
     */
    public function staticSiteErrorPage($name)
    {
        $headers = array('X-Container-Meta-Web-Error' => $name);
        $response = $this->getService()->request($this->url(), 'POST', $headers);

        // check return code
        // @codeCoverageIgnoreStart
        if ($response->httpStatus() > 204) {
            throw new Exceptions\ContainerError(sprintf(
                Lang::translate('Error creating static site error page for [%s], status [%d] response [%s]'),
                $this->name,
                $response->httpStatus(),
                $response->httpBody()
            ));
        }

        return $response;
        // @codeCoverageIgnoreEnd
    }

    /**
     * Returns the CDN URL of the container (if enabled)
     *
     * The CDNURL() is used to manage the container. Note that it is different
     * from the PublicURL() of the container, which is the publicly-accessible
     * URL on the network.
     *
     * @api
     * @return string
     */
    public function CDNURL()
    {
        return $this->getCDN()->url();
    }

    /**
     * Returns the Public URL of the container (on the CDN network)
     *
     */
    public function publicURL()
    {
        return $this->CDNURI();
    }

    /**
     * Returns the CDN info about the container
     *
     * @api
     * @return stdClass
     */
    public function CDNinfo($property = null)
    {
        // Not quite sure why this is here...
        // @codeCoverageIgnoreStart
		if ($this->getService() instanceof CDNService) {
			return $this->metadata;
        }
        // @codeCoverageIgnoreEnd
        
        // return NULL if the CDN container is not enabled
        if (!isset($this->getCDN()->metadata->Enabled) 
            || $this->getCDN()->metadata->Enabled == 'False'
        ) {
            return null;
        }

        // check to see if it's set
        if (isset($this->getCDN()->metadata->$property)) {
            return trim($this->getCDN()->metadata->$property);
        } elseif ($property !== null) {
            return null;
        }

        // otherwise, return the whole metadata object
        return $this->getCDN()->metadata;
    }

    /**
     * Returns the CDN container URI prefix
     *
     * @api
     * @return string
     */
    public function CDNURI()
    {
        return $this->CDNinfo('Uri');
    }

    /**
     * Returns the SSL URI for the container
     *
     * @api
     * @return string
     */
    public function SSLURI()
    {
        return $this->CDNinfo('Ssl-Uri');
    }

    /**
     * Returns the streaming URI for the container
     *
     * @api
     * @return string
     */
    public function streamingURI()
    {
        return $this->CDNinfo('Streaming-Uri');
    }

    /**
     * Returns the IOS streaming URI for the container
     *
     * @api
     * @link http://docs.rackspace.com/files/api/v1/cf-devguide/content/iOS-Streaming-d1f3725.html
     * @return string
     */
    public function iosStreamingURI()
    {
        return $this->CDNinfo('Ios-Uri');
    }

    /**
     * Creates a Collection of objects in the container
     *
     * @param array $params associative array of parameter values.
     * * account/tenant - The unique identifier of the account/tenant.
     * * container- The unique identifier of the container.
     * * limit (Optional) - The number limit of results.
     * * marker (Optional) - Value of the marker, that the object names
     *      greater in value than are returned.
     * * end_marker (Optional) - Value of the marker, that the object names
     *      less in value than are returned.
     * * prefix (Optional) - Value of the prefix, which the returned object
     *      names begin with.
     * * format (Optional) - Value of the serialized response format, either
     *      json or xml.
     * * delimiter (Optional) - Value of the delimiter, that all the object
     *      names nested in the container are returned.
     * @link http://api.openstack.org for a list of possible parameter
     *      names and values
     * @return OpenCloud\Collection
     * @throws ObjFetchError
     */
    public function objectList($params = array())
    {
        // construct a query string out of the parameters
        $params['format'] = 'json';
        
        $queryString = $this->makeQueryString($params);

        // append the query string to the URL
        $url = $this->url();
        if (strlen($queryString) > 0) {
            $url .= '?' . $queryString;
        }
        
        return $this->getService()->collection(
        	'OpenCloud\ObjectStore\Resource\DataObject', $url, $this
        );
    }

    /**
     * Returns a new DataObject associated with this container
     *
     * @param string $name if supplied, the name of the object to return
     * @return DataObject
     */
    public function dataObject($name = null)
    {
        return new DataObject($this, $name);
    }

    /**
     * Refreshes, then associates the CDN container
     */
    public function refresh($id = null, $url = null)
    {
        parent::refresh($id, $url);
        
        // @codeCoverageIgnoreStart
		if ($this->getService() instanceof CDNService) {
			return;
        }
        
        
        if (null !== ($cdn = $this->getService()->CDN())) {
            try {
                $this->cdn = new CDNContainer(
                    $cdn,
                    $this->name
                );
            } catch (Exceptions\ContainerNotFoundError $e) {
                $this->cdn = new CDNContainer($cdn);
                $this->cdn->name = $this->name;
            }
        }
        // @codeCoverageIgnoreEnd
    }

}