Skip to content

Commit

Permalink
Fix code.
Browse files Browse the repository at this point in the history
  • Loading branch information
oawu committed Jun 12, 2016
1 parent 8ecb603 commit 45ae5aa
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 25 deletions.
15 changes: 11 additions & 4 deletions cmd/command
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,27 @@ git add -A && git commit -m 'Fix html code.' && git push origin master



◎ S3 Command
================================================================================
➜ Don't minify
cd cmd && php put.php -b {bucket} -a {access} -s {secret} && cd ..



◎ No installed npm
================================================================================
➜ No gh-pages
➜ No gh-pages branch
git branch -v gh-pages && git checkout gh-pages && cd cmd && sudo npm install . && gulp minify && gulp gh-pages && cd ../ && git add -A && git commit -m 'Minify js、html, fix gh-pages path bug.' && git push origin gh-pages --force && git checkout master

➜ Has gh-pages
➜ Has gh-pages branch
git branch -D gh-pages && git branch -v gh-pages && git checkout gh-pages && cd cmd && sudo npm install . && gulp minify && gulp gh-pages && cd ../ && git add -A && git commit -m 'Minify js、html, fix gh-pages path bug.' && git push origin gh-pages --force && git checkout master



◎ Has installed npm
================================================================================
➜ No gh-pages
➜ No gh-pages branch
git branch -v gh-pages && git checkout gh-pages && cd cmd && gulp minify && gulp gh-pages && cd ../ && git add -A && git commit -m 'Minify js、html, fix gh-pages path bug.' && git push origin gh-pages --force && git checkout master

