Commit 4df7fbdde8d8e44415db2047012ac5b325da33f0
1 parent
c291ad0b
Exists in
master
1.OTA 版本管理,添加OSS上传
Showing
8 changed files
with
399 additions
and
35 deletions
Show diff stats
app-ht/modules/upgrade/controllers/UpgradeController.php
... | ... | @@ -4,7 +4,6 @@ namespace app\ht\modules\upgrade\controllers; |
4 | 4 | |
5 | 5 | use Yii; |
6 | 6 | use yii\data\Pagination; |
7 | -use app\ht\controllers\BaseController; | |
8 | 7 | use common\components\adminlog\AdminLogs; |
9 | 8 | use domain\manufacturer\ManufacturerRepository; |
10 | 9 | use domain\upgrade\Upgrade; |
... | ... | @@ -12,6 +11,8 @@ use domain\upgrade\UpgradeRepository; |
12 | 11 | use domain\upgrade\UpgradeStatus; |
13 | 12 | use domain\model\ModelRepository; |
14 | 13 | use domain\project\ProjectRepository; |
14 | +use app\ht\controllers\BaseController; | |
15 | +use Resource\FileService; | |
15 | 16 | |
16 | 17 | use stdClass; |
17 | 18 | |
... | ... | @@ -158,25 +159,11 @@ class UpgradeController extends BaseController |
158 | 159 | $request = Yii::$app->request; |
159 | 160 | $id = $request->post("id"); // |
160 | 161 | $e = new stdClass(); |
161 | - $e->success = false; | |
162 | - $e->message = 'fail'; | |
163 | 162 | if (empty($id)) { |
164 | - $e->message = 'ID为空'; | |
165 | - return $this->renderJson($e); | |
166 | - } | |
167 | - $upgradeModel = UpgradeRepository::findOne(['id' => $id]); | |
168 | - if (empty($upgradeModel)) { | |
169 | - $e->message = '未找到版本记录'; | |
163 | + | |
170 | 164 | return $this->renderJson($e); |
171 | 165 | } |
172 | - $upgradeModel->status = UpgradeStatus::STATUS_WAIT; | |
173 | - if ($upgradeModel->save()) { | |
174 | - $e->success = true; | |
175 | - } else { | |
176 | - $e->message = '取消失败'; | |
177 | - } | |
178 | 166 | |
179 | - return $this->renderJson($e); | |
180 | 167 | } |
181 | 168 | |
182 | 169 | /** |
... | ... | @@ -425,7 +412,7 @@ class UpgradeController extends BaseController |
425 | 412 | // header("HTTP/1.0 500 Internal Server Error"); |
426 | 413 | // exit; |
427 | 414 | // 5 minutes execution time |
428 | - set_time_limit(5 * 60); | |
415 | + set_time_limit(60 * 60); | |
429 | 416 | // Uncomment this one to fake upload time |
430 | 417 | usleep(5000); |
431 | 418 | // Settings |
... | ... | @@ -536,6 +523,9 @@ class UpgradeController extends BaseController |
536 | 523 | flock($out, LOCK_UN); |
537 | 524 | } |
538 | 525 | fclose($out); |
526 | + | |
527 | + $savePath = self::getFilePath(Yii::$app->user->id, $hashName); | |
528 | + | |
539 | 529 | $response = [ |
540 | 530 | 'success' => true, |
541 | 531 | 'oldName' => $oldName, |
... | ... | @@ -544,6 +534,13 @@ class UpgradeController extends BaseController |
544 | 534 | 'fileSuffixes' => $pathInfo['extension'], //文件后缀名 |
545 | 535 | 'file_md5' => md5_file($uploadPath), |
546 | 536 | ]; |
537 | + | |
538 | + // 上传OSS | |
539 | + $fileService = new FileService(); | |
540 | + $fileService->add($uploadPath, $savePath); | |
541 | + // 删除服务器传成功的文件 | |
542 | + @unlink($uploadPath); | |
543 | + | |
547 | 544 | /** |
548 | 545 | * 后台操作日志 |
549 | 546 | */ |
... | ... | @@ -569,14 +566,6 @@ class UpgradeController extends BaseController |
569 | 566 | if (isset($manufacture["name"])) { |
570 | 567 | $info["manufacture_name"] = $manufacture["name"]; |
571 | 568 | } |
572 | - $project = ProjectRepository::selectOne($info["project_id"]); | |
573 | - if (isset($project["name"])) { | |
574 | - $info["project_name"] = $project["name"]; | |
575 | - } | |
576 | - $model = ModelRepository::selectOne($info["model_id"]); | |
577 | - if (isset($model["name"])) { | |
578 | - $info["model_name"] = $model["name"]; | |
579 | - } | |
580 | 569 | } |
581 | 570 | return $this->render('push-app', ["info" => $info]); |
582 | 571 | } |
... | ... | @@ -595,14 +584,6 @@ class UpgradeController extends BaseController |
595 | 584 | if (isset($manufacture["name"])) { |
596 | 585 | $info["manufacture_name"] = $manufacture["name"]; |
597 | 586 | } |
598 | - $project = ProjectRepository::selectOne($info["project_id"]); | |
599 | - if (isset($project["name"])) { | |
600 | - $info["project_name"] = $project["name"]; | |
601 | - } | |
602 | - $model = ModelRepository::selectOne($info["model_id"]); | |
603 | - if (isset($model["name"])) { | |
604 | - $info["model_name"] = $model["name"]; | |
605 | - } | |
606 | 587 | } |
607 | 588 | return $this->render('push-ota', ["info" => $info]); |
608 | 589 | } |
... | ... | @@ -641,4 +622,17 @@ class UpgradeController extends BaseController |
641 | 622 | |
642 | 623 | return $this->redirect('index?type=' . (!empty($request->post("type")) ? $request->post("type") : UpgradeStatus::TYPE_APP)); |
643 | 624 | } |
625 | + | |
626 | + /** | |
627 | + * 获取文件存储路径: bind_apply/20170509/[userId]/文件名 | |
628 | + * @param $userId | |
629 | + * @param $suffix 文件后缀 | |
630 | + * @return string | |
631 | + */ | |
632 | + public static function getFilePath($userId, $filename) | |
633 | + { | |
634 | + $basePath = "upgrade_packages/" . date("Ymd") . '/' . $userId; | |
635 | + $savePath = $basePath . '/' . $filename; | |
636 | + return $savePath; | |
637 | + } | |
644 | 638 | } |
645 | 639 | \ No newline at end of file | ... | ... |
app-ht/modules/upgrade/views/upgrade/create.php
... | ... | @@ -182,6 +182,7 @@ $this->params['breadcrumbs'][] = $this->title; |
182 | 182 | server: '<?=Url::toRoute("/upgrade/upgrade/upload-file")?>', // 文件接收服务端。 |
183 | 183 | pick: "#pick_"+ids, // 选择文件的按钮。可选 |
184 | 184 | chunked: true, //是否要分片处理大文件上传 |
185 | + timeout: 30 * 60 * 1000, | |
185 | 186 | chunkSize: 2*1024*1024, //分片上传,每片2M,默认是5M |
186 | 187 | fileSizeLimit: 6*1024* 1024 * 1024, // 所有文件总大小限制 6G |
187 | 188 | fileSingleSizeLimit: 5 * 1024 * 1024 * 1024, // 单个文件大小限制 5 G |
... | ... | @@ -238,11 +239,12 @@ $this->params['breadcrumbs'][] = $this->title; |
238 | 239 | }); |
239 | 240 | |
240 | 241 | // 文件上传失败,显示上传出错 |
241 | - uploader.on( 'uploadError', function( file ) { | |
242 | + uploader.on( 'uploadError', function( file ,response) { | |
242 | 243 | $("input[name='path']").val(""); |
243 | 244 | $("input[name='size']").val(""); |
244 | 245 | $("input[name='file_md5']").val(""); |
245 | 246 | $("input[name='oldName']").val(""); |
247 | + console.log(response); | |
246 | 248 | $( '#'+file.id ).find('p.state').text('上传出错'); |
247 | 249 | }); |
248 | 250 | // 完成上传完 | ... | ... |
common/config/bootstrap.php
... | ... | @@ -30,4 +30,5 @@ Yii::setAlias('@upload', $parent . '/upload'); |
30 | 30 | * 第三方插件路径 |
31 | 31 | */ |
32 | 32 | Yii::setAlias('Imagine', $parent . '/common/exts/Imagine'); |
33 | -Yii::setAlias('OSS', $parent . '/common/exts/OSS'); | |
34 | 33 | \ No newline at end of file |
34 | +Yii::setAlias('OSS', $parent . '/common/exts/OSS'); | |
35 | +Yii::setAlias('Resource', $parent . '/common/exts/Resource'); | |
35 | 36 | \ No newline at end of file | ... | ... |
common/config/params.php
... | ... | @@ -3,4 +3,12 @@ return [ |
3 | 3 | 'adminEmail' => 'admin@example.com', |
4 | 4 | 'supportEmail' => 'support@example.com', |
5 | 5 | 'user.passwordResetTokenExpire' => 3600, |
6 | + 'ossOptions' => [ | |
7 | + 'accessKeyId' => 'LTAI4FkTRRhExNtjJNXGSFzL', | |
8 | + 'accessKeySecret' => 'qimZCuCxMUClfZ5nFijJGTXMnDFklZ', | |
9 | + 'endpoint' => 'kingboard-prod.oss-cn-shenzhen.aliyuncs.com', | |
10 | + 'bucket' => 'kingboard-prod', | |
11 | + 'url' => 'https://kingboard-prod.oss-cn-shenzhen.aliyuncs.com', | |
12 | + 'styleUrl' => 'https://kingboard-prod.oss-cn-shenzhen.aliyuncs.com' | |
13 | + ], | |
6 | 14 | ]; | ... | ... |
... | ... | @@ -0,0 +1,164 @@ |
1 | +<?php | |
2 | +namespace Resource; | |
3 | +use Yii; | |
4 | +use yii\base\Exception; | |
5 | +use yii\log\Logger; | |
6 | + | |
7 | + | |
8 | +/** | |
9 | + * Class FileService 和ImageServer 是一样的,只是命名不一样 | |
10 | + * @package resource | |
11 | + */ | |
12 | +class FileService | |
13 | +{ | |
14 | + | |
15 | + /** @var **/ | |
16 | + protected $serviceProvider; | |
17 | + | |
18 | + /** @var string **/ | |
19 | + public $name = 'OSS'; | |
20 | + | |
21 | + /** | |
22 | + * @param \yii\web\Application $app | |
23 | + */ | |
24 | + public function __construct() | |
25 | + { | |
26 | + if('OSS' == $this->name){ | |
27 | + $this->options = \yii::$app->params['ossOptions']; | |
28 | + $serviceProvider = new ServiceProviderOss($this->options); | |
29 | + $this->setServiceProvider($serviceProvider); | |
30 | + } | |
31 | + } | |
32 | + | |
33 | + /** | |
34 | + * @param $path | |
35 | + * @param $destPath | |
36 | + * @return mixed | |
37 | + */ | |
38 | + public function setServiceProvider(ServiceProviderAbstract $serviceProvider) | |
39 | + { | |
40 | + $this->serviceProvider = $serviceProvider; | |
41 | + } | |
42 | + | |
43 | + /** | |
44 | + * @param $path | |
45 | + * @param $destPath | |
46 | + * @return mixed | |
47 | + */ | |
48 | + public function add($path, $destPath) | |
49 | + { | |
50 | + return $this->serviceProvider->add($path, $destPath); | |
51 | + } | |
52 | + | |
53 | + /** | |
54 | + * @param $path | |
55 | + * @param null $style | |
56 | + * @return mixed | |
57 | + */ | |
58 | + public function getUrl($path, $style = null) | |
59 | + { | |
60 | + return $this->serviceProvider->getUrl($path, $style); | |
61 | + } | |
62 | + | |
63 | + /** | |
64 | + * @param $path | |
65 | + * @return mixed | |
66 | + */ | |
67 | + public function delete($path) | |
68 | + { | |
69 | + return $this->serviceProvider->delete($path); | |
70 | + } | |
71 | + | |
72 | + /** | |
73 | + * @param $oldPath | |
74 | + * @param $newPath | |
75 | + * @return mixed | |
76 | + */ | |
77 | + public function replace($oldPath, $newPath, $tmpPath) | |
78 | + { | |
79 | + return $this->serviceProvider->replace($oldPath, $newPath, $tmpPath); | |
80 | + } | |
81 | + | |
82 | + /** | |
83 | + * @param $path | |
84 | + * @return mixed | |
85 | + */ | |
86 | + public function exists($path) | |
87 | + { | |
88 | + return $this->serviceProvider->exists($path); | |
89 | + } | |
90 | + | |
91 | + /** | |
92 | + * @param $oldPath | |
93 | + * @param $newPath | |
94 | + * @return mixed | |
95 | + */ | |
96 | + public function move($oldPath, $newPath) | |
97 | + { | |
98 | + return $this->serviceProvider->move($oldPath, $newPath); | |
99 | + } | |
100 | + | |
101 | + /** | |
102 | + * @param $paths | |
103 | + * @return mixed | |
104 | + */ | |
105 | + public function batchDelete($paths) | |
106 | + { | |
107 | + return $this->serviceProvider->batchDelete($paths); | |
108 | + } | |
109 | + | |
110 | + /** 测试用 */ | |
111 | + public function getProvider() | |
112 | + { | |
113 | + return $this->serviceProvider; | |
114 | + } | |
115 | + | |
116 | + /** 获取指定目录下面的所有文件和文件夹 | |
117 | + * @param string $dir | |
118 | + * @param int $maxKeys | |
119 | + * @return array|bool|null | |
120 | + */ | |
121 | + public function getDirFiles($dir = '', $maxKeys = 20) | |
122 | + { | |
123 | + $serviceProvider = $this->serviceProvider; | |
124 | + if (!$serviceProvider) { | |
125 | + return null; | |
126 | + } | |
127 | + if (empty($dir)) { | |
128 | + return false; | |
129 | + } | |
130 | + $returnList = ['files' => [], 'directories' => []]; | |
131 | + $ossClient = $serviceProvider->getClient(); | |
132 | + $delimiter = '/'; | |
133 | + $nextMarker = ''; | |
134 | + | |
135 | + $options = array( | |
136 | + 'delimiter' => $delimiter, | |
137 | + 'prefix' => $dir, | |
138 | + 'max-keys' => $maxKeys, | |
139 | + 'marker' => $nextMarker, | |
140 | + ); | |
141 | + $ossConfig = $this->options; | |
142 | + $bucket = $ossConfig['bucket']; | |
143 | + try { | |
144 | + $listObjectInfo = $ossClient->listObjects($bucket, $options); | |
145 | + $objectList = $listObjectInfo->getObjectList(); // object list | |
146 | + $prefixList = $listObjectInfo->getPrefixList(); // directory list | |
147 | + if (!empty($objectList)) { | |
148 | + foreach ($objectList as $objectInfo) { | |
149 | + $returnList['files'][] = [$objectInfo->getKey(), $objectInfo->getLastModified(), 'type' => $objectInfo->getType()]; | |
150 | + } | |
151 | + } | |
152 | + if (!empty($prefixList)) { | |
153 | + foreach ($prefixList as $prefixInfo) { | |
154 | + $returnList['directories'][] = $prefixInfo->getPrefix(); | |
155 | + } | |
156 | + } | |
157 | + return $returnList; | |
158 | + } catch (Exception $e) { | |
159 | + Yii::getLogger()->log($e->getMessage(), Logger::LEVEL_ERROR); | |
160 | + return false; | |
161 | + } | |
162 | + | |
163 | + } | |
164 | +} | |
0 | 165 | \ No newline at end of file | ... | ... |
... | ... | @@ -0,0 +1,18 @@ |
1 | +<?php namespace Resource; | |
2 | + | |
3 | + | |
4 | +/** | |
5 | + * Class ImageService | |
6 | + * @package resource | |
7 | + */ | |
8 | +class ImageService extends FileService | |
9 | +{ | |
10 | + | |
11 | + /** @var **/ | |
12 | + protected $serviceProvider; | |
13 | + | |
14 | + /** @var string **/ | |
15 | + public $name = 'OSS'; | |
16 | + | |
17 | + | |
18 | +} | |
0 | 19 | \ No newline at end of file | ... | ... |
... | ... | @@ -0,0 +1,53 @@ |
1 | +<?php namespace Resource; | |
2 | + | |
3 | +/** | |
4 | + * Class ServiceProvider | |
5 | + * @package resource | |
6 | + */ | |
7 | +abstract class ServiceProviderAbstract | |
8 | +{ | |
9 | + /** | |
10 | + * @param $path | |
11 | + * @param $destPath | |
12 | + * @return mixed | |
13 | + */ | |
14 | + abstract public function add($path, $destPath); | |
15 | + | |
16 | + /** | |
17 | + * @param $path | |
18 | + * @param null $style | |
19 | + * @return mixed | |
20 | + */ | |
21 | + abstract public function getUrl($path, $style = null); | |
22 | + | |
23 | + /** | |
24 | + * @param $path | |
25 | + * @return mixed | |
26 | + */ | |
27 | + abstract public function delete($path); | |
28 | + | |
29 | + /** | |
30 | + * @param $oldPath | |
31 | + * @param $newPath | |
32 | + * @return mixed | |
33 | + */ | |
34 | + abstract function replace($oldPath, $newPath, $tmpPath); | |
35 | + | |
36 | + /** | |
37 | + * @param $path | |
38 | + * @return mixed | |
39 | + */ | |
40 | + abstract public function exists($path); | |
41 | + | |
42 | + /** | |
43 | + * @param $path | |
44 | + * @return bool | |
45 | + */ | |
46 | + abstract public function move($oldPath, $newPath); | |
47 | + | |
48 | + /** | |
49 | + * @param $path | |
50 | + * @return mixed | |
51 | + */ | |
52 | + abstract public function batchDelete($paths); | |
53 | +} | |
0 | 54 | \ No newline at end of file | ... | ... |
... | ... | @@ -0,0 +1,124 @@ |
1 | +<?php namespace Resource; | |
2 | + | |
3 | +use OSS\OssClient; | |
4 | + | |
5 | +/** | |
6 | + * Class ServiceProviderOss | |
7 | + * @package resource | |
8 | + */ | |
9 | +class ServiceProviderOss extends ServiceProviderAbstract | |
10 | +{ | |
11 | + /** | |
12 | + * @var OssClient | |
13 | + */ | |
14 | + private $client; | |
15 | + /** | |
16 | + * @var | |
17 | + */ | |
18 | + private $bucket; | |
19 | + /** | |
20 | + * @var | |
21 | + */ | |
22 | + private $styleUrl; | |
23 | + /** | |
24 | + * @var | |
25 | + */ | |
26 | + private $url; | |
27 | + | |
28 | + /** | |
29 | + * ServiceProviderOss constructor. | |
30 | + * @param $options | |
31 | + */ | |
32 | + public function __construct($options) | |
33 | + { | |
34 | + $accessKeyId = $options['accessKeyId']; | |
35 | + $accessKeySecret = $options['accessKeySecret']; | |
36 | + $endpoint = $options['endpoint']; | |
37 | + | |
38 | + $this->bucket = $options['bucket']; | |
39 | + $this->styleUrl = $options['styleUrl']; | |
40 | + $this->url = $options['url']; | |
41 | + | |
42 | + try { | |
43 | + $this->client= new OssClient($accessKeyId, $accessKeySecret, $endpoint ,true); | |
44 | + } catch (OssException $e) { | |
45 | + throw $e; | |
46 | + } | |
47 | + } | |
48 | + | |
49 | + /** | |
50 | + * @param $path | |
51 | + * @param $destPath | |
52 | + * @return null | |
53 | + */ | |
54 | + public function add($path, $destPath) | |
55 | + { | |
56 | + return $this->client->uploadFile($this->bucket, $destPath, $path); | |
57 | + } | |
58 | + | |
59 | + /** | |
60 | + * @param $path | |
61 | + * @param null $style | |
62 | + * @return string | |
63 | + */ | |
64 | + public function getUrl($path, $style = null) | |
65 | + { | |
66 | + if ($style) { | |
67 | + return $path ? $this->styleUrl . '/' . $path . '!' . $style : $this->styleUrl; | |
68 | + } else { | |
69 | + return $path ? $this->url . '/' . $path : $this->url; | |
70 | + } | |
71 | + } | |
72 | + | |
73 | + /** | |
74 | + * @param $path | |
75 | + */ | |
76 | + public function delete($path) | |
77 | + { | |
78 | + $this->client->deleteObject($this->bucket, $path); | |
79 | + } | |
80 | + | |
81 | + /** | |
82 | + * @param $oldPath | |
83 | + * @param $newPath | |
84 | + * @return bool|null | |
85 | + */ | |
86 | + public function replace($oldPath, $newPath, $tmpPath) | |
87 | + { | |
88 | + // 这两个函数是否成功都返回null, By LCN | |
89 | + $this->client->deleteObject($this->bucket, $oldPath); | |
90 | + $this->client->uploadFile($this->bucket, $newPath, $tmpPath); | |
91 | + } | |
92 | + | |
93 | + /** | |
94 | + * @param $path | |
95 | + * @return bool | |
96 | + */ | |
97 | + public function exists($path) | |
98 | + { | |
99 | + return $this->client->doesObjectExist($this->bucket, $path); | |
100 | + } | |
101 | + | |
102 | + /** | |
103 | + * @param $path | |
104 | + * @return bool | |
105 | + */ | |
106 | + public function move($oldPath, $newPath) | |
107 | + { | |
108 | + $this->client->copyObject($this->bucket, $oldPath, $this->bucket, $newPath); | |
109 | + $this->delete($oldPath); | |
110 | + } | |
111 | + | |
112 | + /** | |
113 | + * @param Array $paths | |
114 | + */ | |
115 | + public function batchDelete($paths) | |
116 | + { | |
117 | + $this->client->deleteObjects($this->bucket, $paths); | |
118 | + } | |
119 | + | |
120 | + public function getClient() | |
121 | + { | |
122 | + return $this->client; | |
123 | + } | |
124 | +} | |
0 | 125 | \ No newline at end of file | ... | ... |