-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathblob-cache-handler.php
More file actions
138 lines (111 loc) · 4.43 KB
/
Copy pathblob-cache-handler.php
File metadata and controls
138 lines (111 loc) · 4.43 KB
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
<?php
$path = dirname(__FILE__) . '\library\dependencies';
require_once 'library/WindowsAzure/WindowsAzure.php';
set_include_path(get_include_path() . PATH_SEPARATOR . $path);
use WindowsAzure\Common\ServicesBuilder;
use WindowsAzure\Blob\Models\CreateBlobOptions;
use WindowsAzure\Blob\Models\ListBlobsOptions;
class PN_Blob_Cache_Handler {
private $account_name;
private $account_key;
private $container;
private $connection_string;
private $blob_service;
private $remote_cache_endpoint = NULL;
private $content_type = NULL;
private $headers_as_json;
public function __construct( ){
$this->account_name = getenv("ProjectNamiBlobCache.StorageAccount");
$this->account_key = getenv("ProjectNamiBlobCache.StorageKey");
$this->container = getenv("ProjectNamiBlobCache.StorageContainer");
$this->connection_string = 'DefaultEndpointsProtocol=http;AccountName=' . $this->account_name . ';AccountKey=' . $this->account_key;
$this->blob_service = ServicesBuilder::getInstance()->createBlobService( $this->connection_string );
}
/*
* This takes an array of headers and prepares the data as an array of json data objects containing name & value properties
* It also looks for the "Content-Type" declared in the header array so we can set that value for the blob
*
* SETS
* $this->content_type
* $this->headers_as_json
*/
public function prepare_headers( $headers ){
if( isset( $headers ) ){
foreach( $headers as $x ){
$name_val_pair = explode( ": ", $x );
$header = (object) array( 'name' => trim( $name_val_pair[0] ), 'value' => trim( $name_val_pair[1] ) );
if( strtolower( $header->name ) == "content-type" ){
$content_type_parts = explode( "; ", $header->value );
$this->content_type = $content_type_parts[0];
}
$result[] = $header;
}
$this->headers_as_json = json_encode( $result );
return TRUE;
}
return FALSE;
}
public function pn_blob_cache_set( $key, $data, $expire, $headers ) {
$options = new CreateBlobOptions();
//Set metadata and content-type for blob if header array was provided
if( $this->prepare_headers( $headers ) ){
if( isset( $this->content_type )){
$options->setBlobContentType( $this->content_type );
}
$options->setCacheControl( "max-age=".$expire );
$options->setMetadata( array( 'Projectnamicacheduration' => $expire, 'Headers' => $this->headers_as_json ) );
}
//If we don't have header info, we will only set the cache expiration
else{
$options->setMetadata( array( 'Projectnamicacheduration' => $expire ) );
}
try {
//Upload blob
$this->blob_service->createBlockBlob($this->container, $key, $data, $options);
return true;
}
catch(ServiceException $e){
$this->pn_handle_exception( $e );
}
}
public function pn_blob_cache_get( $key ){
$blob = $this->blob_service->getBlob( $this->container, $key );
$blob_contents = stream_get_contents( $blob->getContentStream() );
return $blob_contents;
}
public function pn_blob_cache_del( $key ){
if ( $this->pn_blob_cache_exists( $key ) ){
try{
$this->blob_service->deleteBlob( $this->container, $key );
return TRUE;
}
catch( ServiceException $e ){
$this->pn_handle_exception( $e );
}
}
return FALSE;
}
public function pn_blob_cache_exists( $key ){
try{
$blob_list_options = new ListBlobsOptions();
$blob_list_options->setPrefix( $key );
$blob_list = $this->blob_service->listBlobs( $this->container, $blob_list_options );
$blobs = $blob_list->getBlobs();
foreach($blobs as $blob){
if( $blob->getname() == $key ){
return TRUE;
}
}
}
catch( ServiceException $e ){
$this->pn_handle_exception( $e );
}
return FALSE;
}
public function pn_handle_exception( $e ){
$code = esc_html( $e->getCode() );
$error_message = esc_html( $e->getMessage() );
echo $code.": ".$error_message."<br />";
}
}
?>