From 6e726df521b17e58e015535ac4d2197be670f0eb Mon Sep 17 00:00:00 2001 From: printempw Date: Fri, 26 Aug 2016 22:42:49 +0800 Subject: [PATCH] add auto-update --- app/Controllers/AdminController.php | 10 ++++- app/Services/Updater.php | 6 +-- app/Services/Utils.php | 1 + resources/views/admin/download.tpl | 67 +++++++++++++++++++++++++++++ resources/views/admin/update.tpl | 2 +- 5 files changed, 80 insertions(+), 6 deletions(-) create mode 100644 resources/views/admin/download.tpl diff --git a/app/Controllers/AdminController.php b/app/Controllers/AdminController.php index 49bdd17e..a94aaaa8 100644 --- a/app/Controllers/AdminController.php +++ b/app/Controllers/AdminController.php @@ -37,7 +37,9 @@ class AdminController extends BaseController public function update() { - if (Utils::getValue('action', $_GET) == "check") { + $action = Utils::getValue('action', $_GET); + + if ($action == "check") { $updater = new \Updater(\App::version()); if ($updater->newVersionAvailable()) { View::json([ @@ -45,8 +47,12 @@ class AdminController extends BaseController 'latest_version' => $updater->latest_version ]); } + } elseif ($action == "download") { + View::show('admin.download'); + } else { + View::show('admin.update'); } - View::show('admin.update'); + } public function users() diff --git a/app/Services/Updater.php b/app/Services/Updater.php index b290e06c..4ec44755 100644 --- a/app/Services/Updater.php +++ b/app/Services/Updater.php @@ -17,8 +17,8 @@ class Updater public function __construct($current_version) { $this->current_version = $current_version; - $this->update_sources = require BASE_DIR."/config/update.php"; - $this->current_source = $this->update_sources[Option::get('update_source')]; + $this->update_sources = require BASE_DIR."/config/update.php"; + $this->current_source = $this->update_sources[\Option::get('update_source')]; } public function getUpdateInfo() @@ -38,7 +38,7 @@ class Updater { $info = $this->getUpdateInfo(); $this->latest_version = $info['latest_version']; - $this->update_time = date('Y-m-d H:i:s', $info['update_time']); + $this->update_time = date('Y-m-d H:i:s', $info['update_time']); } public function downloadUpdate($silent = true) diff --git a/app/Services/Utils.php b/app/Services/Utils.php index ba2b0865..80a47985 100644 --- a/app/Services/Utils.php +++ b/app/Services/Utils.php @@ -3,6 +3,7 @@ namespace App\Services; use App\Exceptions\E; +use Blessing\Storage; class Utils { diff --git a/resources/views/admin/download.tpl b/resources/views/admin/download.tpl new file mode 100644 index 00000000..4dc842df --- /dev/null +++ b/resources/views/admin/download.tpl @@ -0,0 +1,67 @@ +@extends('admin.master') + +@section('title', '下载更新') + +@section('content') + + +
+ +
+

+ 下载更新 + Download Updates +

+
+ + +
+
+
+

下载更新

+
+
newVersionAvailable()) { + $zip_path = $updater->downloadUpdate(false); + + if ($zip_path === false) { + exit('

无法下载更新包。

'); + } + + $zip = new ZipArchive(); + $extract_dir = BASE_DIR."/setup/update_cache/{$updater->latest_version}"; + $res = $zip->open($zip_path); + + if ($res === true) { + echo "

正在解压更新包

"; + $zip->extractTo($extract_dir); + } else { + exit('

更新包解压缩失败。错误代码:'.$res.'

'); + } + $zip->close(); + + if (Storage::copyDir($extract_dir, BASE_DIR) !== true) { + Storage::removeDir(BASE_DIR.'/setup/update_cache/'); + exit('无法覆盖文件。'); + } else { + echo "

正在覆盖文件

"; + Storage::removeDir(BASE_DIR.'/setup/update_cache/'); + echo "

正在清理

"; + } + echo "

更新完成。

"; + } else { + echo "

无可用更新。

"; + } ?> +
+ + +
+ +
+
+ +@endsection diff --git a/resources/views/admin/update.tpl b/resources/views/admin/update.tpl index dc26e254..8cc60f3c 100644 --- a/resources/views/admin/update.tpl +++ b/resources/views/admin/update.tpl @@ -82,7 +82,7 @@ @endif