Upload.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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 app\common\controller\Api;
  16. use library\File;
  17. /**
  18. * @title OSS对象存储
  19. * @controller Upload
  20. * @group common
  21. */
  22. class Upload extends Base
  23. {
  24. protected $id ;
  25. protected $key;
  26. protected $host;
  27. public function initialize(){
  28. $this->id = config('app.ali_set.Key');
  29. $this->key = config('app.ali_set.secret');
  30. $this->host = config('app.ali_set.host');
  31. }
  32. /**
  33. * @title 文件上传
  34. * @desc 文件上传
  35. * @author qc
  36. * @url /api/upload/upload
  37. * @method GET||POST
  38. * @tag 文件上传
  39. * @header name:Authorization require:1 desc:Token
  40. * @param name:file require:1 desc:文件名
  41. *
  42. */
  43. public function upload()
  44. {
  45. if (!($file = $this->getUploadFile()) || empty($file)) {
  46. $this->error('文件上传异常,文件可能过大或未上传!');
  47. }
  48. if (!$file->checkExt(strtolower(sysconf('storage_local_exts')))) {
  49. $this->error('文件上传类型受限,请在后台配置!');
  50. }
  51. if ($file->checkExt('php,sh')) {
  52. $this->error('可执行文件禁止上传到本地服务器!');
  53. }
  54. $this->safe = boolval(input('safe'));
  55. $this->uptype = $this->getUploadType();
  56. $this->extend = pathinfo($file->getInfo('name'), PATHINFO_EXTENSION);
  57. $name = File::name($file->getPathname(), $this->extend, '', 'md5_file');
  58. $info = File::instance($this->uptype)->save($name, file_get_contents($file->getRealPath()), $this->safe);
  59. if (is_array($info) && isset($info['url'])) {
  60. $url = $this->safe ? $name : $info['url'];
  61. $this->success('上传成功',$url);
  62. } else {
  63. $this->error('文件处理失败,请稍候再试!');
  64. }
  65. }
  66. /**
  67. * @title 文件上传【pdf加密】
  68. * @desc pdf加密
  69. * @author qc
  70. * @url /api/upload/pdfUpload
  71. * @method GET||POST
  72. * @param name:file require:1 desc:文件名
  73. *
  74. */
  75. public function pdfUpload()
  76. {
  77. if (!($file = $this->getUploadFile()) || empty($file)) $this->error('文件上传异常,文件可能过大或未上传!');
  78. if (!$file->checkExt(strtolower(sysconf('storage_local_exts')))) $this->error('文件上传类型受限,请在后台配置!');
  79. if ($file->checkExt('php,sh')) $this->error('可执行文件禁止上传到本地服务器!');
  80. $file_info = $file->getInfo();
  81. $ext = pathinfo($file_info['name'],PATHINFO_EXTENSION);
  82. if(strtolower($ext) !== 'pdf') $this->error('请上传pdf格式文件');
  83. $file_name = pathinfo($file_info['name'],PATHINFO_FILENAME);
  84. $new_name = $file_name.date("Y-m-d H:i:s").'.'.$ext;
  85. $save_path = $_SERVER['DOCUMENT_ROOT'].'/static/pdf/'.$new_name;
  86. move_uploaded_file($file_info['tmp_name'],$save_path);
  87. $res = pdfCurl($save_path);
  88. unlink($save_path);
  89. if(!empty($res) && $res['code'] == 1) $this->success('上传成功',$res['data']);
  90. $this->error('上传失败');
  91. }
  92. /**
  93. * @title 文件上传【pdf加密】
  94. * @desc pdf加密
  95. * @author qc
  96. * @url /api/upload/pdfUploadNew
  97. * @method GET||POST
  98. * @param name:file require:1 desc:文件名
  99. *
  100. */
  101. public function pdfUploadNew()
  102. {
  103. if (!($file = $this->getUploadFile()) || empty($file)) $this->error('文件上传异常,文件可能过大或未上传!');
  104. if (!$file->checkExt(strtolower(sysconf('storage_local_exts')))) $this->error('文件上传类型受限,请在后台配置!');
  105. if ($file->checkExt('php,sh')) $this->error('可执行文件禁止上传到本地服务器!');
  106. $file_info = $file->getInfo();
  107. $ext = pathinfo($file_info['name'],PATHINFO_EXTENSION);
  108. if(strtolower($ext) !== 'pdf') $this->error('请上传pdf格式文件');
  109. $this->safe = boolval(input('safe'));
  110. $this->uptype = $this->getUploadType();
  111. $this->extend = pathinfo($file->getInfo('name'), PATHINFO_EXTENSION);
  112. if(strtolower($this->extend) !== 'pdf') $this->error('请上传pdf格式文件');
  113. $name = File::name($file->getPathname(), $this->extend, '', 'md5_file');
  114. $info = File::instance($this->uptype)->save($name, file_get_contents($file->getRealPath()), $this->safe);
  115. if (is_array($info) && isset($info['url'])) {
  116. $url = $this->safe ? $name : $info['url'];
  117. $pdf_url = pdfCurl($url);
  118. $this->success('上传成功',['url'=>$url,'pdf_url'=>$pdf_url['data']]);
  119. } else {
  120. $this->error('文件处理失败,请稍候再试!');
  121. }
  122. }
  123. /**
  124. * 获取本地文件对象
  125. * @return \think\File
  126. */
  127. private function getUploadFile()
  128. {
  129. try {
  130. return $this->request->file('file');
  131. } catch (\Exception $e) {
  132. $this->error(lang($e->getMessage()));
  133. }
  134. }
  135. /**
  136. * 获取文件上传方式
  137. * @return string
  138. * @throws \think\Exception
  139. * @throws \think\exception\PDOException
  140. */
  141. private function getUploadType()
  142. {
  143. $this->uptype = input('uptype');
  144. if (!in_array($this->uptype, ['local', 'oss', 'qiniu'])) {
  145. $this->uptype = sysconf('storage_type');
  146. }
  147. return $this->uptype;
  148. }
  149. /**
  150. * @title 获取配置
  151. * @desc 获取配置
  152. * @author QGF
  153. * @url /api/Upload/getSignedUrl
  154. * @method GET
  155. * @tag 配置信息
  156. * @param name:type type:string require:1 default:-- desc:
  157. */
  158. public function getSignedUrl(){
  159. $type=$this->request->get('type');
  160. $data=$this->getPolicy($type.'/');
  161. $this->success('操作成功',$data);
  162. }
  163. public function getPolicy($path,$maxSize=5000000){
  164. $now = time();
  165. $expire = 600; //设置该policy超时时间是10s. 即这个policy过了这个有效时间,将不能访问
  166. $end = $now + $expire;
  167. $expiration = $this->_gmt_iso8601($end);
  168. $policy = [
  169. 'expiration'=>$expiration,
  170. 'conditions'=>[
  171. ['content-length-range', 1, $maxSize],
  172. ['starts-with', '$key', $path],
  173. ]
  174. ];
  175. $policy = json_encode($policy);
  176. $policy = base64_encode($policy);
  177. $signature = base64_encode(hash_hmac('sha1', $policy, $this->key, true));
  178. $response = array();
  179. $response['OSSAccessKeyId'] = $this->id;
  180. $response['policy'] = $policy;
  181. $response['Signature'] = $signature;
  182. $response['host'] = $this->host;
  183. $response['expire'] = $end;
  184. $response['success_action_status'] = 200;
  185. //这个参数是设置用户上传指定的前缀
  186. $response['key'] = $path;
  187. return $response;
  188. }
  189. protected function _gmt_iso8601($time) {
  190. $dtStr = date("c", $time);
  191. $mydatetime = new \DateTime($dtStr);
  192. $expiration = $mydatetime->format(\DateTime::ISO8601);
  193. $pos = strpos($expiration, '+');
  194. $expiration = substr($expiration, 0, $pos);
  195. return $expiration."Z";
  196. }
  197. public function getSysDirection()
  198. {
  199. $data = [
  200. 'film_notice'=>htmlspecialchars_decode(sysconf('film_notice')),
  201. 'film_direction'=>htmlspecialchars_decode(sysconf('film_direction'))
  202. ];
  203. $this->success('获取成功',$data);
  204. }
  205. }