-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathAction.php
92 lines (81 loc) · 2.38 KB
/
Action.php
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
<?php
/**
* Created by PhpStorm.
* User: yidashi
* Date: 16/5/3
* Time: 下午6:59
*/
namespace yidashi\webuploader;
use Qiniu\Auth;
use Yii;
use yii\web\Response;
use yii\web\UploadedFile;
class Action extends \yii\base\Action
{
public $driver;
/**
* @var array
*/
public $config = [];
public function init()
{
//close csrf
Yii::$app->request->enableCsrfValidation = false;
Yii::$app->response->format = Response::FORMAT_JSON;
$this->driver = Yii::$app->request->get('driver', 'local');
parent::init();
}
public function run()
{
switch ($this->driver) {
case 'local':
return $this->local();
break;
case 'qiniu':
return $this->qiniu();
break;
}
}
private function local()
{
$uploader = UploadedFile::getInstanceByName('file');
$root = Yii::getAlias('@staticroot');
$path = 'upload/image/' . date('Ymd') . '/';
$dir = $root . '/' . $path;
if (!is_dir($dir)) {
mkdir($dir, 0755, true);
}
$name = time() . '.' . $uploader->extension;
$uploader->saveAs($dir . $name);
/**
* 得到上传文件所对应的各个参数,数组结构
* array(
* "state" => "", //上传状态,上传成功时必须返回"success"
* "url" => "", //返回的地址
* "title" => "", //新文件名
* "original" => "", //原始文件名
* "type" => "" //文件类型
* "size" => "", //文件大小
* )
*/
return [
'state' => 'success',
'type' => $uploader->type,
'size' => $uploader->size,
'original' => $uploader->name,
'url' => $path . $name,
'title' => $name
];
}
private function qiniu()
{
$this->config = array_merge($this->config, Yii::$app->params['webuploader_qiniu_config']);
$bucket = $this->config['bucket'];
$accessKey = $this->config['accessKey'];
$secretKey = $this->config['secretKey'];
$auth = new Auth($accessKey, $secretKey);
$upToken = $auth->uploadToken($bucket);
$ret = ['uptoken' => $upToken];
return $ret;
}
}