diff --git a/app-ht/modules/upgrade/controllers/UpgradeController.php b/app-ht/modules/upgrade/controllers/UpgradeController.php index 2fdcf6a..b783d08 100644 --- a/app-ht/modules/upgrade/controllers/UpgradeController.php +++ b/app-ht/modules/upgrade/controllers/UpgradeController.php @@ -4,7 +4,6 @@ namespace app\ht\modules\upgrade\controllers; use Yii; use yii\data\Pagination; -use app\ht\controllers\BaseController; use common\components\adminlog\AdminLogs; use domain\manufacturer\ManufacturerRepository; use domain\upgrade\Upgrade; @@ -12,6 +11,8 @@ use domain\upgrade\UpgradeRepository; use domain\upgrade\UpgradeStatus; use domain\model\ModelRepository; use domain\project\ProjectRepository; +use app\ht\controllers\BaseController; +use Resource\FileService; use stdClass; @@ -158,25 +159,11 @@ class UpgradeController extends BaseController $request = Yii::$app->request; $id = $request->post("id"); // $e = new stdClass(); - $e->success = false; - $e->message = 'fail'; if (empty($id)) { - $e->message = 'ID为空'; - return $this->renderJson($e); - } - $upgradeModel = UpgradeRepository::findOne(['id' => $id]); - if (empty($upgradeModel)) { - $e->message = '未找到版本记录'; + return $this->renderJson($e); } - $upgradeModel->status = UpgradeStatus::STATUS_WAIT; - if ($upgradeModel->save()) { - $e->success = true; - } else { - $e->message = '取消失败'; - } - return $this->renderJson($e); } /** @@ -425,7 +412,7 @@ class UpgradeController extends BaseController // header("HTTP/1.0 500 Internal Server Error"); // exit; // 5 minutes execution time - set_time_limit(5 * 60); + set_time_limit(60 * 60); // Uncomment this one to fake upload time usleep(5000); // Settings @@ -536,6 +523,9 @@ class UpgradeController extends BaseController flock($out, LOCK_UN); } fclose($out); + + $savePath = self::getFilePath(Yii::$app->user->id, $hashName); + $response = [ 'success' => true, 'oldName' => $oldName, @@ -544,6 +534,13 @@ class UpgradeController extends BaseController 'fileSuffixes' => $pathInfo['extension'], //文件后缀名 'file_md5' => md5_file($uploadPath), ]; + + // 上传OSS + $fileService = new FileService(); + $fileService->add($uploadPath, $savePath); + // 删除服务器传成功的文件 + @unlink($uploadPath); + /** * 后台操作日志 */ @@ -569,14 +566,6 @@ class UpgradeController extends BaseController if (isset($manufacture["name"])) { $info["manufacture_name"] = $manufacture["name"]; } - $project = ProjectRepository::selectOne($info["project_id"]); - if (isset($project["name"])) { - $info["project_name"] = $project["name"]; - } - $model = ModelRepository::selectOne($info["model_id"]); - if (isset($model["name"])) { - $info["model_name"] = $model["name"]; - } } return $this->render('push-app', ["info" => $info]); } @@ -595,14 +584,6 @@ class UpgradeController extends BaseController if (isset($manufacture["name"])) { $info["manufacture_name"] = $manufacture["name"]; } - $project = ProjectRepository::selectOne($info["project_id"]); - if (isset($project["name"])) { - $info["project_name"] = $project["name"]; - } - $model = ModelRepository::selectOne($info["model_id"]); - if (isset($model["name"])) { - $info["model_name"] = $model["name"]; - } } return $this->render('push-ota', ["info" => $info]); } @@ -641,4 +622,17 @@ class UpgradeController extends BaseController return $this->redirect('index?type=' . (!empty($request->post("type")) ? $request->post("type") : UpgradeStatus::TYPE_APP)); } + + /** + * 获取文件存储路径: bind_apply/20170509/[userId]/文件名 + * @param $userId + * @param $suffix 文件后缀 + * @return string + */ + public static function getFilePath($userId, $filename) + { + $basePath = "upgrade_packages/" . date("Ymd") . '/' . $userId; + $savePath = $basePath . '/' . $filename; + return $savePath; + } } \ No newline at end of file diff --git a/app-ht/modules/upgrade/views/upgrade/create.php b/app-ht/modules/upgrade/views/upgrade/create.php index e1f9359..ccbb675 100644 --- a/app-ht/modules/upgrade/views/upgrade/create.php +++ b/app-ht/modules/upgrade/views/upgrade/create.php @@ -182,6 +182,7 @@ $this->params['breadcrumbs'][] = $this->title; server: '', // 文件接收服务端。 pick: "#pick_"+ids, // 选择文件的按钮。可选 chunked: true, //是否要分片处理大文件上传 + timeout: 30 * 60 * 1000, chunkSize: 2*1024*1024, //分片上传,每片2M,默认是5M fileSizeLimit: 6*1024* 1024 * 1024, // 所有文件总大小限制 6G fileSingleSizeLimit: 5 * 1024 * 1024 * 1024, // 单个文件大小限制 5 G @@ -238,11 +239,12 @@ $this->params['breadcrumbs'][] = $this->title; }); // 文件上传失败,显示上传出错 - uploader.on( 'uploadError', function( file ) { + uploader.on( 'uploadError', function( file ,response) { $("input[name='path']").val(""); $("input[name='size']").val(""); $("input[name='file_md5']").val(""); $("input[name='oldName']").val(""); + console.log(response); $( '#'+file.id ).find('p.state').text('上传出错'); }); // 完成上传完 diff --git a/common/config/bootstrap.php b/common/config/bootstrap.php index 112d070..0f3713f 100644 --- a/common/config/bootstrap.php +++ b/common/config/bootstrap.php @@ -30,4 +30,5 @@ Yii::setAlias('@upload', $parent . '/upload'); * 第三方插件路径 */ Yii::setAlias('Imagine', $parent . '/common/exts/Imagine'); -Yii::setAlias('OSS', $parent . '/common/exts/OSS'); \ No newline at end of file +Yii::setAlias('OSS', $parent . '/common/exts/OSS'); +Yii::setAlias('Resource', $parent . '/common/exts/Resource'); \ No newline at end of file diff --git a/common/config/params.php b/common/config/params.php index cb12a3c..d5da0a2 100644 --- a/common/config/params.php +++ b/common/config/params.php @@ -3,4 +3,12 @@ return [ 'adminEmail' => 'admin@example.com', 'supportEmail' => 'support@example.com', 'user.passwordResetTokenExpire' => 3600, + 'ossOptions' => [ + 'accessKeyId' => 'LTAI4FkTRRhExNtjJNXGSFzL', + 'accessKeySecret' => 'qimZCuCxMUClfZ5nFijJGTXMnDFklZ', + 'endpoint' => 'kingboard-prod.oss-cn-shenzhen.aliyuncs.com', + 'bucket' => 'kingboard-prod', + 'url' => 'https://kingboard-prod.oss-cn-shenzhen.aliyuncs.com', + 'styleUrl' => 'https://kingboard-prod.oss-cn-shenzhen.aliyuncs.com' + ], ]; diff --git a/common/exts/Resource/FileService.php b/common/exts/Resource/FileService.php new file mode 100644 index 0000000..8be0f45 --- /dev/null +++ b/common/exts/Resource/FileService.php @@ -0,0 +1,164 @@ +name){ + $this->options = \yii::$app->params['ossOptions']; + $serviceProvider = new ServiceProviderOss($this->options); + $this->setServiceProvider($serviceProvider); + } + } + + /** + * @param $path + * @param $destPath + * @return mixed + */ + public function setServiceProvider(ServiceProviderAbstract $serviceProvider) + { + $this->serviceProvider = $serviceProvider; + } + + /** + * @param $path + * @param $destPath + * @return mixed + */ + public function add($path, $destPath) + { + return $this->serviceProvider->add($path, $destPath); + } + + /** + * @param $path + * @param null $style + * @return mixed + */ + public function getUrl($path, $style = null) + { + return $this->serviceProvider->getUrl($path, $style); + } + + /** + * @param $path + * @return mixed + */ + public function delete($path) + { + return $this->serviceProvider->delete($path); + } + + /** + * @param $oldPath + * @param $newPath + * @return mixed + */ + public function replace($oldPath, $newPath, $tmpPath) + { + return $this->serviceProvider->replace($oldPath, $newPath, $tmpPath); + } + + /** + * @param $path + * @return mixed + */ + public function exists($path) + { + return $this->serviceProvider->exists($path); + } + + /** + * @param $oldPath + * @param $newPath + * @return mixed + */ + public function move($oldPath, $newPath) + { + return $this->serviceProvider->move($oldPath, $newPath); + } + + /** + * @param $paths + * @return mixed + */ + public function batchDelete($paths) + { + return $this->serviceProvider->batchDelete($paths); + } + + /** 测试用 */ + public function getProvider() + { + return $this->serviceProvider; + } + + /** 获取指定目录下面的所有文件和文件夹 + * @param string $dir + * @param int $maxKeys + * @return array|bool|null + */ + public function getDirFiles($dir = '', $maxKeys = 20) + { + $serviceProvider = $this->serviceProvider; + if (!$serviceProvider) { + return null; + } + if (empty($dir)) { + return false; + } + $returnList = ['files' => [], 'directories' => []]; + $ossClient = $serviceProvider->getClient(); + $delimiter = '/'; + $nextMarker = ''; + + $options = array( + 'delimiter' => $delimiter, + 'prefix' => $dir, + 'max-keys' => $maxKeys, + 'marker' => $nextMarker, + ); + $ossConfig = $this->options; + $bucket = $ossConfig['bucket']; + try { + $listObjectInfo = $ossClient->listObjects($bucket, $options); + $objectList = $listObjectInfo->getObjectList(); // object list + $prefixList = $listObjectInfo->getPrefixList(); // directory list + if (!empty($objectList)) { + foreach ($objectList as $objectInfo) { + $returnList['files'][] = [$objectInfo->getKey(), $objectInfo->getLastModified(), 'type' => $objectInfo->getType()]; + } + } + if (!empty($prefixList)) { + foreach ($prefixList as $prefixInfo) { + $returnList['directories'][] = $prefixInfo->getPrefix(); + } + } + return $returnList; + } catch (Exception $e) { + Yii::getLogger()->log($e->getMessage(), Logger::LEVEL_ERROR); + return false; + } + + } +} \ No newline at end of file diff --git a/common/exts/Resource/ImageService.php b/common/exts/Resource/ImageService.php new file mode 100644 index 0000000..00291d7 --- /dev/null +++ b/common/exts/Resource/ImageService.php @@ -0,0 +1,18 @@ +bucket = $options['bucket']; + $this->styleUrl = $options['styleUrl']; + $this->url = $options['url']; + + try { + $this->client= new OssClient($accessKeyId, $accessKeySecret, $endpoint ,true); + } catch (OssException $e) { + throw $e; + } + } + + /** + * @param $path + * @param $destPath + * @return null + */ + public function add($path, $destPath) + { + return $this->client->uploadFile($this->bucket, $destPath, $path); + } + + /** + * @param $path + * @param null $style + * @return string + */ + public function getUrl($path, $style = null) + { + if ($style) { + return $path ? $this->styleUrl . '/' . $path . '!' . $style : $this->styleUrl; + } else { + return $path ? $this->url . '/' . $path : $this->url; + } + } + + /** + * @param $path + */ + public function delete($path) + { + $this->client->deleteObject($this->bucket, $path); + } + + /** + * @param $oldPath + * @param $newPath + * @return bool|null + */ + public function replace($oldPath, $newPath, $tmpPath) + { + // 这两个函数是否成功都返回null, By LCN + $this->client->deleteObject($this->bucket, $oldPath); + $this->client->uploadFile($this->bucket, $newPath, $tmpPath); + } + + /** + * @param $path + * @return bool + */ + public function exists($path) + { + return $this->client->doesObjectExist($this->bucket, $path); + } + + /** + * @param $path + * @return bool + */ + public function move($oldPath, $newPath) + { + $this->client->copyObject($this->bucket, $oldPath, $this->bucket, $newPath); + $this->delete($oldPath); + } + + /** + * @param Array $paths + */ + public function batchDelete($paths) + { + $this->client->deleteObjects($this->bucket, $paths); + } + + public function getClient() + { + return $this->client; + } +} \ No newline at end of file -- libgit2 0.21.0