Qiniu.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. namespace Api\Storage\Qiniu;
  3. vendor('Qiniu.autoload');
  4. use app\core\util\SystemConfigService;
  5. use think\Request;
  6. use Qiniu\Auth;
  7. use Qiniu\Storage\BucketManager;
  8. use Qiniu\Storage\UploadManager;
  9. use Qiniu\Config;
  10. use think\Cache;
  11. /**
  12. * TODO 七牛云上传
  13. * Class Qiniu
  14. * @package Api\Storage\Qiniu
  15. */
  16. class Qiniu
  17. {
  18. protected static $accessKey;
  19. protected static $secretKey;
  20. protected static $auth = null;
  21. //TODO 空间域名 Domain
  22. protected static $uploadUrl;
  23. //TODO 存储空间名称 公开空间
  24. protected static $storageName;
  25. /**
  26. * TODO 初始化
  27. * @return null|Auth
  28. * @throws \Exception
  29. */
  30. protected static function autoInfo(){
  31. if(($storageName = Cache::get('storageName')) && ($uploadUrl = Cache::get('uploadUrl')) && ($accessKey = Cache::get('accessKey')) && ($secretKey = Cache::get('secretKey'))){
  32. self::$accessKey = $accessKey;
  33. self::$secretKey = $secretKey;
  34. self::$uploadUrl = $uploadUrl;
  35. self::$storageName = $storageName;
  36. }else{
  37. self::$accessKey = trim(SystemConfigService::get('accessKey'));
  38. self::$secretKey = trim(SystemConfigService::get('secretKey'));
  39. self::$uploadUrl = trim(SystemConfigService::get('uploadUrl')).'/';
  40. self::$storageName = trim(SystemConfigService::get('storage_name'));
  41. Cache::set('accessKey',self::$accessKey);
  42. Cache::set('secretKey',self::$secretKey);
  43. Cache::set('uploadUrl',self::$uploadUrl);
  44. Cache::set('storageName',self::$storageName);
  45. }
  46. if(!self::$accessKey || !self::$secretKey || !self::$uploadUrl || !self::$storageName){
  47. exception('请设置 secretKey 和 accessKey 和 空间域名 和 存储空间名称');
  48. }
  49. if(self::$auth == null) self::$auth = new Auth(self::$accessKey,self::$secretKey);
  50. return self::$auth;
  51. }
  52. /**
  53. * TODO 获取上传图片视频token和domain
  54. * @return array
  55. */
  56. public static function getToKenAndDomainI(){
  57. $token = self::autoInfo()->uploadToken(self::$storageName);
  58. $domain = self::$uploadUrl;
  59. $fileName = md5(rand(1000,9999). date('YmdHis') . rand(0, 9999));
  60. return compact('token','domain','fileName');
  61. }
  62. /**
  63. * TODO 图片上传 名称
  64. * @param string $filename
  65. * @return array
  66. * @throws \Exception
  67. */
  68. public static function uploadImage($filename = 'image'){
  69. $request = Request::instance();
  70. $file = $request->file($filename);
  71. $filePath = $file->getRealPath();
  72. $ext = pathinfo($file->getInfo('name'), PATHINFO_EXTENSION);
  73. $key = substr(md5($file->getRealPath()) , 0, 5). date('YmdHis') . rand(0, 9999) . '.' . $ext;
  74. $token = self::autoInfo()->uploadToken(self::$storageName);
  75. try{
  76. $uploadMgr = new UploadManager();
  77. return $uploadMgr->putFile($token, $key, $filePath);
  78. }catch (\Exception $e){
  79. return $e->getMessage();
  80. }
  81. }
  82. /**
  83. * TODO 图片上传 内容
  84. * @param $key
  85. * @param $content
  86. * @return array|string
  87. * @throws \Exception
  88. */
  89. public static function uploadImageStream($key, $content){
  90. $token = self::autoInfo()->uploadToken(self::$storageName, $key);
  91. try{
  92. $uploadMgr = new UploadManager();
  93. return $uploadMgr->put($token, $key, $content);
  94. }catch (\Exception $e){
  95. return $e->getMessage();
  96. }
  97. }
  98. /**
  99. * TODO 图片下载链接
  100. * @param $key uploadImage() 返回的key
  101. * @param string $type 是否设置图片大小
  102. * @param string $imageUrl 图片访问链接
  103. * @param int $time 图片链接最后的访问时间
  104. * @return array
  105. */
  106. public static function imageUrl($key, $type = '', $imageUrl = '', $time = 0){
  107. return ['code'=>200,'name'=>$key,'dir'=>self::$uploadUrl.$key,'thumb_path'=>self::$uploadUrl.$key,'time'=>time()];
  108. // $imageValue = !strlen(trim($type)) ? self::$uploadUrl.$key : self::$uploadUrl.$key.self::getType($type);
  109. // if($time > time() && !strlen(trim($imageUrl))) return ['code'=>100,'dir'=>$imageUrl,'thumb_path'=>$imageUrl,'time'=>$time];
  110. // $imageUrl = self::autoInfo()->privateDownloadUrl($imageValue);
  111. // return ['code'=>200,'name'=>$key,'dir'=>$imageUrl,'thumb_path'=>$imageUrl,'time'=>bcadd(time(),3600,0)];
  112. }
  113. /**
  114. * TODO 获取图片时转换图片大小
  115. * @param $imageType
  116. * @return string
  117. */
  118. public static function getType($imageType){
  119. $type = '';
  120. switch ($imageType){
  121. case "8x6":
  122. $type='?imageView2/1/w/800/h/600';
  123. break;
  124. }
  125. return $type;
  126. }
  127. /**
  128. * TODO 删除资源
  129. * @param $key
  130. * @param $bucket
  131. * @return mixed
  132. */
  133. public static function delete($key){
  134. $bucketManager = new BucketManager(self::autoInfo(),new Config());
  135. return $bucketManager->delete(self::$storageName, $key);
  136. }
  137. }