now we have an awesome progress bar when download updates
This commit is contained in:
parent
6834b68948
commit
9b6c18cd6b
@ -50,7 +50,7 @@ class SetupController extends Controller
|
||||
|
||||
// skip if the file is not valid or expired
|
||||
if (!isset($matches[2]) ||
|
||||
version_compare($matches[2], option('version'), '<')) {
|
||||
version_compare($matches[2], config('app.version'), '<')) {
|
||||
continue;
|
||||
}
|
||||
|
||||
|
184
app/Http/Controllers/UpdateController.php
Normal file
184
app/Http/Controllers/UpdateController.php
Normal file
@ -0,0 +1,184 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use Arr;
|
||||
use Log;
|
||||
use Utils;
|
||||
use App\Services\Storage;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class UpdateController extends Controller
|
||||
{
|
||||
public $currentVersion;
|
||||
|
||||
public $latestVersion;
|
||||
|
||||
public $updateSource;
|
||||
|
||||
protected $updateInfo;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->updateSource = option('update_source');
|
||||
|
||||
$this->currentVersion = config('app.version');
|
||||
}
|
||||
|
||||
public function showUpdatePage()
|
||||
{
|
||||
$info = [
|
||||
'latest_version' => '',
|
||||
'current_version' => $this->currentVersion,
|
||||
'release_note' => '',
|
||||
'release_url' => '',
|
||||
'pre_release' => false,
|
||||
// fallback to current time
|
||||
'release_time' => time(),
|
||||
'new_version_available' => false
|
||||
];
|
||||
|
||||
// if current update source is available
|
||||
if ($this->getUpdateInfo()) {
|
||||
$info['latest_version'] = $this->getUpdateInfo('latest_version');
|
||||
|
||||
if ($current_release = $this->getReleaseInfo($this->currentVersion)) {
|
||||
$info['release_time'] = Arr::get($current_release, 'release_time') ?: time();
|
||||
}
|
||||
|
||||
$info['new_version_available'] = version_compare(
|
||||
$info['latest_version'],
|
||||
$info['current_version'], '>'
|
||||
);
|
||||
|
||||
if ($detail = $this->getReleaseInfo($info['latest_version'])) {
|
||||
$info = array_merge($info, Arr::only($detail, [
|
||||
'release_note',
|
||||
'release_url',
|
||||
'release_time',
|
||||
'pre_release'
|
||||
]));
|
||||
} else {
|
||||
// if detailed release info is not given
|
||||
$info['new_version_available'] = false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return view('admin.update')->with('info', $info);
|
||||
}
|
||||
|
||||
public function checkUpdates()
|
||||
{
|
||||
return json([
|
||||
'latest' => $this->getUpdateInfo('latest_version'),
|
||||
'available' => $this->newVersionAvailable()
|
||||
]);
|
||||
}
|
||||
|
||||
protected function newVersionAvailable()
|
||||
{
|
||||
$latest = $this->getUpdateInfo('latest_version');
|
||||
|
||||
return version_compare($latest, $this->currentVersion, '>') && $this->getReleaseInfo($latest);
|
||||
}
|
||||
|
||||
public function download(Request $request)
|
||||
{
|
||||
$action = $request->input('action');
|
||||
|
||||
if (!$this->newVersionAvailable()) return;
|
||||
|
||||
$release_url = $this->getReleaseInfo($this->latestVersion)['release_url'];
|
||||
$file_size = Utils::getRemoteFileSize($release_url);
|
||||
$tmp_path = session('tmp_path');
|
||||
|
||||
switch ($action) {
|
||||
case 'prepare-download':
|
||||
|
||||
$update_cache = storage_path('update_cache');
|
||||
|
||||
if (!is_dir($update_cache)) {
|
||||
if (false === mkdir($update_cache)) {
|
||||
exit('创建下载缓存文件夹失败,请检查目录权限。');
|
||||
}
|
||||
}
|
||||
|
||||
$tmp_path = $update_cache."/update_".time().".zip";
|
||||
|
||||
session(['tmp_path' => $tmp_path]);
|
||||
|
||||
return json(compact('release_url', 'tmp_path', 'file_size'));
|
||||
|
||||
break;
|
||||
|
||||
case 'start-download':
|
||||
|
||||
if (!session()->has('tmp_path')) return "No temp path is set.";
|
||||
|
||||
try {
|
||||
Utils::download($release_url, $tmp_path);
|
||||
|
||||
} catch (\Exception $e) {
|
||||
Storage::remove($tmp_path);
|
||||
|
||||
exit('发生错误:'.$e->getMessage());
|
||||
}
|
||||
|
||||
return json(compact('tmp_path'));
|
||||
|
||||
break;
|
||||
|
||||
case 'get-file-size':
|
||||
|
||||
if (!session()->has('tmp_path')) return "No temp path is set.";
|
||||
|
||||
if (file_exists($tmp_path)) {
|
||||
return json(['size' => filesize($tmp_path)]);
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case 'extract':
|
||||
//
|
||||
break;
|
||||
|
||||
default:
|
||||
# code...
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
protected function getUpdateInfo($key = null)
|
||||
{
|
||||
if (!$this->updateInfo) {
|
||||
// add timestamp to control cdn cache
|
||||
$url = $this->updateSource."?v=".substr(time(), 0, -3);
|
||||
|
||||
try {
|
||||
$response = file_get_contents($url);
|
||||
} catch (\Exception $e) {
|
||||
Log::error("[CheckingUpdate] Failed to get update information: ".$e->getMessage());
|
||||
}
|
||||
|
||||
if (isset($response)) {
|
||||
$this->updateInfo = json_decode($response, true);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
$this->latestVersion = Arr::get($this->updateInfo, 'latest_version', $this->currentVersion);
|
||||
|
||||
if (!is_null($key)) {
|
||||
return Arr::get($this->updateInfo, $key);
|
||||
}
|
||||
|
||||
return $this->updateInfo;
|
||||
}
|
||||
|
||||
protected function getReleaseInfo($version)
|
||||
{
|
||||
return Arr::get($this->getUpdateInfo('releases'), $version);
|
||||
}
|
||||
|
||||
}
|
@ -102,7 +102,12 @@ Route::group(['middleware' => 'admin', 'prefix' => 'admin'], function ()
|
||||
Route::any('/customize', 'AdminController@customize');
|
||||
Route::any('/score', 'AdminController@score');
|
||||
Route::any('/options', 'AdminController@options');
|
||||
Route::any('/update', 'AdminController@update');
|
||||
|
||||
Route::any('/update', 'UpdateController@showUpdatePage');
|
||||
|
||||
Route::any('/update/download', 'UpdateController@download');
|
||||
|
||||
Route::get('/update/check', 'UpdateController@checkUpdates');
|
||||
|
||||
Route::get('/users', 'AdminController@users');
|
||||
Route::get('/players', 'AdminController@players');
|
||||
|
@ -40,6 +40,64 @@ class Utils
|
||||
}
|
||||
}
|
||||
|
||||
public static function download($url, $path)
|
||||
{
|
||||
set_time_limit(0);
|
||||
|
||||
touch($path);
|
||||
|
||||
Log::info("[File Downloader] Download started, source: $url");
|
||||
Log::info("[File Downloader] ======================================");
|
||||
|
||||
if ($fp = fopen($url, "rb")) {
|
||||
|
||||
if (!$download_fp = fopen($path, "wb")) {
|
||||
return false;
|
||||
}
|
||||
|
||||
while (!feof($fp)) {
|
||||
|
||||
if (!file_exists($path)) {
|
||||
// cancel downloading if destination is no longer available
|
||||
fclose($download_fp);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
Log::info("[Download] 1024 bytes wrote");
|
||||
fwrite($download_fp, fread($fp, 1024 * 8 ), 1024 * 8);
|
||||
}
|
||||
|
||||
fclose($download_fp);
|
||||
fclose($fp);
|
||||
|
||||
Log::info("[File Downloader] Finished downloading, data stored to: $path");
|
||||
Log::info("[File Downloader] ===========================================");
|
||||
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static function getRemoteFileSize($url)
|
||||
{
|
||||
$regex = '/^Content-Length: *+\K\d++$/im';
|
||||
|
||||
if (!$fp = @fopen($url, 'rb')) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (
|
||||
isset($http_response_header) &&
|
||||
preg_match($regex, implode("\n", $http_response_header), $matches)
|
||||
) {
|
||||
return (int)$matches[0];
|
||||
}
|
||||
|
||||
return strlen(stream_get_contents($fp));
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate random string
|
||||
*
|
||||
|
@ -9,7 +9,7 @@ return [
|
||||
| Version of Blessing Skin Server
|
||||
|
|
||||
*/
|
||||
'version' => '3.1.2',
|
||||
'version' => '3.2-pr2',
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
|
@ -2,7 +2,7 @@
|
||||
* @Author: printempw
|
||||
* @Date: 2016-07-22 14:02:44
|
||||
* @Last Modified by: printempw
|
||||
* @Last Modified time: 2016-09-24 19:58:24
|
||||
* @Last Modified time: 2016-11-25 12:45:28
|
||||
*/
|
||||
|
||||
'use strict';
|
||||
@ -309,3 +309,80 @@ function deletePlayer(pid) {
|
||||
error: showAjaxError
|
||||
});
|
||||
}
|
||||
|
||||
function downloadUpdates() {
|
||||
var file_size = 0;
|
||||
var progress = 0;
|
||||
|
||||
console.log("Prepared to download");
|
||||
|
||||
$.ajax({
|
||||
url: './update/download?action=prepare-download',
|
||||
type: 'GET',
|
||||
dataType: 'json',
|
||||
beforeSend: function() {
|
||||
$('#update-button').html('<i class="fa fa-spinner fa-spin"></i> 正在准备').prop('disabled', 'disabled');
|
||||
},
|
||||
})
|
||||
.done(function(json) {
|
||||
console.log(json);
|
||||
|
||||
file_size = json.file_size;
|
||||
|
||||
$('#file-size').html(file_size);
|
||||
|
||||
$('#modal-start-download').modal({
|
||||
'backdrop': 'static',
|
||||
'keyboard': false
|
||||
});
|
||||
|
||||
console.log("started downloading");
|
||||
$.ajax({
|
||||
url: './update/download?action=start-download',
|
||||
type: 'POST',
|
||||
dataType: 'json'
|
||||
})
|
||||
.done(function(json) {
|
||||
// set progress to 100 when got the response
|
||||
progress = 100;
|
||||
|
||||
console.log("Downloading finished");
|
||||
console.log(json);
|
||||
})
|
||||
.fail(showAjaxError);
|
||||
|
||||
var interval_id = window.setInterval(function() {
|
||||
|
||||
if (progress == 100) {
|
||||
clearInterval(interval_id);
|
||||
|
||||
$('#modal-start-download').modal('toggle');
|
||||
|
||||
swal({
|
||||
type: 'success',
|
||||
html: '下载完成!'
|
||||
}).then(function(new_name) {
|
||||
//
|
||||
});
|
||||
} else {
|
||||
$.ajax({
|
||||
url: './update/download?action=get-file-size',
|
||||
type: 'GET'
|
||||
})
|
||||
.done(function(json) {
|
||||
progress = (json.size / file_size * 100).toFixed(2);
|
||||
|
||||
$('#imported-progress').html(progress);
|
||||
$('.progress-bar').css('width', progress+'%').attr('aria-valuenow', progress);
|
||||
|
||||
console.log("Progress: "+progress);
|
||||
})
|
||||
.fail(showAjaxError);
|
||||
}
|
||||
|
||||
}, 300);
|
||||
|
||||
})
|
||||
.fail(showAjaxError);
|
||||
|
||||
}
|
||||
|
@ -118,10 +118,11 @@
|
||||
@if (option('check_update') == '1')
|
||||
<script>
|
||||
$(document).ready(function() {
|
||||
var url = "{{ url('admin/update') }}";
|
||||
$.getJSON(url + '?action=check', function(data) {
|
||||
if (data.new_version_available == true)
|
||||
$('[href="' + url + '"]').append('<span class="label label-primary pull-right">v'+data.latest_version+'</span>');
|
||||
// check for updates
|
||||
$.getJSON("{{ url('admin/update/check') }}", function(data) {
|
||||
if (data.available == true) {
|
||||
$('[href="{{ url('admin/update') }}"]').append('<span class="label label-primary pull-right">v'+data.latest+'</span>');
|
||||
}
|
||||
});
|
||||
});
|
||||
</script>
|
||||
|
@ -21,7 +21,6 @@
|
||||
</h1>
|
||||
</section>
|
||||
|
||||
<?php $updater = new Updater(config('app.version')); ?>
|
||||
<!-- Main content -->
|
||||
<section class="content">
|
||||
<div class="row">
|
||||
@ -31,41 +30,45 @@
|
||||
<h3 class="box-title">更新信息</h3>
|
||||
</div><!-- /.box-header -->
|
||||
<div class="box-body">
|
||||
@if ($updater->newVersionAvailable())
|
||||
@if ($info['new_version_available'])
|
||||
<div class="callout callout-info">有更新可用。</div>
|
||||
<table class="table">
|
||||
<tbody>
|
||||
<tr>
|
||||
<td class="key">最新版本:</td>
|
||||
<td class="value">
|
||||
v{{ $updater->latest_version }}
|
||||
v{{ $info['latest_version'] }}
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td class="key">当前版本:</td>
|
||||
<td class="value">
|
||||
v{{ $updater->current_version }}
|
||||
v{{ $info['current_version'] }}
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td class="key">发布时间:</td>
|
||||
<td class="value">
|
||||
{{ $updater->update_time }}
|
||||
{{ Utils::getTimeFormatted($info['release_time']) }}
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td class="key">更新日志:</td>
|
||||
<td class="value">
|
||||
{!! nl2br($updater->getUpdateInfo()['releases'][$updater->latest_version]['release_note']) !!}
|
||||
{!! nl2br($info['release_note']) ?: "无内容" !!}
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td class="key">下载地址:</td>
|
||||
<td class="value">
|
||||
<a href="{!! $updater->getUpdateInfo()['releases'][$updater->latest_version]['release_url'] !!}">@GitHub</a>
|
||||
<a href="{!! $info['release_url'] !!}">点击下载完整安装包</a>
|
||||
</td>
|
||||
</tr>
|
||||
|
||||
@if($info['pre_release'])
|
||||
<div class="callout callout-warning">本次更新为预发布版,请谨慎选择是否更新。</div>
|
||||
@endif
|
||||
|
||||
</tbody>
|
||||
</table>
|
||||
@else
|
||||
@ -75,13 +78,17 @@
|
||||
<tr>
|
||||
<td class="key">当前版本:</td>
|
||||
<td class="value">
|
||||
v{{ $updater->current_version }}
|
||||
v{{ $info['current_version'] }}
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td class="key">发布时间:</td>
|
||||
<td class="value">
|
||||
{{ @date('Y-m-d H:i:s', $updater->getUpdateInfo()['releases'][$updater->current_version]['release_time']) }}
|
||||
@if (isset($info['release_time']))
|
||||
{{ Utils::getTimeFormatted($info['release_time']) }}
|
||||
@else
|
||||
当前版本为未发布测试版
|
||||
@endif
|
||||
</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
@ -89,7 +96,7 @@
|
||||
@endif
|
||||
</div><!-- /.box-body -->
|
||||
<div class="box-footer">
|
||||
<a href="?action=download" class="btn btn-primary" {{ !$updater->newVersionAvailable() ? 'disabled="disabled"' : '' }} >马上升级</a>
|
||||
<a class="btn btn-primary" id="update-button" {!! !$info['new_version_available'] ? 'disabled="disabled"' : 'href="javascript:downloadUpdates();"' !!}>马上升级</a>
|
||||
<a href="http://www.mcbbs.net/thread-552877-1-1.html" style="float: right;" class="btn btn-default">查看 MCBBS 发布贴</a>
|
||||
</div>
|
||||
</div>
|
||||
@ -120,12 +127,19 @@
|
||||
if ($key != "option" && $key != "submit")
|
||||
Option::set($key, $value);
|
||||
}
|
||||
|
||||
echo '<div class="callout callout-success">设置已保存。</div>';
|
||||
} ?>
|
||||
}
|
||||
|
||||
try {
|
||||
$response = file_get_contents(option('update_source'));
|
||||
} catch (Exception $e) {
|
||||
echo '<div class="callout callout-danger">无法访问当前更新源。详细信息:'.$e->getMessage().'</div>';
|
||||
}
|
||||
|
||||
?>
|
||||
<table class="table">
|
||||
<tbody>
|
||||
|
||||
|
||||
<tr>
|
||||
<td class="key">检查更新</td>
|
||||
<td class="value">
|
||||
@ -135,19 +149,12 @@
|
||||
</td>
|
||||
</tr>
|
||||
|
||||
<?php $current_source = option('update_source'); ?>
|
||||
<tr>
|
||||
<td class="key">更新源</td>
|
||||
<td class="value">
|
||||
<select class="form-control" name="update_source">
|
||||
@foreach ($updater->getUpdateSources() as $key => $value)
|
||||
<option {!! $current_source == $key ? 'selected="selected"' : '' !!} value="{{ $key }}">{{ $value['name'] }}</option>
|
||||
@endforeach
|
||||
</select>
|
||||
<input type="text" class="form-control" name="update_source" value="{{ option('update_source') }}">
|
||||
|
||||
@foreach ($updater->getUpdateSources() as $key => $value)
|
||||
<p class="description" id="{{ $key }}" {!! $current_source == $key ? '' : 'style="display: none;"' !!}>{!! $value['description'] !!}</p>
|
||||
@endforeach
|
||||
<p class="description">可用的更新源列表可以在这里查看:<a href="https://github.com/printempw/blessing-skin-server/wiki/%E6%9B%B4%E6%96%B0%E6%BA%90%E5%88%97%E8%A1%A8">@GitHub Wiki</a></p>
|
||||
</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
@ -164,13 +171,23 @@
|
||||
</section><!-- /.content -->
|
||||
</div><!-- /.content-wrapper -->
|
||||
|
||||
@endsection
|
||||
<div id="modal-start-download" class="modal fade" tabindex="-1" role="dialog">
|
||||
<div class="modal-dialog">
|
||||
<div class="modal-content">
|
||||
<div class="modal-header">
|
||||
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
|
||||
<h4 class="modal-title">正在下载更新包</h4>
|
||||
</div>
|
||||
<div class="modal-body">
|
||||
<p>更新包大小:<span id="file-size">0</span> Bytes</p>
|
||||
<div class="progress">
|
||||
<div class="progress-bar progress-bar-striped active" role="progressbar" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100" style="width: 0%">
|
||||
<span id="imported-progress">0</span>%
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div><!-- /.modal-content -->
|
||||
</div><!-- /.modal-dialog -->
|
||||
</div><!-- /.modal -->
|
||||
|
||||
@section('script')
|
||||
<script>
|
||||
$('select[name=update_source]').change(function() {
|
||||
$('.description').hide();
|
||||
$('#' + this.value).show();
|
||||
});
|
||||
</script>
|
||||
@endsection
|
||||
|
@ -6,7 +6,7 @@
|
||||
<p>数据库升级成功,欢迎使用 Blessing Skin Server {{ config('app.version') }}!</p>
|
||||
|
||||
{{-- if any tip is given --}}
|
||||
@if (isset($tips))
|
||||
@if (!empty($tips))
|
||||
<p><b>升级提示:</b></p>
|
||||
<ul>
|
||||
@foreach ($tips as $tip)
|
||||
|
2
storage/update_cache/.gitignore
vendored
Normal file
2
storage/update_cache/.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
*
|
||||
!.gitignore
|
Loading…
Reference in New Issue
Block a user