2016-11-25 12:54:20 +08:00
|
|
|
<?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;
|
2016-11-25 12:54:20 +08:00
|
|
|
use Illuminate\Http\Request;
|
2018-06-30 16:31:39 +08:00
|
|
|
use Composer\Semver\Comparator;
|
2019-04-05 17:23:27 +08:00
|
|
|
use App\Services\PackageManager;
|
2016-11-25 12:54:20 +08:00
|
|
|
|
|
|
|
class UpdateController extends Controller
|
|
|
|
{
|
2018-08-15 18:21:32 +08:00
|
|
|
/**
|
|
|
|
* Current application version.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $currentVersion;
|
2016-11-25 12:54:20 +08:00
|
|
|
|
2018-08-15 18:21:32 +08:00
|
|
|
/**
|
|
|
|
* Latest application version in update source.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $latestVersion;
|
2016-11-25 12:54:20 +08:00
|
|
|
|
2018-08-15 18:21:32 +08:00
|
|
|
/**
|
|
|
|
* Where to get information of new application versions.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $updateSource;
|
2016-11-25 12:54:20 +08:00
|
|
|
|
2018-08-15 18:21:32 +08:00
|
|
|
/**
|
|
|
|
* Updates information fetched from update source.
|
|
|
|
*
|
|
|
|
* @var array|null
|
|
|
|
*/
|
2016-11-25 12:54:20 +08:00
|
|
|
protected $updateInfo;
|
|
|
|
|
2018-08-15 18:21:32 +08:00
|
|
|
/**
|
|
|
|
* Guzzle HTTP client.
|
|
|
|
*
|
|
|
|
* @var \GuzzleHttp\Client
|
|
|
|
*/
|
|
|
|
protected $guzzle;
|
|
|
|
|
|
|
|
public function __construct(\GuzzleHttp\Client $guzzle)
|
2016-11-25 12:54:20 +08:00
|
|
|
{
|
2018-08-15 18:46:57 +08:00
|
|
|
$this->updateSource = config('app.update_source');
|
2016-11-25 12:54:20 +08:00
|
|
|
$this->currentVersion = config('app.version');
|
2018-08-15 18:21:32 +08:00
|
|
|
|
|
|
|
$this->guzzle = $guzzle;
|
2016-11-25 12:54:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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' => '',
|
2019-03-02 22:58:37 +08:00
|
|
|
'new_version_available' => false,
|
2016-11-25 12:54:20 +08:00
|
|
|
];
|
|
|
|
|
2018-02-16 17:31:04 +08:00
|
|
|
// If current update source is available
|
2016-11-25 12:54:20 +08:00
|
|
|
if ($this->getUpdateInfo()) {
|
|
|
|
$info['latest_version'] = $this->getUpdateInfo('latest_version');
|
|
|
|
|
2018-06-30 16:31:39 +08:00
|
|
|
$info['new_version_available'] = Comparator::greaterThan(
|
2016-11-25 12:54:20 +08:00
|
|
|
$info['latest_version'],
|
2018-06-30 16:31:39 +08:00
|
|
|
$info['current_version']
|
2016-11-25 12:54:20 +08:00
|
|
|
);
|
|
|
|
|
|
|
|
if ($detail = $this->getReleaseInfo($info['latest_version'])) {
|
2019-02-27 23:44:50 +08:00
|
|
|
$info = array_merge($info, Arr::only($detail, [
|
2016-11-25 12:54:20 +08:00
|
|
|
'release_note',
|
|
|
|
'release_url',
|
|
|
|
'release_time',
|
2019-03-02 22:58:37 +08:00
|
|
|
'pre_release',
|
2016-11-25 12:54:20 +08:00
|
|
|
]));
|
|
|
|
} 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
|
|
|
}
|
2016-11-25 12:54:20 +08:00
|
|
|
}
|
|
|
|
|
2018-08-15 18:46:57 +08:00
|
|
|
$connectivity = true;
|
|
|
|
|
|
|
|
try {
|
2019-04-05 17:23:27 +08:00
|
|
|
$this->guzzle->request('GET', $this->updateSource);
|
2018-08-15 18:46:57 +08:00
|
|
|
} catch (Exception $e) {
|
|
|
|
$connectivity = $e->getMessage();
|
|
|
|
}
|
2016-11-25 12:54:20 +08:00
|
|
|
|
2019-03-23 19:52:14 +08:00
|
|
|
$extra = ['canUpdate' => $info['new_version_available']];
|
|
|
|
return view('admin.update', compact('info', 'connectivity', 'extra'));
|
2016-11-25 12:54:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public function checkUpdates()
|
|
|
|
{
|
|
|
|
return json([
|
|
|
|
'latest' => $this->getUpdateInfo('latest_version'),
|
2019-03-02 22:58:37 +08:00
|
|
|
'available' => $this->newVersionAvailable(),
|
2016-11-25 12:54:20 +08:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function newVersionAvailable()
|
|
|
|
{
|
|
|
|
$latest = $this->getUpdateInfo('latest_version');
|
|
|
|
|
2018-06-30 16:31:39 +08:00
|
|
|
return Comparator::greaterThan($latest, $this->currentVersion) && $this->getReleaseInfo($latest);
|
2016-11-25 12:54:20 +08:00
|
|
|
}
|
|
|
|
|
2019-04-05 17:23:27 +08:00
|
|
|
public function download(Request $request, PackageManager $package)
|
2016-11-25 12:54:20 +08:00
|
|
|
{
|
2019-03-02 22:58:37 +08:00
|
|
|
if (! $this->newVersionAvailable()) {
|
2018-08-18 09:48:39 +08:00
|
|
|
return json([]);
|
2019-03-02 22:58:37 +08:00
|
|
|
}
|
2016-11-25 12:54:20 +08:00
|
|
|
|
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':
|
2017-08-05 16:00:14 +08:00
|
|
|
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);
|
2017-08-05 16:00:14 +08:00
|
|
|
}
|
2019-04-05 17:23:27 +08:00
|
|
|
case 'progress':
|
|
|
|
return $package->progress();
|
2016-11-25 12:54:20 +08:00
|
|
|
default:
|
2017-11-18 20:36:31 +08:00
|
|
|
return json(trans('general.illegal-parameters'), 1);
|
2016-11-25 12:54:20 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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')
|
2019-03-02 22:58:37 +08:00
|
|
|
? $this->updateSource.'?v='.substr(time(), 0, -3)
|
2017-11-18 20:36:31 +08:00
|
|
|
: $this->updateSource;
|
2016-11-25 12:54:20 +08:00
|
|
|
|
|
|
|
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) {
|
2019-03-02 22:58:37 +08:00
|
|
|
Log::error('[CheckingUpdate] Failed to get update information: '.$e->getMessage());
|
2016-11-25 12:54:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2016-11-25 12:54:20 +08:00
|
|
|
|
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);
|
2016-11-25 12:54:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return $this->updateInfo;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function getReleaseInfo($version)
|
|
|
|
{
|
2019-02-27 23:44:50 +08:00
|
|
|
return Arr::get($this->getUpdateInfo('releases'), $version);
|
2016-11-25 12:54:20 +08:00
|
|
|
}
|
|
|
|
}
|