Use Guzzle to request update source

This commit is contained in:
printempw 2018-08-15 18:21:32 +08:00
parent bf167639ff
commit 82e78d548d

View File

@ -2,9 +2,7 @@
namespace App\Http\Controllers; namespace App\Http\Controllers;
use Arr;
use Log; use Log;
use Utils;
use File; use File;
use Cache; use Cache;
use Option; use Option;
@ -17,19 +15,58 @@ use Composer\Semver\Comparator;
class UpdateController extends Controller class UpdateController extends Controller
{ {
public $currentVersion; /**
* Current application version.
*
* @var string
*/
protected $currentVersion;
public $latestVersion; /**
* Latest application version in update source.
*
* @var string
*/
protected $latestVersion;
public $updateSource; /**
* Where to get information of new application versions.
*
* @var string
*/
protected $updateSource;
/**
* Updates information fetched from update source.
*
* @var array|null
*/
protected $updateInfo; protected $updateInfo;
public function __construct() /**
* Guzzle HTTP client.
*
* @var \GuzzleHttp\Client
*/
protected $guzzle;
/**
* Default request options for Guzzle HTTP client.
*
* @var array
*/
protected $guzzleConfig;
public function __construct(\GuzzleHttp\Client $guzzle)
{ {
$this->updateSource = option('update_source'); $this->updateSource = option('update_source');
$this->currentVersion = config('app.version'); $this->currentVersion = config('app.version');
$this->guzzle = $guzzle;
$this->guzzleConfig = [
'headers' => ['User-Agent' => config('secure.user_agent')],
'verify' => config('secure.certificates')
];
} }
public function showUpdatePage() public function showUpdatePage()
@ -55,7 +92,7 @@ class UpdateController extends Controller
); );
if ($detail = $this->getReleaseInfo($info['latest_version'])) { if ($detail = $this->getReleaseInfo($info['latest_version'])) {
$info = array_merge($info, Arr::only($detail, [ $info = array_merge($info, array_only($detail, [
'release_note', 'release_note',
'release_url', 'release_url',
'release_time', 'release_time',
@ -67,20 +104,18 @@ class UpdateController extends Controller
} }
if (! $info['new_version_available']) { if (! $info['new_version_available']) {
$info['release_time'] = Arr::get($this->getReleaseInfo($this->currentVersion), 'release_time'); $info['release_time'] = array_get($this->getReleaseInfo($this->currentVersion), 'release_time');
} }
} }
$update = Option::form('update', OptionForm::AUTO_DETECT, function($form) $update = Option::form('update', OptionForm::AUTO_DETECT, function ($form) {
{ $form->checkbox('check_update')->label();
$form->checkbox('check_update', OptionForm::AUTO_DETECT)->label(OptionForm::AUTO_DETECT); $form->text('update_source')->description();
$form->text('update_source', OptionForm::AUTO_DETECT) })->handle()->always(function ($form) {
->description(OptionForm::AUTO_DETECT);
})->handle()->always(function($form) {
try { try {
$response = file_get_contents(option('update_source')); $response = $this->guzzle->request('GET', option('update_source'), $this->guzzleConfig)->getBody();
} catch (Exception $e) { } catch (Exception $e) {
$form->addMessage(trans('admin.update.errors.connection').$e->getMessage(), 'danger'); $form->addMessage(trans('admin.update.errors.connection').e($e->getMessage()), 'danger');
} }
}); });
@ -111,12 +146,6 @@ class UpdateController extends Controller
$release_url = $this->getReleaseInfo($this->latestVersion)['release_url']; $release_url = $this->getReleaseInfo($this->latestVersion)['release_url'];
$tmp_path = Cache::get('tmp_path'); $tmp_path = Cache::get('tmp_path');
$client = new \GuzzleHttp\Client();
$guzzle_config = [
'headers' => ['User-Agent' => config('secure.user_agent')],
'verify' => config('secure.certificates')
];
switch ($action) { switch ($action) {
case 'prepare-download': case 'prepare-download':
@ -149,7 +178,7 @@ class UpdateController extends Controller
Log::info('[UpdateWizard] Start downloading update package'); Log::info('[UpdateWizard] Start downloading update package');
try { try {
$client->request('GET', $release_url, array_merge($guzzle_config, [ $this->guzzle->request('GET', $release_url, array_merge($this->guzzleConfig, [
'sink' => $tmp_path, 'sink' => $tmp_path,
'progress' => function ($total, $downloaded) { 'progress' => function ($total, $downloaded) {
if ($total == 0) return; if ($total == 0) return;
@ -240,7 +269,7 @@ class UpdateController extends Controller
: $this->updateSource; : $this->updateSource;
try { try {
$response = file_get_contents($url); $response = $this->guzzle->request('GET', $url, $this->guzzleConfig)->getBody();
} catch (Exception $e) { } catch (Exception $e) {
Log::error("[CheckingUpdate] Failed to get update information: ".$e->getMessage()); Log::error("[CheckingUpdate] Failed to get update information: ".$e->getMessage());
} }
@ -248,13 +277,12 @@ class UpdateController extends Controller
if (isset($response)) { if (isset($response)) {
$this->updateInfo = json_decode($response, true); $this->updateInfo = json_decode($response, true);
} }
} }
$this->latestVersion = Arr::get($this->updateInfo, 'latest_version', $this->currentVersion); $this->latestVersion = array_get($this->updateInfo, 'latest_version', $this->currentVersion);
if (! is_null($key)) { if (! is_null($key)) {
return Arr::get($this->updateInfo, $key); return array_get($this->updateInfo, $key);
} }
return $this->updateInfo; return $this->updateInfo;
@ -262,7 +290,7 @@ class UpdateController extends Controller
protected function getReleaseInfo($version) protected function getReleaseInfo($version)
{ {
return Arr::get($this->getUpdateInfo('releases'), $version); return array_get($this->getUpdateInfo('releases'), $version);
} }
} }