Upload.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. public function upload()
  32. {
  33. if (!($file = $this->getUploadFile()) || empty($file)) {
  34. $this->error('文件上传异常,文件可能过大或未上传!');
  35. }
  36. if (!$file->checkExt(strtolower(sysconf('storage_local_exts')))) {
  37. $this->error('文件上传类型受限,请在后台配置!');
  38. }
  39. if ($file->checkExt('php,sh')) {
  40. $this->error('可执行文件禁止上传到本地服务器!');
  41. }
  42. $this->safe = boolval(input('safe'));
  43. $this->uptype = $this->getUploadType();
  44. $this->extend = pathinfo($file->getInfo('name'), PATHINFO_EXTENSION);
  45. $name = File::name($file->getPathname(), $this->extend, '', 'md5_file');
  46. $info = File::instance($this->uptype)->save($name, file_get_contents($file->getRealPath()), $this->safe);
  47. if (is_array($info) && isset($info['url'])) {
  48. $url = $this->safe ? $name : $info['url'];
  49. $this->success('上传成功',$url);
  50. } else {
  51. $this->error('文件处理失败,请稍候再试!');
  52. }
  53. }
  54. private function getUploadFile()
  55. {
  56. try {
  57. return $this->request->file('file');
  58. } catch (\Exception $e) {
  59. $this->error(lang($e->getMessage()));
  60. }
  61. }
  62. private function getUploadType()
  63. {
  64. $this->uptype = input('uptype');
  65. if (!in_array($this->uptype, ['local', 'oss', 'qiniu'])) {
  66. $this->uptype = sysconf('storage_type');
  67. }
  68. return $this->uptype;
  69. }
  70. /**
  71. * @Apidoc\Title("获取配置")
  72. * @Apidoc\Desc("获取配置")
  73. * @Apidoc\Method("GET")
  74. * @Apidoc\Author("HG")
  75. * @Apidoc\Tag("")
  76. * @Apidoc\Query("type", type="string",require=true, desc="文件要保存的文件名称")
  77. */
  78. public function getSignedUrl(){
  79. $type=$this->request->get('type','image');
  80. $data=$this->getPolicy($type.'/');
  81. $this->success('操作成功',$data);
  82. }
  83. public function getPolicy($path,$maxSize=5000000){
  84. $now = time();
  85. $expire = 600; //设置该policy超时时间是10s. 即这个policy过了这个有效时间,将不能访问
  86. $end = $now + $expire;
  87. $expiration = $this->_gmt_iso8601($end);
  88. $policy = [
  89. 'expiration'=>$expiration,
  90. 'conditions'=>[
  91. ['content-length-range', 1, $maxSize],
  92. ['starts-with', '$key', $path],
  93. ]
  94. ];
  95. $policy = json_encode($policy);
  96. $policy = base64_encode($policy);
  97. $signature = base64_encode(hash_hmac('sha1', $policy, $this->key, true));
  98. $response = array();
  99. $response['OSSAccessKeyId'] = $this->id;
  100. $response['policy'] = $policy;
  101. $response['Signature'] = $signature;
  102. $response['host'] = $this->host;
  103. $response['expire'] = $end;
  104. $response['success_action_status'] = 200;
  105. //这个参数是设置用户上传指定的前缀
  106. $response['key'] = $path;
  107. return $response;
  108. }
  109. protected function _gmt_iso8601($time) {
  110. $dtStr = date("c", $time);
  111. $mydatetime = new \DateTime($dtStr);
  112. $expiration = $mydatetime->format(\DateTime::ISO8601);
  113. $pos = strpos($expiration, '+');
  114. $expiration = substr($expiration, 0, $pos);
  115. return $expiration."Z";
  116. }
  117. }