Skip to content

Commit 311f73b

Browse files
committed
Merge pull request phingofficial#129 from Sitebase/master
S3Put Content Type support
2 parents 8db052d + b99a12c commit 311f73b

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

classes/phing/tasks/ext/Service/Amazon/S3/S3PutTask.php

+49
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,35 @@ class S3PutTask extends Service_Amazon_S3
8484
* @access protected
8585
*/
8686
protected $_acl = 'private';
87+
88+
/**
89+
* File content type
90+
* Use this to set the content type of your static files
91+
* Set contentType to "auto" if you want to autodetect the content type based on the source file extension
92+
*
93+
* (default value: 'binary/octet-stream')
94+
*
95+
* @var string
96+
* @access protected
97+
*/
98+
protected $_contentType = 'binary/octet-stream';
99+
100+
/**
101+
* Extension content type mapper
102+
*
103+
* @var array
104+
* @access protected
105+
*/
106+
protected $_extensionContentTypeMapper = array(
107+
'js' => 'application/x-javascript',
108+
'css' => 'text/css',
109+
'html' => 'text/html',
110+
'gif' => 'image/gif',
111+
'png' => 'image/png',
112+
'jpg' => 'image/jpeg',
113+
'jpeg' => 'image/jpeg',
114+
'txt' => 'text/plain'
115+
);
87116

88117
public function setSource($source)
89118
{
@@ -152,6 +181,25 @@ public function getAcl()
152181
{
153182
return $this->_acl;
154183
}
184+
185+
public function setContentType($contentType)
186+
{
187+
$this->_contentType = $contentType;
188+
}
189+
190+
public function getContentType()
191+
{
192+
if($this->_contentType === 'auto') {
193+
$ext = strtolower(substr(strrchr($this->getSource(), '.'), 1));
194+
if(isset($this->_extensionContentTypeMapper[$ext])) {
195+
return $this->_extensionContentTypeMapper[$ext];
196+
} else {
197+
return 'binary/octet-stream';
198+
}
199+
} else {
200+
return $this->_contentType;
201+
}
202+
}
155203

156204
public function setCreateBuckets($createBuckets)
157205
{
@@ -260,6 +308,7 @@ protected function saveObject($object, $data)
260308
$object = $this->getObjectInstance($object);
261309
$object->data = $data;
262310
$object->acl = $this->getAcl();
311+
$object->contentType = $this->getContentType();
263312
$object->save();
264313

265314
if(!$this->isObjectAvailable($object->key)) {

0 commit comments

Comments
 (0)