Commit e5fb2ad4 authored by 顾文旭's avatar 顾文旭

修改为七牛

parent 320804bd
......@@ -5,6 +5,12 @@ namespace Api\PhpServices\Ksy;
use Api\PhpUtils\Http\HttpUtil;
use Api\PhpUtils\Mon\MonUtil;
// 引入鉴权类
use Qiniu\Auth;
// 引入上传类
use Qiniu\Storage\UploadManager;
/**
* 文档: http://ydwiki.yidian-inc.com/pages/viewpage.action?pageId=68515904
* Class Ks3Api
......@@ -144,31 +150,60 @@ class Ks3Api
*/
public static function upload($type)
{
$url = self::BASE_URL . self::PATH_PIC_UPLOAD;
$timestamp = self::msectime();
$handle = fopen($_FILES['file']['tmp_name'], 'r');
$content = fread($handle, filesize($_FILES['file']['tmp_name']));
$extend = ["type" => $type];
$extendJsonStr = json_encode($extend);
$md5Str = $extendJsonStr . "&" . md5($content) . "&" . $timestamp . "&" . self::KSYUN_SK;
$signature = self::KSYUN_AK . ":" . md5($md5Str);
$params = [
"pic" => new \CURLFile($_FILES['file']['tmp_name'], $_FILES['file']['type']),
"timestamp" => $timestamp,
"signature" => $signature,
"extend" => $extendJsonStr,
// 控制台获取密钥:https://portal.qiniu.com/user/key
$accessKey = "v8h5axBiWk5aoyNV-xlWcnmAZdKOHPKeqXp2yJRc";
$secretKey = "kbTIQ2mfpzsWnV9o6iWm7gSHds3lKOWaV5jYzTB9";
$bucket = "pinyipin";
// 构建鉴权对象
$auth = new Auth($accessKey, $secretKey);
// 生成上传 Token
$token = $auth->uploadToken($bucket);
// 初始化 UploadManager 对象并进行文件的上传。
$uploadMgr = new UploadManager();
// 调用 UploadManager 的 putFile 方法进行文件的上传,该方法会判断文件大小,进而决定使用表单上传还是分片上传,无需手动配置。
list($ret, $err) = $uploadMgr->putFile($token, $_FILES["file"]["name"], $_FILES['file']['tmp_name']);
// $url = self::BASE_URL . self::PATH_PIC_UPLOAD;
// $timestamp = self::msectime();
//
// $handle = fopen($_FILES['file']['tmp_name'], 'r');
// $content = fread($handle, filesize($_FILES['file']['tmp_name']));
//
// $extend = ["type" => $type];
// $extendJsonStr = json_encode($extend);
// $md5Str = $extendJsonStr . "&" . md5($content) . "&" . $timestamp . "&" . self::KSYUN_SK;
// $signature = self::KSYUN_AK . ":" . md5($md5Str);
//
// $params = [
// "pic" => new \CURLFile($_FILES['file']['tmp_name'], $_FILES['file']['type']),
// "timestamp" => $timestamp,
// "signature" => $signature,
// "extend" => $extendJsonStr,
// ];
//
// $begin = microtime(true);
// $uploadRes = HttpUtil::post($url, $params);
// $end = microtime(true);
// $totalTime = round(($end - $begin), 4) * 1000;
// $resCode = $uploadRes["response"]['code'] === 0 ? 200 : $uploadRes["response"]['code'];
// MonUtil::proxyMon($url, $resCode, 'ks3api', $totalTime);
return [
"response" => [
"data" => [
"animated" => false,
"format" => $_FILES['file']['type'],
"image_id" => $ret['key'],
"size" => [],
"url" => "https://pingouimg.jwshq.cn/" . $ret['key'] . "?url=" . $ret['key'],
]
],
];
$begin = microtime(true);
$uploadRes = HttpUtil::post($url, $params);
$end = microtime(true);
$totalTime = round(($end - $begin), 4) * 1000;
$resCode = $uploadRes["response"]['code'] === 0 ? 200 : $uploadRes["response"]['code'];
MonUtil::proxyMon($url, $resCode, 'ks3api', $totalTime);
return $uploadRes;
}
/**
......@@ -180,61 +215,4 @@ class Ks3Api
list($msec, $sec) = explode(' ', microtime());
return (float)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000);
}
/**
* 批量获取图片
* @param $imageIds image_id 数组
* @param int $widthSize
* @param int $highSize
* @param float|int $expirationTime
* @return array
*/
public static function concurrencyPicEncryptUrl($imageIds, $widthSize = 0, $highSize = 0, $expirationTime = 60 * 60 * 24 * 365)
{
$url = self::BASE_GEN_URL . self::PATH_PIC_ENCRYPT_URL;
$timestamp = self::msectime();
$extend = [];
$extend["expiration_time"] = $expirationTime;
if (!empty($widthSize) && !empty($highSize)) {
$extend["query"] = "type=thumbnail_" . $widthSize . "x" . $highSize;
}
$extendJsonStr = json_encode($extend);
$params = [];
$urls = [];
foreach ($imageIds as $imageId) {
$md5Str = $extendJsonStr . "&" . $imageId . "&" . self::AUDIT_ID . "&" . $timestamp . "&" . self::KSYUN_SK;
$signature = self::KSYUN_AK . ":" . md5($md5Str);
$contentType = 'multipart/form-data';
$param = [
"image_id" => $imageId,
"auth_id" => self::AUDIT_ID,
"timestamp" => $timestamp,
"signature" => $signature,
"content_type" => $contentType,
"extend" => $extendJsonStr,
];
$params[$imageId] = $param;
$urls[$imageId] = $url;
}
$data = [];
if (!empty($urls)) {
$begin = microtime(true);
$encryptRes = HttpUtil::concurrencyPost($urls, $params);
$end = microtime(true);
$totalTime = round(($end - $begin), 4) * 1000;
$resCode = $encryptRes["response"]['code'] === 0 ? 200 : $encryptRes["response"]['code'];
MonUtil::proxyMon($url, $resCode, 'ks3api', $totalTime);
if (!empty($encryptRes)) {
foreach ($encryptRes as $imageId => $item) {
if (!empty($item["response"]["data"]["url"])) {
$data[$imageId] = $item["response"]["data"]["url"];
}
}
}
}
return $data;
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment