Upload.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkAdmin
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2014~2019 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://demo.thinkadmin.top
  8. // +----------------------------------------------------------------------
  9. // | 开源协议 ( https://mit-license.org )
  10. // +----------------------------------------------------------------------
  11. // | gitee 代码仓库:https://gitee.com/zoujingli/ThinkAdmin
  12. // | github 代码仓库:https://github.com/zoujingli/ThinkAdmin
  13. // +----------------------------------------------------------------------
  14. namespace app\api\controller;
  15. use app\common\controller\Api;
  16. use library\File;
  17. /**
  18. * @title OSS对象存储
  19. * @controller Upload
  20. * @group common
  21. */
  22. class Upload extends Base
  23. {
  24. public function upload()
  25. {
  26. if (!($file = $this->getUploadFile()) || empty($file)) {
  27. $this->error('文件上传异常,文件可能过大或未上传!');
  28. }
  29. if (!$file->checkExt(strtolower(sysconf('storage_local_exts')))) {
  30. $this->error('文件上传类型受限,请在后台配置!');
  31. }
  32. if ($file->checkExt('php,sh')) {
  33. $this->error('可执行文件禁止上传到本地服务器!');
  34. }
  35. $this->safe = boolval(input('safe'));
  36. $this->uptype = $this->getUploadType();
  37. $this->extend = pathinfo($file->getInfo('name'), PATHINFO_EXTENSION);
  38. $name = File::name($file->getPathname(), $this->extend, '', 'md5_file');
  39. $info = File::instance($this->uptype)->save($name, file_get_contents($file->getRealPath()), $this->safe);
  40. if (is_array($info) && isset($info['url'])) {
  41. $url = $this->safe ? $name : $info['url'];
  42. $this->success('成功',$url);
  43. } else {
  44. $this->error('文件处理失败,请稍候再试!');
  45. }
  46. }
  47. /**
  48. * 获取本地文件对象
  49. * @return \think\File
  50. */
  51. private function getUploadFile()
  52. {
  53. try {
  54. return $this->request->file('file');
  55. } catch (\Exception $e) {
  56. $this->error(lang($e->getMessage()));
  57. }
  58. }
  59. /**
  60. * 获取文件上传方式
  61. * @return string
  62. * @throws \think\Exception
  63. * @throws \think\exception\PDOException
  64. */
  65. private function getUploadType()
  66. {
  67. $this->uptype = input('uptype');
  68. if (!in_array($this->uptype, ['local', 'oss', 'qiniu'])) {
  69. $this->uptype = sysconf('storage_type');
  70. }
  71. return $this->uptype;
  72. }
  73. /**
  74. * @title 获取配置
  75. * @desc 获取配置
  76. * @author QGF
  77. * @url /api/Upload/getSignedUrl
  78. * @method GET
  79. * @tag 配置信息
  80. */
  81. public function getSignedUrl(){
  82. $type=$this->request->get('type');
  83. $data=$this->getPolicy($type.'/');
  84. $this->success('操作成功',$data);
  85. }
  86. protected $id= 'LTAI5tDNASTjwM9mDSGckY3N';
  87. protected $key= 'nAKY40GjGz4hf5A9RpoffDmaYWNL3y';
  88. protected $host = 'https://zjth2021.oss-cn-beijing.aliyuncs.com';
  89. public function getPolicy($path,$maxSize=5000000){
  90. $now = time();
  91. $expire = 600; //设置该policy超时时间是10s. 即这个policy过了这个有效时间,将不能访问
  92. $end = $now + $expire;
  93. $expiration = $this->_gmt_iso8601($end);
  94. $policy = [
  95. 'expiration'=>$expiration,
  96. 'conditions'=>[
  97. ['content-length-range', 1, $maxSize],
  98. ['starts-with', '$key', $path],
  99. ]
  100. ];
  101. $policy = json_encode($policy);
  102. $policy = base64_encode($policy);
  103. $signature = base64_encode(hash_hmac('sha1', $policy, $this->key, true));
  104. $response = array();
  105. $response['OSSAccessKeyId'] = $this->id;
  106. $response['policy'] = $policy;
  107. $response['Signature'] = $signature;
  108. $response['host'] = $this->host;
  109. $response['expire'] = $end;
  110. $response['success_action_status'] = 200;
  111. //这个参数是设置用户上传指定的前缀
  112. $response['key'] = $path;
  113. return $response;
  114. }
  115. protected function _gmt_iso8601($time) {
  116. $dtStr = date("c", $time);
  117. $mydatetime = new \DateTime($dtStr);
  118. $expiration = $mydatetime->format(\DateTime::ISO8601);
  119. $pos = strpos($expiration, '+');
  120. $expiration = substr($expiration, 0, $pos);
  121. return $expiration."Z";
  122. }
  123. }