Upload.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 library\File;
  16. use hg\apidoc\annotation as Apidoc;
  17. /**
  18. * @Apidoc\Title("上传文件")
  19. * @Apidoc\Group("api")
  20. */
  21. class Upload extends Base
  22. {
  23. protected $id ;
  24. protected $key;
  25. protected $host;
  26. public function initialize(){
  27. $this->id = sysconf('storage_oss_keyid');
  28. $this->key = sysconf('storage_oss_secret');
  29. $this->host = "https://".sysconf('storage_oss_domain');
  30. }
  31. /**
  32. * @Apidoc\Title("上传文件接口(上传到本地(导入模板需要))")
  33. * @Apidoc\Desc("获取配置")
  34. * @Apidoc\Method("GET")
  35. * @Apidoc\Author("HG")
  36. * @Apidoc\Tag("")
  37. * @Apidoc\Query("", type="string",require=true, desc="表达提交的文件信息")
  38. * @Apidoc\Returned("", type="string", desc="上传后的文件路径")
  39. */
  40. public function upload()
  41. {
  42. if (!($file = $this->getUploadFile()) || empty($file)) {
  43. $this->error('文件上传异常,文件可能过大或未上传!');
  44. }
  45. if (!$file->checkExt(strtolower(sysconf('storage_local_exts')))) {
  46. $this->error('文件上传类型受限,请在后台配置!');
  47. }
  48. if ($file->checkExt('php,sh')) {
  49. $this->error('可执行文件禁止上传到本地服务器!');
  50. }
  51. $this->safe = boolval(input('safe'));
  52. $this->uptype = $this->getUploadType();
  53. $this->extend = pathinfo($file->getInfo('name'), PATHINFO_EXTENSION);
  54. $name = File::name($file->getPathname(), $this->extend, '', 'md5_file');
  55. $info = File::instance($this->uptype)->save($name, file_get_contents($file->getRealPath()), $this->safe);
  56. if (is_array($info) && isset($info['url'])) {
  57. $url = $this->safe ? $name : $info['url'];
  58. $this->success('上传成功',$url);
  59. } else {
  60. $this->error('文件处理失败,请稍候再试!');
  61. }
  62. }
  63. private function getUploadFile()
  64. {
  65. try {
  66. return $this->request->file('file');
  67. } catch (\Exception $e) {
  68. $this->error(lang($e->getMessage()));
  69. }
  70. }
  71. private function getUploadType()
  72. {
  73. $this->uptype = input('uptype');
  74. if (!in_array($this->uptype, ['local', 'oss', 'qiniu'])) {
  75. $this->uptype = sysconf('storage_type');
  76. }
  77. return $this->uptype;
  78. }
  79. /**
  80. * @Apidoc\Title("获取OSS配置(用于上传文件到OSS使用)")
  81. * @Apidoc\Desc("获取配置")
  82. * @Apidoc\Method("GET")
  83. * @Apidoc\Author("HG")
  84. * @Apidoc\Tag("")
  85. * @Apidoc\Query("type", type="string",require=true, desc="文件要保存的目录名称(需前端创建好)")
  86. * @Apidoc\Returned("OSSAccessKeyId", type="string", desc="keyId")
  87. * @Apidoc\Returned("policy", type="string", desc="规则")
  88. * @Apidoc\Returned("Signature", type="string", desc="签名")
  89. * @Apidoc\Returned("host", type="string", desc="oss地址")
  90. * @Apidoc\Returned("expire", type="string", desc="有效截止时间")
  91. * @Apidoc\Returned("key", type="string", desc="路径(前端生成的目录名称+/)")
  92. */
  93. public function getSignedUrl(){
  94. $type=$this->request->get('type','image');
  95. $data=$this->getPolicy($type.'/');
  96. $this->success('操作成功',$data);
  97. }
  98. public function getPolicy($path,$maxSize=5000000){
  99. $now = time();
  100. $expire = 600; //设置该policy超时时间是10s. 即这个policy过了这个有效时间,将不能访问
  101. $end = $now + $expire;
  102. $expiration = $this->_gmt_iso8601($end);
  103. $policy = [
  104. 'expiration'=>$expiration,
  105. 'conditions'=>[
  106. ['content-length-range', 1, $maxSize],
  107. ['starts-with', '$key', $path],
  108. ]
  109. ];
  110. $policy = json_encode($policy);
  111. $policy = base64_encode($policy);
  112. $signature = base64_encode(hash_hmac('sha1', $policy, $this->key, true));
  113. $response = array();
  114. $response['OSSAccessKeyId'] = $this->id;
  115. $response['policy'] = $policy;
  116. $response['Signature'] = $signature;
  117. $response['host'] = $this->host;
  118. $response['expire'] = $end;
  119. $response['success_action_status'] = 200;
  120. //这个参数是设置用户上传指定的前缀
  121. $response['key'] = $path;
  122. return $response;
  123. }
  124. protected function _gmt_iso8601($time) {
  125. $dtStr = date("c", $time);
  126. $mydatetime = new \DateTime($dtStr);
  127. $expiration = $mydatetime->format(\DateTime::ISO8601);
  128. $pos = strpos($expiration, '+');
  129. $expiration = substr($expiration, 0, $pos);
  130. return $expiration."Z";
  131. }
  132. }