blessing-skin-server/app/Http/Controllers/UpdateController.php

184 lines
5.1 KiB
PHP
Raw Normal View History

<?php
namespace App\Http\Controllers;
use Log;
2017-08-05 15:03:58 +08:00
use File;
2018-08-17 23:24:08 +08:00
use Cache;
2017-11-18 20:36:31 +08:00
use Storage;
2018-08-17 23:24:08 +08:00
use Exception;
2016-11-25 13:13:26 +08:00
use ZipArchive;
2019-02-27 23:44:50 +08:00
use Illuminate\Support\Arr;
use Illuminate\Http\Request;
use Composer\Semver\Comparator;
2019-04-05 17:23:27 +08:00
use App\Services\PackageManager;
class UpdateController extends Controller
{
2018-08-15 18:21:32 +08:00
/**
* Current application version.
*
* @var string
*/
protected $currentVersion;
2018-08-15 18:21:32 +08:00
/**
* Latest application version in update source.
*
* @var string
*/
protected $latestVersion;
2018-08-15 18:21:32 +08:00
/**
* Where to get information of new application versions.
*
* @var string
*/
protected $updateSource;
2018-08-15 18:21:32 +08:00
/**
* Updates information fetched from update source.
*
* @var array|null
*/
protected $updateInfo;
2018-08-15 18:21:32 +08:00
/**
* Guzzle HTTP client.
*
* @var \GuzzleHttp\Client
*/
protected $guzzle;
public function __construct(\GuzzleHttp\Client $guzzle)
{
$this->updateSource = config('app.update_source');
$this->currentVersion = config('app.version');
2018-08-15 18:21:32 +08:00
$this->guzzle = $guzzle;
}
public function showUpdatePage()
{
$info = [
'latest_version' => '',
'current_version' => $this->currentVersion,
'release_note' => '',
'release_url' => '',
'pre_release' => false,
2018-02-16 17:31:04 +08:00
// Fallback to current time
2016-12-30 20:37:27 +08:00
'release_time' => '',
'new_version_available' => false,
];
2018-02-16 17:31:04 +08:00
// If current update source is available
if ($this->getUpdateInfo()) {
$info['latest_version'] = $this->getUpdateInfo('latest_version');
$info['new_version_available'] = Comparator::greaterThan(
$info['latest_version'],
$info['current_version']
);
if ($detail = $this->getReleaseInfo($info['latest_version'])) {
2019-02-27 23:44:50 +08:00
$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;
}
2016-12-30 20:37:27 +08:00
2018-02-16 17:31:04 +08:00
if (! $info['new_version_available']) {
2019-02-27 23:44:50 +08:00
$info['release_time'] = Arr::get($this->getReleaseInfo($this->currentVersion), 'release_time');
2016-12-30 20:37:27 +08:00
}
}
$connectivity = true;
try {
2019-04-05 17:23:27 +08:00
$this->guzzle->request('GET', $this->updateSource);
} catch (Exception $e) {
$connectivity = $e->getMessage();
}
2019-03-23 19:52:14 +08:00
$extra = ['canUpdate' => $info['new_version_available']];
return view('admin.update', compact('info', 'connectivity', 'extra'));
}
public function checkUpdates()
{
return json([
'latest' => $this->getUpdateInfo('latest_version'),
'available' => $this->newVersionAvailable(),
]);
}
protected function newVersionAvailable()
{
$latest = $this->getUpdateInfo('latest_version');
return Comparator::greaterThan($latest, $this->currentVersion) && $this->getReleaseInfo($latest);
}
2019-04-05 17:23:27 +08:00
public function download(Request $request, PackageManager $package)
{
if (! $this->newVersionAvailable()) {
2018-08-18 09:48:39 +08:00
return json([]);
}
2019-04-05 17:23:27 +08:00
$url = $this->getReleaseInfo($this->latestVersion)['release_url'];
$path = storage_path('packages/bs_'.$this->latestVersion.'.zip');
switch ($request->get('action')) {
case 'download':
try {
2019-04-05 17:23:27 +08:00
$package->download($url, $path)->extract(base_path());
return json(trans('admin.update.complete'), 0);
2018-08-17 23:24:08 +08:00
} catch (Exception $e) {
report($e);
2019-04-05 17:23:27 +08:00
return json($e->getMessage(), 1);
}
2019-04-05 17:23:27 +08:00
case 'progress':
return $package->progress();
default:
2017-11-18 20:36:31 +08:00
return json(trans('general.illegal-parameters'), 1);
}
}
protected function getUpdateInfo($key = null)
{
2018-02-16 17:31:04 +08:00
if (! $this->updateInfo) {
// Add timestamp to control cdn cache
2017-11-18 20:36:31 +08:00
$url = starts_with($this->updateSource, 'http')
? $this->updateSource.'?v='.substr(time(), 0, -3)
2017-11-18 20:36:31 +08:00
: $this->updateSource;
try {
2019-04-05 17:23:27 +08:00
$response = $this->guzzle->request('GET', $url)->getBody();
2018-08-17 23:24:08 +08:00
} catch (Exception $e) {
Log::error('[CheckingUpdate] Failed to get update information: '.$e->getMessage());
}
if (isset($response)) {
$this->updateInfo = json_decode($response, true);
}
}
2019-02-27 23:44:50 +08:00
$this->latestVersion = Arr::get($this->updateInfo, 'latest_version', $this->currentVersion);
2018-02-16 17:31:04 +08:00
if (! is_null($key)) {
2019-02-27 23:44:50 +08:00
return Arr::get($this->updateInfo, $key);
}
return $this->updateInfo;
}
protected function getReleaseInfo($version)
{
2019-02-27 23:44:50 +08:00
return Arr::get($this->getUpdateInfo('releases'), $version);
}
}