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

296 lines
9.5 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;
2016-12-30 20:37:27 +08:00
use Option;
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;
2017-01-01 12:22:18 +08:00
use App\Services\OptionForm;
use Illuminate\Http\Request;
use Composer\Semver\Comparator;
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;
/**
* Default request options for Guzzle HTTP client.
*
* @var array
*/
protected $guzzleConfig;
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;
$this->guzzleConfig = [
'headers' => ['User-Agent' => config('secure.user_agent')],
'verify' => config('secure.certificates')
];
}
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'])) {
2018-08-15 18:21:32 +08:00
$info = array_merge($info, array_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']) {
2018-08-15 18:21:32 +08:00
$info['release_time'] = array_get($this->getReleaseInfo($this->currentVersion), 'release_time');
2016-12-30 20:37:27 +08:00
}
}
$connectivity = true;
try {
$this->guzzle->request('GET', $this->updateSource, $this->guzzleConfig);
} catch (Exception $e) {
$connectivity = $e->getMessage();
}
return view('admin.update', compact('info', 'connectivity'));
}
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);
}
public function download(Request $request)
{
2018-08-17 23:24:08 +08:00
if (! $this->newVersionAvailable())
2018-08-18 09:48:39 +08:00
return json([]);
2018-08-17 23:24:08 +08:00
$action = $request->get('action');
$release_url = $this->getReleaseInfo($this->latestVersion)['release_url'];
2018-08-17 23:24:08 +08:00
$tmp_path = Cache::get('tmp_path');
switch ($action) {
case 'prepare-download':
2018-08-17 23:24:08 +08:00
Cache::forget('download-progress');
$update_cache = storage_path('update_cache');
2018-02-16 17:31:04 +08:00
if (! is_dir($update_cache)) {
2018-06-28 18:25:31 +08:00
if (false === Storage::disk('root')->makeDirectory('storage/update_cache')) {
2018-08-18 09:48:39 +08:00
return json(trans('admin.update.errors.write-permission'), 1);
}
}
2018-08-17 23:24:08 +08:00
// Set temporary path for the update package
$tmp_path = $update_cache.'/update_'.time().'.zip';
Cache::put('tmp_path', $tmp_path, 60);
Log::info('[Update Wizard] Prepare to download update package', compact('release_url', 'tmp_path'));
2018-08-17 23:24:08 +08:00
// We won't get remote file size here since HTTP HEAD method is not always reliable
return json(compact('release_url', 'tmp_path'));
case 'start-download':
2018-08-17 23:24:08 +08:00
if (! $tmp_path) {
2018-08-18 09:48:39 +08:00
return json('No temp path available, please try again.', 1);
2018-02-16 17:31:04 +08:00
}
2018-08-17 23:24:08 +08:00
@set_time_limit(0);
$GLOBALS['last_downloaded'] = 0;
2018-08-17 23:24:08 +08:00
Log::info('[Update Wizard] Start downloading update package');
2018-08-17 23:24:08 +08:00
try {
2018-08-15 18:21:32 +08:00
$this->guzzle->request('GET', $release_url, array_merge($this->guzzleConfig, [
2018-08-17 23:24:08 +08:00
'sink' => $tmp_path,
'progress' => function ($total, $downloaded) {
2018-08-18 09:48:39 +08:00
// @codeCoverageIgnoreStart
2018-08-17 23:24:08 +08:00
if ($total == 0) return;
// Log current progress per 100 KiB
if ($total == $downloaded || floor($downloaded / 102400) > floor($GLOBALS['last_downloaded'] / 102400)) {
$GLOBALS['last_downloaded'] = $downloaded;
Log::info('[Update Wizard] Download progress (in bytes):', [$total, $downloaded]);
Cache::put('download-progress', compact('total', 'downloaded'), 60);
}
2018-08-18 09:48:39 +08:00
// @codeCoverageIgnoreEnd
2018-08-17 23:24:08 +08:00
}
]));
} catch (Exception $e) {
@unlink($tmp_path);
2018-08-18 09:48:39 +08:00
return json(trans('admin.update.errors.prefix').$e->getMessage(), 1);
}
2018-08-17 23:24:08 +08:00
Log::info('[Update Wizard] Finished downloading update package');
return json(compact('tmp_path'));
2018-08-17 23:24:08 +08:00
case 'get-progress':
2018-08-17 23:24:08 +08:00
return json((array) Cache::get('download-progress'));
case 'extract':
2016-11-25 13:13:26 +08:00
2018-02-16 17:31:04 +08:00
if (! file_exists($tmp_path)) {
2018-08-18 09:48:39 +08:00
return json('No file available', 1);
2018-02-16 17:31:04 +08:00
}
2016-11-25 13:13:26 +08:00
$extract_dir = storage_path("update_cache/{$this->latestVersion}");
$zip = new ZipArchive();
$res = $zip->open($tmp_path);
if ($res === true) {
2018-08-17 23:24:08 +08:00
Log::info("[Update Wizard] Extracting file $tmp_path");
2016-11-25 13:13:26 +08:00
2017-11-18 20:36:31 +08:00
if ($zip->extractTo($extract_dir) === false) {
2018-08-18 09:48:39 +08:00
return json(trans('admin.update.errors.prefix').'Cannot unzip file.', 1);
2016-11-25 13:13:26 +08:00
}
} else {
2018-08-18 09:48:39 +08:00
return json(trans('admin.update.errors.unzip').$res, 1);
2016-11-25 13:13:26 +08:00
}
$zip->close();
try {
File::copyDirectory("$extract_dir/vendor", base_path('vendor'));
2018-08-17 23:24:08 +08:00
} catch (Exception $e) {
report($e);
Log::error('[Update Wizard] Unable to extract vendors');
// Skip copying vendor
File::deleteDirectory("$extract_dir/vendor");
}
2017-08-05 15:03:58 +08:00
try {
File::copyDirectory($extract_dir, base_path());
2016-11-25 13:13:26 +08:00
2018-08-17 23:24:08 +08:00
Log::info('[Update Wizard] Overwrite with extracted files');
2016-11-25 13:13:26 +08:00
2018-08-17 23:24:08 +08:00
} catch (Exception $e) {
report($e);
Log::error('[Update Wizard] Error occured when overwriting files');
2016-11-25 13:13:26 +08:00
2017-11-18 20:36:31 +08:00
// Response can be returned, while cache will be cleared
// @see https://gist.github.com/g-plane/2f88ad582826a78e0a26c33f4319c1e0
2018-08-18 09:48:39 +08:00
return json(trans('admin.update.errors.overwrite').$e->getMessage(), 1);
2017-11-18 20:36:31 +08:00
} finally {
2017-08-05 15:03:58 +08:00
File::deleteDirectory(storage_path('update_cache'));
2018-08-17 23:24:08 +08:00
Log::info('[Update Wizard] Cleaning cache');
2016-11-25 13:13:26 +08:00
}
2018-08-17 23:24:08 +08:00
Log::info('[Update Wizard] Done');
2017-01-01 12:22:18 +08:00
return json(trans('admin.update.complete'), 0);
2016-11-25 13:13:26 +08:00
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)
: $this->updateSource;
try {
2018-08-15 18:21:32 +08:00
$response = $this->guzzle->request('GET', $url, $this->guzzleConfig)->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);
}
}
2018-08-15 18:21:32 +08:00
$this->latestVersion = array_get($this->updateInfo, 'latest_version', $this->currentVersion);
2018-02-16 17:31:04 +08:00
if (! is_null($key)) {
2018-08-15 18:21:32 +08:00
return array_get($this->updateInfo, $key);
}
return $this->updateInfo;
}
protected function getReleaseInfo($version)
{
2018-08-15 18:21:32 +08:00
return array_get($this->getUpdateInfo('releases'), $version);
}
}