From c38eff82bc9508feb0bcf768423f3c14cd6de191 Mon Sep 17 00:00:00 2001 From: Kenneth Reitz Date: Wed, 1 Aug 2007 12:29:27 +0000 Subject: [PATCH] further tweaks added create folder capabilities --- tantan/wordpress-s3/admin-tab.html | 14 ++++---- tantan/wordpress-s3/class-plugin.php | 26 ++++++++++---- tantan/wordpress-s3/lib.s3.php | 50 ++++++++++++++++++++++++++- tantan/wordpress-s3/styles/styles.css | 9 +++-- 4 files changed, 83 insertions(+), 16 deletions(-) diff --git a/tantan/wordpress-s3/admin-tab.html b/tantan/wordpress-s3/admin-tab.html index c4e8905..f53d79e 100644 --- a/tantan/wordpress-s3/admin-tab.html +++ b/tantan/wordpress-s3/admin-tab.html @@ -34,7 +34,7 @@ function print_keys($keys, $level=0) { home / '; -$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) { @@ -70,21 +70,20 @@ if (ereg('/', $label)) $label = substr($label, strrpos($label, '/')+1); -
diff --git a/tantan/wordpress-s3/class-plugin.php b/tantan/wordpress-s3/class-plugin.php index 9ecc352..79caa3c 100644 --- a/tantan/wordpress-s3/class-plugin.php +++ b/tantan/wordpress-s3/class-plugin.php @@ -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) { diff --git a/tantan/wordpress-s3/lib.s3.php b/tantan/wordpress-s3/lib.s3.php index 71f7b54..dbcf1f9 100644 --- a/tantan/wordpress-s3/lib.s3.php +++ b/tantan/wordpress-s3/lib.s3.php @@ -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()); diff --git a/tantan/wordpress-s3/styles/styles.css b/tantan/wordpress-s3/styles/styles.css index b151d26..6e33374 100644 --- a/tantan/wordpress-s3/styles/styles.css +++ b/tantan/wordpress-s3/styles/styles.css @@ -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; +} \ No newline at end of file