74 class Container implements \Countable, \IteratorAggregate {
119 if (empty($prefix)) {
123 foreach ($metadata as
$key => $val) {
124 $headers[$prefix .
$key] = $val;
152 if (strpos($oname,
'/') === FALSE) {
153 return $base .
'/' . rawurlencode($oname);
156 $oParts = explode(
'/', $oname);
158 foreach ($oParts as $part) {
159 $buffer[] = rawurlencode($part);
161 $newname = implode(
'/', $buffer);
162 return $base .
'/' . $newname;
188 if (empty($prefix)) {
191 $attributes = array();
192 $offset = strlen($prefix);
193 foreach ($headers as $header => $value) {
195 $index = strpos($header, $prefix);
197 $key = substr($header, $offset);
198 $attributes[
$key] = $value;
233 if (!empty($jsonArray[
'count'])) {
237 if (!empty($jsonArray[
'bytes'])) {
268 $container->bytes = $response->header(
'X-Container-Bytes-Used', 0);
269 $container->count = $response->header(
'X-Container-Object-Count', 0);
352 $this->cdnSslUrl = $sslUrl;
374 if (is_null($this->
bytes)) {
405 return $this->metadata;
454 if (is_null($this->
count)) {
488 if (empty($this->
token)) {
489 throw new \HPCloud\Exception(
'Container does not have an auth token.');
491 if (empty($this->
url)) {
492 throw new \HPCloud\Exception(
'Container does not have a URL to send data.');
512 if (!empty($encoding)) {
513 $headers[
'Content-Encoding'] = rawurlencode($encoding);
518 if (!empty($disposition)) {
519 $headers[
'Content-Disposition'] = $disposition;
527 if (!empty($moreHeaders)) {
528 $headers += $moreHeaders;
531 $client = \HPCloud\Transport::instance();
535 $headers[
'Etag'] = $obj->
eTag();
542 $headers[
'Transfer-Encoding'] =
'chunked';
547 $response = $client->doRequest(
$url,
'PUT', $headers, $obj->
content());
556 $stat = fstat(
$file);
557 $headers[
'Content-Length'] = $stat[
'size'];
560 $hash = hash_init(
'md5');
561 hash_update_stream($hash,
$file);
562 $etag = hash_final($hash);
563 $headers[
'Etag'] = $etag;
568 $response = $client->doRequestWithResource(
$url,
'PUT', $headers,
$file);
572 if ($response->status() != 201) {
573 throw new \HPCloud\Exception(
'An unknown error occurred while saving: ' . $response->status());
616 $client = \HPCloud\Transport::instance();
619 $response = $client->doRequest(
$url,
'POST', $headers, $obj->
content());
621 if ($response->status() != 202) {
622 throw new \HPCloud\Exception(
'An unknown error occurred while saving: ' . $response->status());
656 $sourceUrl = self::objectUrl($this->
url, $obj->
name());
658 if (empty($newName)) {
659 throw new \HPCloud\Exception(
"An object name is required to copy the object.");
667 $destUrl = self::objectUrl(
'/' .
$container, $newName);
670 'X-Auth-Token' => $this->
token,
671 'Destination' => $destUrl,
674 $client = \HPCloud\Transport::instance();
675 $response = $client->doRequest($sourceUrl,
'COPY', $headers);
677 if ($response->status() != 201) {
678 throw new \HPCloud\Exception(
"An unknown condition occurred during copy. " . $response->status());
720 $cdnSsl = self::objectUrl($this->cdnSslUrl,
$name);
726 $client = \HPCloud\Transport::instance();
728 if (empty($this->
cdnUrl)) {
729 $response = $client->doRequest(
$url,
'GET', $headers);
732 $from = $requireSSL ? $cdnSsl : $cdn;
734 $response = $client->doRequest($from,
'GET', $headers);
737 if ($response->status() != 200) {
738 throw new \HPCloud\Exception(
'An unknown error occurred while saving: ' . $response->status());
742 $remoteObject->setContent($response->content());
744 if (!empty($this->
cdnUrl)) {
745 $remoteObject->useCDN($cdn, $cdnSsl);
748 return $remoteObject;
784 $cdnSsl = self::objectUrl($this->cdnSslUrl,
$name);
786 'X-Auth-Token' => $this->
token,
790 $client = \HPCloud\Transport::instance();
792 if (empty($this->
cdnUrl)) {
793 $response = $client->doRequest(
$url,
'HEAD', $headers);
796 $response = $client->doRequest($cdnSsl,
'HEAD', $headers);
799 if ($response->status() != 200) {
800 throw new \HPCloud\Exception(
'An unknown error occurred while saving: ' . $response->status());
803 $headers = $response->headers();
805 $obj = RemoteObject::newFromHeaders(
$name, $headers, $this->
token,
$url);
807 if (!empty($this->
cdnUrl)) {
808 $obj->useCDN($cdn, $cdnSsl);
852 public function objects($limit = NULL, $marker = NULL) {
854 return $this->
objectQuery($params, $limit, $marker);
917 'delimiter' => $delimiter,
919 return $this->
objectQuery($params, $limit, $marker);
961 public function objectsByPath($path, $delimiter =
'/', $limit = NULL, $marker = NULL) {
964 'delimiter' => $delimiter,
966 return $this->
objectQuery($params, $limit, $marker);
985 return $ssl ? $this->cdnSslUrl : $this->cdnUrl;
1007 if (!isset($this->
acl)) {
1028 if (empty($this->
url) || empty($this->
token)) {
1029 throw new \HPCloud\Exception(
'Remote data cannot be fetched. Tokena and endpoint URL are required.');
1032 $client = \HPCloud\Transport::instance();
1034 'X-Auth-Token' => $this->
token,
1036 $response = $client->doRequest($this->
url,
'GET', $headers);
1042 $this->
bytes = $response->header(
'X-Container-Bytes-Used', 0);
1043 $this->
count = $response->header(
'X-Container-Object-Count', 0);
1056 protected function objectQuery($params = array(), $limit = NULL, $marker = NULL) {
1057 if (isset($limit)) {
1058 $params[
'limit'] = (int) $limit;
1059 if (!empty($marker)) {
1060 $params[
'marker'] = (string) $marker;
1065 $params[
'format'] =
'json';
1067 $query = http_build_query($params);
1068 $query = str_replace(
'%2F',
'/', $query);
1069 $url = $this->
url .
'?' . $query;
1071 $client = \HPCloud\Transport::instance();
1073 'X-Auth-Token' => $this->
token,
1076 $response = $client->doRequest(
$url,
'GET', $headers);
1080 if ($response->status() != 200) {
1081 throw new \HPCloud\Exception(
'An unknown exception occurred while processing the request.');
1084 $responseContent = $response->content();
1085 $json = json_decode($responseContent, TRUE);
1089 foreach ($json as $item) {
1090 if (!empty($item[
'subdir'])) {
1091 $list[] =
new Subdir($item[
'subdir'], $params[
'delimiter']);
1093 elseif (empty($item[
'name'])) {
1094 throw new \HPCloud\Exception(
'Unexpected entity returned.');
1098 $url = self::objectUrl($this->
url, $item[
'name']);
1099 $list[] = RemoteObject::newFromJSON($item, $this->
token,
$url);
1135 return new \ArrayIterator($this->
objects());
1150 'X-Auth-Token' => $this->
token,
1153 $client = \HPCloud\Transport::instance();
1156 $response = $client->doRequest(
$url,
'DELETE', $headers);
1158 catch (\HPCloud\
Transport\FileNotFoundException $fnfe) {
1162 if ($response->status() != 204) {
1163 throw new \HPCloud\Exception(
"An unknown exception occured while deleting $name.");