Qiniu.php key = sha1(time() . rand(0, 10000)); $this-auth = new QiniuAuth($config['key'], $config['secret']); $this-bucket = $config['bucket']; $this-domain = $config['domain']; } /** * 单例模式 * @return Kindle * @author Mr.Cong */ sta
key = sha1(time() . rand(0, 10000)); $this->auth = new QiniuAuth($config['key'], $config['secret']); $this->bucket = $config['bucket']; $this->domain = $config['domain']; } /** * 单例模式 * @return Kindle * @author Mr.Cong */ static public function getInstance() { if (is_null(self::$_instance) || isset (self::$_instance)) { self::$_instance = new self(); } return self::$_instance; } /** * 远程抓取文件然后保存到七牛云储存 * @param $url * @author Mr.Cong */ public function fetch($url = '') { $extension = $this->getExt($url); /** * 根据URL从远处抓取文件并保存到Bucket */ $key = sprintf('avatar/%s/%s/%s%s', date('Y'), date('m'), $this->key, $extension); $uploadMgr = new BucketManager($this->auth); list($ret, $err) = $uploadMgr->fetch($url, $this->bucket, $key); if ($err !== null) { $data = array( 'status' => 0, 'msg' => $err, ); return $data; } else { $data = array( 'status' => 1, 'url' => $this->domain . $ret['key'], ); return $data; } } /** * 删除Object * @param $bucket * @param $key * @return [如果删除失败则返回失败信息,如果成功,则什么也不返回] * @author Mr.Cong */ public function destroy($bucket,$key) { $objectMgr = new BucketManager($this->auth); $ret = $objectMgr->delete($bucket,$key); if ($ret != null) { return $ret; } } /** * 七牛云储存上传文件 * @param string $filePath * @throws Exception * @author Mr.Cong */ public function upload($filePath = '') { $token = $this->getToken(); // 初始化 UploadManager 对象并进行文件的上传。 $uploadMgr = new UploadManager(); list($ret, $err) = $uploadMgr->putFile($token, $this->key, $filePath); if ($err !== null) { $data = array( 'status' => 0, 'msg' => $err, ); return $data; } else { $data = array( 'status' => 1, 'url' => $this->domain . $ret['key'], ); return $data; } } /** * 生成Token * @return string * @author Mr.Cong */ public function getToken() { $token = $this->auth->uploadToken($this->bucket); return $token; } /** * 根据URL获取文件MIME TYPE * @param $url * @return string * @author Mr.Cong */ public function getExt($url) { /** * 如果是linux/Unix系统或者Mac系统,则直接运行curl,高性能 */ if (function_exists('curl_exec') && in_array(PHP_OS, array('Darwin', 'FreeBSD', 'Linux', 'Unix'))) { $contentType = `curl -Is '${url}' |grep "Content-Type:"`; $mime = @explode(':', $contentType); $mimeType = trim($mime[1]); } else { /** * 否则如果是win的,性能就比较Low了 */ $buffer = @file_get_contents($url); $finfo = new \finfo(FILEINFO_MIME_TYPE); $mime = $finfo->buffer($buffer); $mimeType = trim($mime); } /** * 根据MIME TYPE 获取扩展名 */ $extension = new Extension(); $ext = $extension->getExt($mimeType); return $ext; } }