mirror of
https://github.com/kennethreitz-archive/wp-s3.git
synced 2026-06-05 23:50:19 +00:00
further tweaks
added create folder capabilities
This commit is contained in:
@@ -34,7 +34,7 @@ function print_keys($keys, $level=0) {
|
||||
<?php
|
||||
$tree = $keys;
|
||||
echo '<a href="'.add_query_arg('prefix', urlencode(''), $_SERVER['REQUEST_URI']).'" class="home">home</a> / ';
|
||||
$path = '/';
|
||||
$path = '';
|
||||
$paths = preg_split('/\//', $prefix, 100, PREG_SPLIT_NO_EMPTY);
|
||||
$numPaths = count($paths);
|
||||
$i=0;
|
||||
@@ -62,7 +62,7 @@ foreach ($paths as $name) if ($name) {
|
||||
<?php
|
||||
if (is_array($prefixes)) foreach ($prefixes as $prefix):
|
||||
$label = substr($prefix, strrpos(trim($prefix, '/'), '/')+1);
|
||||
$label = ereg_replace($path, "", '/'.$prefix);
|
||||
$label = ($path ? ereg_replace($path, "", '/'.$prefix) : $prefix);
|
||||
$label = trim($prefix, '/');
|
||||
if (ereg('/', $label)) $label = substr($label, strrpos($label, '/')+1);
|
||||
?>
|
||||
@@ -70,21 +70,20 @@ if (ereg('/', $label)) $label = substr($label, strrpos($label, '/')+1);
|
||||
<?php
|
||||
endforeach;
|
||||
?>
|
||||
<!--
|
||||
<li id="createFolder">
|
||||
<a href="#" class="add" onclick="return s3_toggleCreateFolder();">create folder</a>
|
||||
<div class="form">
|
||||
<input type="hidden" name="prefix" value="<?php echo htmlentities($_GET['prefix']);?>">
|
||||
<input type="text" name="newfolder" id="newfolder" />
|
||||
<input type="submit" value="add" />
|
||||
<input type="submit" value="create" />
|
||||
</div>
|
||||
</li>
|
||||
-->
|
||||
</ul>
|
||||
</form>
|
||||
</div>
|
||||
<div class="files">
|
||||
<ul id="keys">
|
||||
<?php if (is_array($keys)) foreach ($keys as $i => $file):
|
||||
<?php if (count($keys)): foreach ($keys as $i => $file):
|
||||
$file = '/'.$file;
|
||||
$url = 'http://'.$accessDomain.$file;
|
||||
$label = substr($file, strrpos($file, '/')+1);
|
||||
@@ -100,6 +99,9 @@ class="file <?php echo ereg_replace('/', ' ', $meta[$i]['content-type']);?>"
|
||||
title="<?php echo $meta[$i]['date'];?> - <?php echo $meta[$i]['content-length'];?> bytes"><?php echo $label;?></a>
|
||||
</li>
|
||||
<?php endforeach;?>
|
||||
<?php else:?>
|
||||
<li class="empty">no files in this folder</li>
|
||||
<?php endif;?>
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -157,7 +157,8 @@ class TanTanWordPressS3Plugin {
|
||||
require_once(dirname(__FILE__).'/lib.s3.php');
|
||||
$this->s3 = new TanTanS3($this->options['key'], $this->options['secret']);
|
||||
|
||||
$this->s3->putObjectStream($this->options['bucket'], $_GET['prefix'].$_POST['newfolder'], '/dev/null');
|
||||
$this->s3->putPrefix($this->options['bucket'], $_POST['prefix'].$_POST['newfolder']);
|
||||
//echo ($this->options['bucket']. " : ". $_POST['prefix'].$_POST['newfolder']);
|
||||
}
|
||||
}
|
||||
function tab() {
|
||||
@@ -172,7 +173,7 @@ class TanTanWordPressS3Plugin {
|
||||
|
||||
$prefix = $_GET['prefix'] ? $_GET['prefix'] : '';
|
||||
|
||||
list($prefixes, $keys, $meta) = $this->getKeys($prefix);
|
||||
list($prefixes, $keys, $meta, $privateKeys) = $this->getKeys($prefix);
|
||||
include(dirname(__FILE__).'/admin-tab.html');
|
||||
}
|
||||
|
||||
@@ -214,13 +215,22 @@ class TanTanWordPressS3Plugin {
|
||||
if ($this->s3->parsed_xml->CommonPrefixes) foreach ($this->s3->parsed_xml->CommonPrefixes as $content) {
|
||||
$prefixes[] = (string) $content->Prefix;
|
||||
}
|
||||
natcasesort($prefixes);
|
||||
|
||||
if ($this->s3->parsed_xml->Contents) foreach ($this->s3->parsed_xml->Contents as $content) {
|
||||
$key = (string) $content->Key;
|
||||
|
||||
if ($this->isPublic($key)) $keys[] = $key;
|
||||
else $privateKeys[] = $key;
|
||||
else {
|
||||
if (!($p1 = ereg('^\.', $key)) &&
|
||||
!($p2 = ereg('_\$folder\$$', $key)) &&
|
||||
!($p3 = ereg('placeholder.ns3', $key))) {
|
||||
$privateKeys[] = $key;
|
||||
} elseif ($p2) {
|
||||
$prefix = ereg_replace('(_\$folder\$$)', '/', $key);
|
||||
if (!in_array($prefix, $prefixes)) $prefixes[] = $prefix;
|
||||
} else {
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
if ($this->options['permissions'] == 'public') {
|
||||
foreach ($privateKeys as $key) {
|
||||
@@ -229,14 +239,16 @@ class TanTanWordPressS3Plugin {
|
||||
}
|
||||
}
|
||||
|
||||
natcasesort($keys);
|
||||
foreach ($keys as $i => $key) {
|
||||
$meta[$i] = $this->s3->getMetadata($this->options['bucket'], $key);
|
||||
}
|
||||
natcasesort($keys);
|
||||
natcasesort($prefixes);
|
||||
//print_r($prefixes);
|
||||
//print_r($keys);
|
||||
//print_r($meta);
|
||||
return array($prefixes, $keys, $meta);
|
||||
|
||||
return array($prefixes, $keys, $meta, $privateKeys);
|
||||
}
|
||||
|
||||
function isPublic($key) {
|
||||
|
||||
@@ -290,8 +290,55 @@ return true;
|
||||
curl_close($curl_inst);
|
||||
}
|
||||
function stream_function($handle, $fd, $length){return fread($this->fp, $length);}
|
||||
|
||||
function putPrefix($bucket, $prefix){
|
||||
$ret = $this->send($bucket."/".urlencode($prefix.'_$folder$'), '', 'PUT', array('Content-Type' => '', 'Content-Length' => 0));
|
||||
if ($ret == 200) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
function send($resource, $args='', $method='GET') {
|
||||
function putObject($bucket, $key, $filePath, $contentType, $contentLength, $acl, $metadataArray, $md5){
|
||||
sort($metadataArray);
|
||||
$resource = $bucket."/".urlencode($key);
|
||||
$req = & new HTTP_Request($this->serviceUrl.$resource);
|
||||
$req->setMethod("PUT");
|
||||
$httpDate = gmdate("D, d M Y G:i:s T");
|
||||
$req->addHeader("Date", $httpDate);
|
||||
$req->addHeader("Content-Type", $contentType);
|
||||
$req->addHeader("Content-Length", $contentLength);
|
||||
$req->addHeader("x-amz-acl", $acl);
|
||||
if($md5){
|
||||
$MD5 = $this->hex2b64(md5_file($filePath));
|
||||
$req->addHeader("Content-MD5", $MD5);
|
||||
}
|
||||
$req->setBody(file_get_contents($filePath));
|
||||
$stringToSign="PUT\n$MD5\n$contentType\n$httpDate\nx-amz-acl:$acl\n";
|
||||
foreach($metadataArray as $current){
|
||||
if($current!=""){
|
||||
$stringToSign.="x-amz-meta-$current\n";
|
||||
$header = substr($current,0,strpos($current,':'));
|
||||
$meta = substr($current,strpos($current,':')+1,strlen($current));
|
||||
$req->addHeader("x-amz-meta-$header", $meta);
|
||||
}
|
||||
}
|
||||
$stringToSign.="/$resource";
|
||||
$signature = $this->constructSig($stringToSign);
|
||||
$req->addHeader("Authorization", "AWS " . $this->accessKeyId . ":" . $signature);
|
||||
$req->sendRequest();
|
||||
$this->responseCode = $req->getResponseCode();
|
||||
$this->responseString = $req->getResponseBody();
|
||||
$this->parsed_xml = simplexml_load_string($this->responseString);
|
||||
if ($this->responseCode == 200) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
function send($resource, $args='', $method='GET', $headers=false) {
|
||||
$method=strtoupper($method);
|
||||
$httpDate = gmdate("D, d M Y G:i:s T");
|
||||
$signature = $this->constructSig("$method\n\n\n$httpDate\n/$resource");
|
||||
@@ -300,6 +347,7 @@ return true;
|
||||
$this->req->setMethod($method);
|
||||
$this->req->addHeader("Date", $httpDate);
|
||||
$this->req->addHeader("Authorization", "AWS " . $this->accessKeyId . ":" . $signature);
|
||||
if (is_array($headers)) foreach ($headers as $key => $header) $this->req->addHeader($key, $header);
|
||||
$this->req->sendRequest();
|
||||
if ($method=='GET') {
|
||||
$this->parsed_xml = simplexml_load_string($this->req->getResponseBody());
|
||||
|
||||
@@ -76,14 +76,14 @@
|
||||
.folders li {
|
||||
float:left;
|
||||
margin-right:10px;
|
||||
font-size:0.9em;
|
||||
font-size:11px;
|
||||
|
||||
}
|
||||
.folders li a {
|
||||
color:#555;
|
||||
border:0;
|
||||
text-decoration:none;
|
||||
background:url(folder.gif) no-repeat 0px -2px;
|
||||
background:url(folder.gif) no-repeat 0px -3px;
|
||||
padding:0 0 0 20px;
|
||||
}
|
||||
.folders li a:hover {
|
||||
@@ -128,3 +128,8 @@
|
||||
.files ul li a:hover {
|
||||
background:#eee;
|
||||
}
|
||||
.files ul li.empty {
|
||||
color:#888;
|
||||
letter-spacing:1px;
|
||||
font-style:italic;
|
||||
}
|
||||
Reference in New Issue
Block a user