Skip to content

Commit

Permalink
Optimize timezone.
Browse files Browse the repository at this point in the history
Remove date_default_timezone_set()
  • Loading branch information
freyo committed Jun 11, 2017
1 parent 5dcae14 commit 4e176db
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/Client/Auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ private static function createSignature(
return self::AUTH_SECRET_ID_KEY_ERROR;
}

$now = time();
$now = Cosapi::time();
$random = rand();
$plainText = "a=$appId&k=$secretId&e=$expiration&t=$now&r=$random&f=$fileId&b=$bucket";
$bin = hash_hmac('SHA1', $plainText, $secretKey, true);
Expand Down
28 changes: 19 additions & 9 deletions src/Client/Cosapi.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace Freyo\Flysystem\QcloudCOSv4\Client;

date_default_timezone_set('PRC');

class Cosapi
{
//计算sign签名的时间参数
Expand All @@ -19,6 +17,18 @@ class Cosapi
private static $timeout = 60;
private static $region = 'gz'; // default region is guangzou

/**
* 获取当前指定时区的 Unix 时间戳
*
* @param string $timezone
*
* @return int
*/
public static function time($timezone = 'Asia/Shanghai')
{
return date_create('now', timezone_open($timezone))->getTimestamp();
}

/**
* 设置HTTP请求超时时间.
*
Expand Down Expand Up @@ -97,7 +107,7 @@ public static function createFolder($bucket, $folder, $bizAttr = null)

$folder = self::normalizerPath($folder, true);
$folder = self::cosUrlEncode($folder);
$expired = time() + self::EXPIRED_SECONDS;
$expired = self::time() + self::EXPIRED_SECONDS;
$url = self::generateResUrl($bucket, $folder);
$signature = Auth::createReusableSignature($expired, $bucket);

Expand Down Expand Up @@ -296,7 +306,7 @@ private static function uploadFile($bucket, $srcPath, $dstPath, $bizAttr = null,
];
}

$expired = time() + self::EXPIRED_SECONDS;
$expired = self::time() + self::EXPIRED_SECONDS;
$url = self::generateResUrl($bucket, $dstPath);
$signature = Auth::createReusableSignature($expired, $bucket);
$fileSha = hash_file('sha1', $srcPath);
Expand Down Expand Up @@ -347,9 +357,9 @@ private static function uploadBySlicing($bucket, $srcFpath, $dstFpath, $bizAttr
$sliceCount = ceil($fileSize / $sliceSize);
// expiration seconds for one slice mutiply by slice count
// will be the expired seconds for whole file
$expiration = time() + (self::EXPIRED_SECONDS * $sliceCount);
if ($expiration >= (time() + 10 * 24 * 60 * 60)) {
$expiration = time() + 10 * 24 * 60 * 60;
$expiration = self::time() + (self::EXPIRED_SECONDS * $sliceCount);
if ($expiration >= (self::time() + 10 * 24 * 60 * 60)) {
$expiration = self::time() + 10 * 24 * 60 * 60;
}
$signature = Auth::createReusableSignature($expiration, $bucket);

Expand Down Expand Up @@ -423,7 +433,7 @@ private static function uploadBySlicing($bucket, $srcFpath, $dstFpath, $bizAttr
private static function listBase($bucket, $path, $num = 20, $pattern = 'eListBoth', $order = 0, $context = null)
{
$path = self::cosUrlEncode($path);
$expired = time() + self::EXPIRED_SECONDS;
$expired = self::time() + self::EXPIRED_SECONDS;
$url = self::generateResUrl($bucket, $path);
$signature = Auth::createReusableSignature($expired, $bucket);

Expand Down Expand Up @@ -545,7 +555,7 @@ private static function updateBase(
private static function statBase($bucket, $path)
{
$path = self::cosUrlEncode($path);
$expired = time() + self::EXPIRED_SECONDS;
$expired = self::time() + self::EXPIRED_SECONDS;
$url = self::generateResUrl($bucket, $path);
$signature = Auth::createReusableSignature($expired, $bucket);

Expand Down

0 comments on commit 4e176db

Please sign in to comment.