Upload.php 4.9 KB

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