Upload.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. protected $id ;
  25. protected $key;
  26. protected $host;
  27. public function initialize(){
  28. $this->id = config('app.ali_set.Key');
  29. $this->key = config('app.ali_set.secret');
  30. $this->host = config('app.ali_set.host');
  31. }
  32. /**
  33. * @title 文件上传
  34. * @desc 个人信息
  35. * @author qc
  36. * @url /api/upload/upload
  37. * @method GET||POST
  38. * @tag 个人信息
  39. * @header name:Authorization require:1 desc:Token
  40. * @param name:file require:1 desc:文件名
  41. *
  42. */
  43. public function upload()
  44. {
  45. var_dump( $this->request->file('file'));
  46. die();
  47. if (!($file = $this->getUploadFile()) || empty($file)) {
  48. $this->error('文件上传异常,文件可能过大或未上传!');
  49. }
  50. if (!$file->checkExt(strtolower(sysconf('storage_local_exts')))) {
  51. $this->error('文件上传类型受限,请在后台配置!');
  52. }
  53. if ($file->checkExt('php,sh')) {
  54. $this->error('可执行文件禁止上传到本地服务器!');
  55. }
  56. $this->safe = boolval(input('safe'));
  57. $this->uptype = $this->getUploadType();
  58. $this->extend = pathinfo($file->getInfo('name'), PATHINFO_EXTENSION);
  59. $name = File::name($file->getPathname(), $this->extend, '', 'md5_file');
  60. $info = File::instance($this->uptype)->save($name, file_get_contents($file->getRealPath()), $this->safe);
  61. if (is_array($info) && isset($info['url'])) {
  62. $url = $this->safe ? $name : $info['url'];
  63. $this->success('上传成功',$url);
  64. } else {
  65. $this->error('文件处理失败,请稍候再试!');
  66. }
  67. }
  68. /**
  69. * 获取本地文件对象
  70. * @return \think\File
  71. */
  72. private function getUploadFile()
  73. {
  74. try {
  75. return $this->request->file('file');
  76. } catch (\Exception $e) {
  77. $this->error(lang($e->getMessage()));
  78. }
  79. }
  80. /**
  81. * 获取文件上传方式
  82. * @return string
  83. * @throws \think\Exception
  84. * @throws \think\exception\PDOException
  85. */
  86. private function getUploadType()
  87. {
  88. $this->uptype = input('uptype');
  89. if (!in_array($this->uptype, ['local', 'oss', 'qiniu'])) {
  90. $this->uptype = sysconf('storage_type');
  91. }
  92. return $this->uptype;
  93. }
  94. /**
  95. * @title 获取配置
  96. * @desc 获取配置
  97. * @author QGF
  98. * @url /api/Upload/getSignedUrl
  99. * @method GET
  100. * @tag 配置信息
  101. * @param name:type type:string require:1 default:-- desc:
  102. */
  103. public function getSignedUrl(){
  104. $type=$this->request->get('type');
  105. $data=$this->getPolicy($type.'/');
  106. $this->success('操作成功',$data);
  107. }
  108. public function getPolicy($path,$maxSize=5000000){
  109. $now = time();
  110. $expire = 600; //设置该policy超时时间是10s. 即这个policy过了这个有效时间,将不能访问
  111. $end = $now + $expire;
  112. $expiration = $this->_gmt_iso8601($end);
  113. $policy = [
  114. 'expiration'=>$expiration,
  115. 'conditions'=>[
  116. ['content-length-range', 1, $maxSize],
  117. ['starts-with', '$key', $path],
  118. ]
  119. ];
  120. $policy = json_encode($policy);
  121. $policy = base64_encode($policy);
  122. $signature = base64_encode(hash_hmac('sha1', $policy, $this->key, true));
  123. $response = array();
  124. $response['OSSAccessKeyId'] = $this->id;
  125. $response['policy'] = $policy;
  126. $response['Signature'] = $signature;
  127. $response['host'] = $this->host;
  128. $response['expire'] = $end;
  129. $response['success_action_status'] = 200;
  130. //这个参数是设置用户上传指定的前缀
  131. $response['key'] = $path;
  132. return $response;
  133. }
  134. protected function _gmt_iso8601($time) {
  135. $dtStr = date("c", $time);
  136. $mydatetime = new \DateTime($dtStr);
  137. $expiration = $mydatetime->format(\DateTime::ISO8601);
  138. $pos = strpos($expiration, '+');
  139. $expiration = substr($expiration, 0, $pos);
  140. return $expiration."Z";
  141. }
  142. public function getSysDirection()
  143. {
  144. $data = [
  145. 'film_notice'=>htmlspecialchars_decode(sysconf('film_notice')),
  146. 'film_direction'=>htmlspecialchars_decode(sysconf('film_direction'))
  147. ];
  148. $this->success('获取成功',$data);
  149. }
  150. }