Upload.php 6.2 KB

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