add DocBlocks for Updater

This commit is contained in:
printempw 2016-08-27 18:11:34 +08:00
parent 0362b1c1dd
commit 986b20a0ad
4 changed files with 83 additions and 16 deletions

View File

@ -46,13 +46,17 @@ class AdminController extends BaseController
'new_version_available' => true,
'latest_version' => $updater->latest_version
]);
} else {
View::json([
'new_version_available' => false,
'latest_version' => $updater->current_version
]);
}
} elseif ($action == "download") {
View::show('admin.download');
} else {
View::show('admin.update');
}
}
public function users()

View File

@ -19,6 +19,11 @@ class Storage
return $result;
}
public static function put($filename, $data)
{
return file_put_contents($filename, $data);
}
public static function exists($filename)
{
return file_exists($filename);

View File

@ -2,16 +2,50 @@
namespace App\Services;
use \Blessing\Storage;
class Updater
{
/**
* Current version
*
* @var string
*/
public $current_version = "";
/**
* Latest version
*
* @var string
*/
public $latest_version = "";
/**
* Latest update time in Y-m-d H:i:s format
*
* @var string
*/
public $update_time = "";
/**
* See /config/update.php
*
* @var array
*/
private $update_sources = null;
/**
* Current selected update source
*
* @var array
*/
private $current_source = null;
/**
* Details of updates
*
* @var array
*/
private $update_info = null;
public function __construct($current_version)
@ -21,6 +55,11 @@ class Updater
$this->current_source = $this->update_sources[\Option::get('update_source')];
}
/**
* Get update info from selected json source
*
* @return array Decoded json
*/
public function getUpdateInfo()
{
$ch = curl_init();
@ -30,34 +69,58 @@ class Updater
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec($ch);
curl_close($ch);
$this->update_info = json_decode($result, true);
return $this->update_info;
}
/**
* Check for updates
*
* @return void
*/
public function checkUpdate()
{
$info = $this->getUpdateInfo();
$this->latest_version = $info['latest_version'];
$this->update_time = date('Y-m-d H:i:s', $info['update_time']);
}
/**
* Download update files
*
* @param bool $silent Don't print messages
* @return void
*/
public function downloadUpdate($silent = true)
{
$release_url = $this->update_info['releases'][$this->latest_version]['release_url'];
if (!$silent) echo "<p>正在下载更新包:$release_url </p>";
// I don't know why curl cant get full file here..
$file = fopen($release_url, 'r');
if (!$silent) echo "<p>下载完成。</p>";
if (!$silent)
echo "<p>正在下载更新包:$release_url </p>";
// I don't know why curl can't get full file content here..
$file = file_get_contents($release_url);
if (!$silent)
echo "<p>下载完成。</p>";
$update_cache = BASE_DIR."/setup/update_cache/";
if (!is_dir($update_cache)) mkdir($update_cache);
if (!is_dir($update_cache)) {
if (false === mkdir($update_cache)) {
exit('<p>创建下载缓存文件夹失败,请检查目录权限。</p>');
}
}
$zip_path = $update_cache."update_".time().".zip";
if (file_put_contents($zip_path, $file) === false) {
\Storage::removeDir(BASE_DIR.'/setup/update_cache/');
if (Storage::put($zip_path, $file) === false) {
Storage::removeDir(BASE_DIR.'/setup/update_cache/');
return false;
}
return $zip_path;
}
@ -86,7 +149,7 @@ class Updater
*/
private function compareVersion($v1, $v2)
{
if (strnatcasecmp($v1, $v2) > 0) {
if (version_compare($v1, $v2) > 0) {
// v1 > v2
return true;
} else {

View File

@ -9,15 +9,10 @@
*/
return array(
'github' => [
'name' => 'GitHub',
'update_url' => 'https://work.prinzeugen.net/update.json',
'description' => '从 <a href="https://prinzeugen.net/">作者主机</a> 上取更新信息GitHub Releases 上取更新包。国内主机可能会奇慢无比,请注意。'
],
'nyavm' => [
'name' => 'NyaVM',
'update_url' => 'http://anycast.cdn.nyavm.net/',
'description' => '啦啦啦'
'update_url' => 'http://work.prinzeugen.net/update.json',
'description' => '感谢 <a href="https://www.nyavm.com/">NyaVM</a> 提供的 Anycast CDN国内外主机都可获得不错的速度。NyaVM 主机,百兆 CN2 只需 $3.99 月付,稳定快速大带宽。'
],
'little_service' => [
'name' => 'LittleService-COS',