Upload.php 5.2 KB

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