Upload.php 4.5 KB

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