123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130 |
- <?php
- namespace app\admin\controller\api;
- use library\Controller;
- use library\File;
- class Plugs extends Controller
- {
-
- public function icon()
- {
- $this->title = '图标选择器';
- $this->field = input('field', 'icon');
- $this->fetch();
- }
-
- public function check()
- {
- $diff1 = explode(',', strtolower(input('exts', '')));
- $diff2 = explode(',', strtolower(sysconf('storage_local_exts')));
- $exts = array_intersect($diff1, $diff2);
- $this->success('获取文件上传参数', [
- 'exts' => join('|', $exts),
- 'mime' => File::mine($exts),
- 'type' => $this->getUploadType(),
- 'data' => $this->getUploadData(),
- ]);
- }
-
- public function upload()
- {
- if (!($file = $this->getUploadFile()) || empty($file)) {
- return json(['uploaded' => false, 'error' => ['message' => '文件上传异常,文件可能过大或未上传']]);
- }
- if (!$file->checkExt(strtolower(sysconf('storage_local_exts')))) {
- return json(['uploaded' => false, 'error' => ['message' => '文件上传类型受限,请在后台配置']]);
- }
- if ($file->checkExt('php,sh')) {
- return json(['uploaded' => false, 'error' => ['message' => '可执行文件禁止上传到本地服务器']]);
- }
- $this->safe = boolval(input('safe'));
- $this->uptype = $this->getUploadType();
- $this->extend = pathinfo($file->getInfo('name'), PATHINFO_EXTENSION);
- $name = File::name($file->getPathname(), $this->extend, '', 'md5_file');
- $info = File::instance($this->uptype)->save($name, file_get_contents($file->getRealPath()), $this->safe);
- if (is_array($info) && isset($info['url'])) {
- return json(['uploaded' => true, 'filename' => $name, 'url' => $this->safe ? $name : $info['url']]);
- } else {
- return json(['uploaded' => false, 'error' => ['message' => '文件处理失败,请稍候再试!']]);
- }
- }
-
- private function getUploadData()
- {
- if ($this->getUploadType() === 'qiniu') {
- $file = File::instance('qiniu');
- return ['url' => $file->upload(true), 'token' => $file->buildUploadToken(), 'uptype' => $this->getUploadType()];
- } else {
- return ['url' => '?s=admin/api.plugs/upload', 'token' => uniqid('local_upload_'), 'uptype' => $this->getUploadType()];
- }
- }
-
- private function getUploadType()
- {
- $this->uptype = input('uptype');
- if (!in_array($this->uptype, ['local', 'oss', 'qiniu'])) {
- $this->uptype = sysconf('storage_type');
- }
- return $this->uptype;
- }
-
- private function getUploadFile()
- {
- try {
- return $this->request->file('file');
- } catch (\Exception $e) {
- $this->error(lang($e->getMessage()));
- }
- }
- }
|