config->load('qiniu', true, true); $type = isset($config['type'])?$config['type']:''; $qiniu_config = $CI->config->item('qiniu'); switch ($type){ case 'video': $this->config = $qiniu_config['video']; break; default: $this->config = $qiniu_config['img']; } } public function getConfig() { return $this->config; } /** * 获取文件当前URL地址 * @param string $filename * @param string|null $storage * @return bool|string * @throws Exception */ public function getFileUrl($filename) { $auth = new \Qiniu\Auth($this->config['access_key'], $this->config['secret_key']); $bucketMgr = new \Qiniu\Storage\BucketManager($auth); list($ret, $err) = $bucketMgr->stat($this->config['bucket'], $filename); return $err === null ? $this->getBaseUriQiniu().$filename : false; } /** * 根据配置获取到七牛云文件上传目标地址 * @param bool $isClient * @return string * @throws Exception */ public function getUploadQiniuUrl($isClient = true) { $region = $this->config['region']; $isHttps = !!$this->config['is_https']; switch ($region) { case '华东': if ($isHttps) { return $isClient ? 'https://upload.qiniup.com' : 'https://upload.qiniup.com'; } return $isClient ? 'http://upload.qiniup.com' : 'http://upload.qiniup.com'; case '华北': if ($isHttps) { return $isClient ? 'https://upload-z1.qiniup.com' : 'https://up-z1.qiniup.com'; } return $isClient ? 'http://upload-z1.qiniup.com' : 'http://up-z1.qiniup.com'; case '北美': if ($isHttps) { return $isClient ? 'https://upload-na0.qiniup.com' : 'https://up-na0.qiniup.com'; } return $isClient ? 'http://upload-na0.qiniup.com' : 'http://up-na0.qiniup.com'; case '华南': default: if ($isHttps) { return $isClient ? 'https://upload-z2.qiniup.com' : 'https://up-z2.qiniup.com'; } return $isClient ? 'http://upload-z2.qiniup.com' : 'http://up-z2.qiniup.com'; } } /** * 获取七牛云URL前缀 * @return string * @throws Exception */ public function getBaseUriQiniu() { switch (strtolower($this->config['is_https'])) { case 'https': return 'https://' . $this->config['domain'].DIRECTORY_SEPARATOR; case 'http': return 'http://' . $this->config['domain'].DIRECTORY_SEPARATOR; default: return '//' . $this->config['domain'].DIRECTORY_SEPARATOR; } } /** * 根据Key读取文件内容 * @param string $filename * @param string|null $storage * @return string|null * @throws Exception */ public function readFile($filename) { $auth = new \Qiniu\Auth($this->config['access_key'], $this->config['secret_key']); return file_get_contents($auth->privateDownloadUrl($this->getBaseUriQiniu() . $filename)); } /** * 获取文件相对名称 * @param string $local_url 文件标识 * @param string $ext 文件后缀 * @param string $pre 文件前缀(若有值需要以/结尾) * @return string */ public function getFileName($local_url, $ext = '', $pre = '') { empty($ext) && $ext = strtolower(pathinfo($local_url, 4)); return $pre . join('/', str_split(md5($local_url), 16)) . '.' . ($ext ? $ext : 'tmp'); } /** * 存储图片 * @param $filename * @param $content * @return |null * @throws Exception */ public function save($filename, $content) { $auth = new \Qiniu\Auth($this->config['access_key'], $this->config['secret_key']); $token = $auth->uploadToken($this->config['bucket']); $filename = $this->config['attch'].$filename; $uploadMgr = new \Qiniu\Storage\UploadManager(); list($result, $err) = $uploadMgr->put($token, $filename, $content); /*edit by xuxianbin 19900819 文件存在可以直接返回*/ if ($err !== null && 'file exists' != $err->message()) { debug_log('七牛云文件上传失败, ' . $err->message() . '; filename=' . $filename); return null; } $result['file'] = $filename; $result['url'] = $this->getBaseUriQiniu() . $filename; return $result; } /** * 删除图片 * @param $filename * @return bool */ public function rm($filename){ $auth = new \Qiniu\Auth($this->config['access_key'], $this->config['secret_key']); $filename = $this->config['attch'].$filename; //初始化BucketManager $bucketMgr = new \Qiniu\Storage\BucketManager($auth); $err = $bucketMgr->delete($this->config['bucket'], $filename); if ($err !== null) { debug_log("七牛云文件删除失败, " . $err->message(). "; filename={$filename}"); return false; } return true; } /** * 获取文件信息 * @param $filename * @return mixed */ public function getInfo($filename){ $auth = new \Qiniu\Auth($this->config['access_key'], $this->config['secret_key']); $bucketMgr = new \Qiniu\Storage\BucketManager($auth); $filename = $this->config['attch'].$filename; list($fileInfo, $err) = $bucketMgr->stat($this->config['bucket'], $filename); if ($err) { debug_log("七牛云获取文件信息失败, " . $err->message(). "; filename={$filename}"); return null; } return $fileInfo; } /** * 抓取网络图片 * @param $url * @param $filename * @return array */ public function fetch($url, $filename){ $auth = new \Qiniu\Auth($this->config['access_key'], $this->config['secret_key']); $filename = $this->config['attch'].$filename; //初始化BucketManager $bucketMgr = new \Qiniu\Storage\BucketManager($auth); list($result, $err) = $bucketMgr->fetch($url, $this->config['bucket'], $filename); if ($err !== null && 'file exists' != $err->message()) { debug_log('七牛云抓取网络文件失败, ' . $err->message() . ";url={$url}; filename=" . $filename); return array(); } $result['file'] = $filename; $result['url'] = $this->getBaseUriQiniu() . $filename; return $result; } /** * 获取token * @param $filename * @return string * @throws Exception */ public function getToken($filename) { $baseUrl = $this->getBaseUriQiniu(); $bucket = $this->config['bucket']; $accessKey = $this->config['access_key']; $secretKey = $this->config['secret_key']; $params = array( "scope" => "{$bucket}:{$filename}", "deadline" => 3600 + time(), "returnBody" => "{\"data\":{\"site_url\":\"{$baseUrl}$(key)\",\"file_url\":\"$(key)\"}, \"code\": \"SUCCESS\"}", ); $data = str_replace(array('+', '/'), array('-', '_'), base64_encode(json_encode($params))); return $accessKey . ':' . str_replace(array('+', '/'), array('-', '_'), base64_encode(hash_hmac('sha1', $data, $secretKey, true))) . ':' . $data; } }