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

106 lines
3.0 KiB
PHP
Raw Normal View History

<?php
namespace App\Http\Controllers;
2018-08-17 23:24:08 +08:00
use Exception;
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
protected $currentVersion;
protected $updateSource;
protected $guzzle;
2019-04-06 22:52:43 +08:00
protected $error;
protected $info = [];
2018-08-15 18:21:32 +08:00
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 = [
2019-04-06 22:52:43 +08:00
'latest' => Arr::get($this->getUpdateInfo(), 'latest'),
'current' => $this->currentVersion,
];
2019-04-06 22:52:43 +08:00
$error = $this->error;
$extra = ['canUpdate' => $this->canUpdate()];
2019-04-19 19:36:36 +08:00
2019-04-06 22:52:43 +08:00
return view('admin.update', compact('info', 'error', 'extra'));
}
public function checkUpdates()
{
2019-04-06 22:52:43 +08:00
return json(['available' => $this->canUpdate()]);
}
2019-04-05 17:23:27 +08:00
public function download(Request $request, PackageManager $package)
{
2019-04-06 22:52:43 +08:00
if (! $this->canUpdate()) {
2018-08-18 09:48:39 +08:00
return json([]);
}
2019-04-06 22:52:43 +08:00
$path = storage_path('packages/bs_'.$this->info['latest'].'.zip');
2019-04-05 17:23:27 +08:00
switch ($request->get('action')) {
case 'download':
try {
2019-04-06 22:52:43 +08:00
$package->download($this->info['url'], $path)->extract(base_path());
2019-04-19 19:36:36 +08:00
2019-04-05 17:23:27 +08:00
return json(trans('admin.update.complete'), 0);
2018-08-17 23:24:08 +08:00
} catch (Exception $e) {
report($e);
2019-04-19 19:36:36 +08:00
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);
}
}
2019-04-06 22:52:43 +08:00
protected function getUpdateInfo()
{
2019-07-05 14:48:12 +08:00
$acceptableSpec = 2;
if (app()->runningUnitTests() || ! $this->info) {
try {
2019-04-19 23:15:05 +08:00
$json = $this->guzzle->request(
'GET',
$this->updateSource,
['verify' => \Composer\CaBundle\CaBundle::getSystemCaRootBundlePath()]
2019-04-19 23:15:05 +08:00
)->getBody();
2019-04-06 22:52:43 +08:00
$info = json_decode($json, true);
if (Arr::get($info, 'spec') == $acceptableSpec) {
$this->info = $info;
} else {
2019-07-05 14:48:12 +08:00
$this->error = trans('admin.update.errors.spec');
2019-04-06 22:52:43 +08:00
}
2018-08-17 23:24:08 +08:00
} catch (Exception $e) {
2019-04-06 22:52:43 +08:00
$this->error = $e->getMessage();
}
}
2019-04-19 19:36:36 +08:00
2019-04-06 22:52:43 +08:00
return $this->info;
}
2019-04-06 22:52:43 +08:00
protected function canUpdate()
{
2019-04-06 22:52:43 +08:00
$this->getUpdateInfo();
2019-04-19 19:36:36 +08:00
2019-07-05 14:48:12 +08:00
$php = Arr::get($this->info, 'php');
if (Comparator::lessThan(PHP_VERSION, $php)) {
$this->error = trans('admin.update.errors.php', ['version' => $php]);
2019-07-05 14:48:12 +08:00
return false;
}
2019-04-06 22:52:43 +08:00
return Comparator::greaterThan(Arr::get($this->info, 'latest'), $this->currentVersion);
}
}