AssetController.php
4.0 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2018 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: kane <chengjin005@163.com>
// +----------------------------------------------------------------------
namespace app\user\controller;
use cmf\controller\AdminBaseController;
use cmf\lib\Upload;
use think\View;
/**
* 附件上传控制器
* Class Asset
* @package app\asset\controller
*/
class AssetController extends AdminBaseController
{
public function _initialize()
{
$adminId = cmf_get_current_admin_id();
$userId = cmf_get_current_user_id();
if (empty($adminId) && empty($userId)) {
exit("非法上传!");
}
}
/**
* webuploader 上传
*/
public function webuploader()
{
if ($this->request->isPost()) {
$uploader = new Upload();
$result = $uploader->upload();
if ($result === false) {
$this->error($uploader->getError());
} else {
$this->success("上传成功!", '', $result);
}
} else {
$uploadSetting = cmf_get_upload_setting();
$arrFileTypes = [
'image' => ['title' => 'Image files', 'extensions' => $uploadSetting['file_types']['image']['extensions']],
'video' => ['title' => 'Video files', 'extensions' => $uploadSetting['file_types']['video']['extensions']],
'audio' => ['title' => 'Audio files', 'extensions' => $uploadSetting['file_types']['audio']['extensions']],
'file' => ['title' => 'Custom files', 'extensions' => $uploadSetting['file_types']['file']['extensions']]
];
$arrData = $this->request->param();
if (empty($arrData["filetype"])) {
$arrData["filetype"] = "image";
}
$fileType = $arrData["filetype"];
if (array_key_exists($arrData["filetype"], $arrFileTypes)) {
$extensions = $uploadSetting['file_types'][$arrData["filetype"]]['extensions'];
$fileTypeUploadMaxFileSize = $uploadSetting['file_types'][$fileType]['upload_max_filesize'];
} else {
$this->error('上传文件类型配置错误!');
}
View::share('filetype', $arrData["filetype"]);
View::share('extensions', $extensions);
View::share('upload_max_filesize', $fileTypeUploadMaxFileSize * 1024);
View::share('upload_max_filesize_mb', intval($fileTypeUploadMaxFileSize / 1024));
$maxFiles = intval($uploadSetting['max_files']);
$maxFiles = empty($maxFiles) ? 20 : $maxFiles;
$chunkSize = intval($uploadSetting['chunk_size']);
$chunkSize = empty($chunkSize) ? 512 : $chunkSize;
View::share('max_files', $arrData["multi"] ? $maxFiles : 1);
View::share('chunk_size', $chunkSize); //// 单位KB
View::share('multi', $arrData["multi"]);
View::share('app', $arrData["app"]);
$content = hook_one('fetch_upload_view');
$tabs = ['local', 'url', 'cloud'];
$tab = !empty($arrData['tab']) && in_array($arrData['tab'], $tabs) ? $arrData['tab'] : 'local';
if (!empty($content)) {
$this->assign('has_cloud_storage', true);
}
if (!empty($content) && $tab == 'cloud') {
return $content;
}
$tab = $tab == 'cloud' ? 'local' : $tab;
$this->assign('tab', $tab);
return $this->fetch(":webuploader");
}
}
}