➜ Has gh-pages
➜ Has gh-pages branch
git branch -D gh-pages && git branch -v gh-pages && git checkout gh-pages && cd cmd && gulp minify && gulp gh-pages && cd ../ && git add -A && git commit -m 'Minify js、html, fix gh-pages path bug.' && git push origin gh-pages --force && git checkout master
28 changes: 13 additions & 15 deletions cmd/libs/S3.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ class S3 {
private static $__secret_key = NULL;

public static function init ($access_key, $secret_key) {
// if (!(isset ($key['access_key']) && isset ($key['secret_key'])))
// return trigger_error (sprintf ("S3::init(): [%s] %s", 999, 'No access_key, secret_key'), E_USER_WARNING);
self::$__access_key = $access_key;
self::$__secret_key = $secret_key;
}
Expand All @@ -30,7 +28,7 @@ public static function listBuckets ($detailed = false) {
$rest = $rest->getResponse ();

if (($rest->error !== false) || ($rest->code !== 200))
return trigger_error (sprintf ("S3::listBuckets(): [%s] %s", $rest->code, 'Unexpected HTTP status'), E_USER_WARNING);
throw new Exception (sprintf ("S3::listBuckets(): [%s] %s", $rest->code, 'Unexpected HTTP status'));

$results = array ();
if (!isset ($rest->body->Buckets))
Expand Down Expand Up @@ -65,7 +63,7 @@ public static function getBucket ($bucket, $prefix = null, $marker = null, $maxK

$response = $rest->getResponse ();
if (($response->error !== false) || ($response->code !== 200))
return trigger_error (sprintf ("S3::getBucket(): [%s] %s", $response->code, 'Unexpected HTTP status'), E_USER_WARNING);
throw new Exception (sprintf ("S3::getBucket(): [%s] %s", $response->code, 'Unexpected HTTP status'));

$results = array ();

Expand Down Expand Up @@ -123,7 +121,7 @@ public static function putBucket ($bucket, $acl = self::ACL_PRIVATE, $location =
$rest = $rest->getResponse ();

if (($rest->error !== false) || ($rest->code !== 200))
return trigger_error (sprintf ("S3::putBucket(%s, $s, $s): [%s] %s", $bucket, $acl, $location, $rest->code, 'Unexpected HTTP status'));
throw new Exception (sprintf ("S3::putBucket(%s, $s, $s): [%s] %s", $bucket, $acl, $location, $rest->code, 'Unexpected HTTP status'));

return true;
}
Expand All @@ -132,7 +130,7 @@ public static function deleteBucket ($bucket) {
$rest = new S3Request ('DELETE', $bucket);
$rest = $rest->getResponse ();
if (($rest->error !== false) || ($rest->code !== 200))
return trigger_error (sprintf ("S3::deleteBucket(%s): [%s] %s", $bucket, $rest->code, 'Unexpected HTTP status'));
throw new Exception (sprintf ("S3::deleteBucket(%s): [%s] %s", $bucket, $rest->code, 'Unexpected HTTP status'));

return true;
}
Expand All @@ -142,7 +140,7 @@ public static function fileMD5 ($filePath) {
}
public static function putFile ($filePath, $bucket, $s3Path, $acl = self::ACL_PUBLIC_READ, $metaHeaders = array (), $requestHeaders = array ()) {
if (!(file_exists ($filePath) && is_file ($filePath) && is_readable ($filePath)))
return trigger_error ('S3::putFile(): Unable to open input file: ' . $filePath, E_USER_WARNING);
throw new Exception ('S3::putFile(): Unable to open input file: ' . $filePath);

$rest = new S3Request ('PUT', $bucket, $s3Path);

Expand All @@ -158,12 +156,12 @@ public static function putFile ($filePath, $bucket, $s3Path, $acl = self::ACL_PU
foreach ($metaHeaders as $h => $v) $rest->setAmzHeader ('x-amz-meta-' . $h, $v);

if (!(($rest->size >= 0) && (($rest->fp !== false) || ($rest->data !== false))))
return trigger_error (sprintf ("S3::putObject(): [%s] %s", 0, 'Missing input parameters'), E_USER_WARNING);
throw new Exception (sprintf ("S3::putObject(): [%s] %s", 0, 'Missing input parameters'));

$rest->getResponse ();

if (($rest->response->error !== false) || ($rest->response->code !== 200))
return trigger_error (sprintf ("S3::putObject(): [%s] %s", $rest->response->code, 'Unexpected HTTP status'), E_USER_WARNING);
throw new Exception (sprintf ("S3::putObject(): [%s] %s", $rest->response->code, 'Unexpected HTTP status'));

return true;
}
Expand All @@ -182,12 +180,12 @@ public static function getObject($bucket, $uri, $saveTo = false) {

if ($saveTo !== false)
if (($rest->fp = @fopen ($saveTo, 'wb')) !== false) $rest->file = realpath ($saveTo);
else return trigger_error (sprintf ("S3::getObject(%s, %s): [%s] %s",$bucket, $uri, 0, 'Unable to open save file for writing: ' . $saveTo));
throw new Exception (sprintf ("S3::getObject(%s, %s): [%s] %s",$bucket, $uri, 0, 'Unable to open save file for writing: ' . $saveTo));

$rest->getResponse();

if (($rest->response->error !== false) || ($rest->response->code !== 200))
return trigger_error (sprintf ("S3::getObject(%s, %s): [%s] %s",$bucket, $uri, $rest->response->code, 'Unexpected HTTP status'));
throw new Exception (sprintf ("S3::getObject(%s, %s): [%s] %s",$bucket, $uri, $rest->response->code, 'Unexpected HTTP status'));

return $rest->response;
}
Expand All @@ -197,7 +195,7 @@ public static function getObjectInfo ($bucket, $uri, $returnInfo = true) {
$rest = $rest->getResponse ();

if (($rest->error !== false) || (($rest->code !== 200) && ($rest->code !== 404)))
return trigger_error (sprintf ("S3::getObjectInfo(%s, %s): [%s] %s", $bucket, $uri, $rest->code, 'Unexpected HTTP status'), E_USER_WARNING);
throw new Exception (sprintf ("S3::getObjectInfo(%s, %s): [%s] %s", $bucket, $uri, $rest->code, 'Unexpected HTTP status'));

return $rest->code == 200 ? $returnInfo ? $rest->headers : true : false;
}
Expand All @@ -221,7 +219,7 @@ public static function copyObject ($srcBucket, $srcUri, $bucket, $uri, $acl = se
$rest = $rest->getResponse ();

if (($rest->error !== false) || ($rest->code !== 200))
return trigger_error (sprintf ("S3::copyObject(%s, %s, %s, %s): [%s] %s", $srcBucket, $srcUri, $bucket, $uri, $rest->code, 'Unexpected HTTP status'), E_USER_WARNING);
throw new Exception (sprintf ("S3::copyObject(%s, %s, %s, %s): [%s] %s", $srcBucket, $srcUri, $bucket, $uri, $rest->code, 'Unexpected HTTP status'));

return isset ($rest->body->LastModified, $rest->body->ETag) ? array ('time' => date ('Y-m-d H:i:s', strtotime ((String) $rest->body->LastModified)), 'hash' => substr ((String) $rest->body->ETag, 1, -1)) : false;
}
Expand All @@ -231,7 +229,7 @@ public static function getBucketLocation ($bucket) {
$rest->setParameter ('location', null);
$rest = $rest->getResponse ();
if (($rest->error !== false) || ($rest->code !== 200))
return trigger_error (sprintf ("S3::getBucketLocation(%s): [%s] %s", $bucket, $rest->code, 'Unexpected HTTP status'), E_USER_WARNING);
throw new Exception (sprintf ("S3::getBucketLocation(%s): [%s] %s", $bucket, $rest->code, 'Unexpected HTTP status'));
return (isset ($rest->body[0]) && (((String)$rest->body[0]) !== '')) ? (String)$rest->body[0] : 'US';
}

Expand All @@ -240,7 +238,7 @@ public static function deleteObject ($bucket, $uri) {
$rest = $rest->getResponse ();

if (($rest->error !== false) || ($rest->code !== 204))
return trigger_error (sprintf ("S3::deleteObject(): [%s] %s", $rest->code, 'Unexpected HTTP status'), E_USER_WARNING);
throw new Exception (sprintf ("S3::deleteObject(): [%s] %s", $rest->code, 'Unexpected HTTP status'));

return true;
}
Expand Down
26 changes: 20 additions & 6 deletions cmd/put.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,16 @@
// // ========================================================================

echo '' . color ('列出 S3 上所有檔案', 'g');
$s3_files = array_filter (S3::getBucket ($bucket), function ($s3_file) {
return preg_match ('/^' . NAME . '\//', $s3_file['name']);
});
echo color ('(' . ($c = count ($s3_files)) . ')', 'g') . ' - 100% - ' . color ('取得檔案成功!', 'C') . "\n";
echo str_repeat ('-', 80) . "\n";
try {
$s3_files = array_filter (S3::getBucket ($bucket), function ($s3_file) {
return preg_match ('/^' . NAME . '\//', $s3_file['name']);
});
echo color ('(' . ($c = count ($s3_files)) . ')', 'g') . ' - 100% - ' . color ('取得檔案成功!', 'C') . "\n";
echo str_repeat ('-', 80) . "\n";
} catch (Exception $e) {
echo ' - ' . color ('取得檔案失敗!', 'R') . "\n";
exit ();
}

// // ========================================================================
// // ========================================================================
Expand Down Expand Up @@ -117,7 +122,11 @@
$i = 0;
if (array_filter (array_map (function ($file) use ($bucket, &$i, $c) {
echo sprintf ("\r" . '' . color ('上傳檔案', 'g') . color ('(' . $c . ')', 'g') . " - % 3d%% ", ceil ((++$i * 100) / $c));
return !S3::putFile ($file['path'], $bucket, NAME . DIRECTORY_SEPARATOR . $file['uri']);
try {
return !S3::putFile ($file['path'], $bucket, NAME . DIRECTORY_SEPARATOR . $file['uri']);
} catch (Exception $e) {
return true;
}
}, $upload_files))) {
echo '- ' . color ('上傳發生錯誤!', 'r') . "\n";
echo str_repeat ('=', 80) . "\n";
Expand Down Expand Up @@ -151,6 +160,11 @@
echo '- ' . (array_filter (array_map (function ($file) use ($bucket, &$i, $c) {
echo sprintf ("\r" . '' . color ('刪除 S3 上需要刪除的檔案(' . $c . ')', 'g') . " - % 3d%% ", ceil ((++$i * 100) / $c));
return !S3::deleteObject ($bucket, $file['name']);
try {
return !S3::deleteObject ($bucket, $file['name']);
} catch (Exception $e) {
return true;
}
}, $delete_files)) ? color ('刪除 S3 上需要刪除的檔案失敗!', 'r') : color ('刪除 S3 上需要刪除的檔案成功!', 'C')) . "\n";
echo str_repeat ('-', 80) . "\n";

Expand Down

0 comments on commit 45ae5aa

Please sign in to comment.