2016-11-25 12:54:20 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
|
|
|
use Arr;
|
|
|
|
use Log;
|
|
|
|
use Utils;
|
2017-08-05 15:03:58 +08:00
|
|
|
use File;
|
2016-12-30 20:37:27 +08:00
|
|
|
use Option;
|
2017-11-18 20:36:31 +08:00
|
|
|
use Storage;
|
2016-11-25 13:13:26 +08:00
|
|
|
use ZipArchive;
|
2017-01-01 12:22:18 +08:00
|
|
|
use App\Services\OptionForm;
|
2016-11-25 12:54:20 +08:00
|
|
|
use Illuminate\Http\Request;
|
|
|
|
|
|
|
|
class UpdateController extends Controller
|
|
|
|
{
|
|
|
|
public $currentVersion;
|
|
|
|
|
|
|
|
public $latestVersion;
|
|
|
|
|
|
|
|
public $updateSource;
|
|
|
|
|
|
|
|
protected $updateInfo;
|
|
|
|
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
$this->updateSource = option('update_source');
|
|
|
|
|
|
|
|
$this->currentVersion = config('app.version');
|
|
|
|
}
|
|
|
|
|
|
|
|
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' => '',
|
2016-11-25 12:54:20 +08:00
|
|
|
'new_version_available' => false
|
|
|
|
];
|
|
|
|
|
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');
|
|
|
|
|
2017-01-02 16:23:31 +08:00
|
|
|
$info['new_version_available'] = Utils::versionCompare(
|
2016-11-25 12:54:20 +08:00
|
|
|
$info['latest_version'],
|
|
|
|
$info['current_version'], '>'
|
|
|
|
);
|
|
|
|
|
|
|
|
if ($detail = $this->getReleaseInfo($info['latest_version'])) {
|
|
|
|
$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']) {
|
2016-12-30 20:37:27 +08:00
|
|
|
$info['release_time'] = Arr::get($this->getReleaseInfo($this->currentVersion), 'release_time');
|
|
|
|
}
|
2016-11-25 12:54:20 +08:00
|
|
|
}
|
|
|
|
|
2017-01-01 12:22:18 +08:00
|
|
|
$update = Option::form('update', OptionForm::AUTO_DETECT, function($form)
|
2016-12-30 20:37:27 +08:00
|
|
|
{
|
2017-01-01 12:22:18 +08:00
|
|
|
$form->checkbox('check_update', OptionForm::AUTO_DETECT)->label(OptionForm::AUTO_DETECT);
|
|
|
|
$form->text('update_source', OptionForm::AUTO_DETECT)
|
|
|
|
->description(OptionForm::AUTO_DETECT);
|
2016-12-30 20:37:27 +08:00
|
|
|
})->handle()->always(function($form) {
|
|
|
|
try {
|
|
|
|
$response = file_get_contents(option('update_source'));
|
|
|
|
} catch (\Exception $e) {
|
2017-01-01 12:22:18 +08:00
|
|
|
$form->addMessage(trans('admin.update.errors.connection').$e->getMessage(), 'danger');
|
2016-12-30 20:37:27 +08:00
|
|
|
}
|
|
|
|
});
|
2016-11-25 12:54:20 +08:00
|
|
|
|
2016-12-30 20:37:27 +08:00
|
|
|
return view('admin.update')->with('info', $info)->with('update', $update);
|
2016-11-25 12:54:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public function checkUpdates()
|
|
|
|
{
|
|
|
|
return json([
|
|
|
|
'latest' => $this->getUpdateInfo('latest_version'),
|
|
|
|
'available' => $this->newVersionAvailable()
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function newVersionAvailable()
|
|
|
|
{
|
|
|
|
$latest = $this->getUpdateInfo('latest_version');
|
|
|
|
|
2017-01-02 16:23:31 +08:00
|
|
|
return Utils::versionCompare($latest, $this->currentVersion, '>') && $this->getReleaseInfo($latest);
|
2016-11-25 12:54:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public function download(Request $request)
|
|
|
|
{
|
|
|
|
$action = $request->input('action');
|
|
|
|
|
2018-02-16 17:31:04 +08:00
|
|
|
if (! $this->newVersionAvailable()) return;
|
2016-11-25 12:54:20 +08:00
|
|
|
|
|
|
|
$release_url = $this->getReleaseInfo($this->latestVersion)['release_url'];
|
|
|
|
$file_size = Utils::getRemoteFileSize($release_url);
|
|
|
|
$tmp_path = session('tmp_path');
|
|
|
|
|
|
|
|
switch ($action) {
|
|
|
|
case 'prepare-download':
|
|
|
|
|
|
|
|
$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')) {
|
2017-11-18 20:36:31 +08:00
|
|
|
return response(trans('admin.update.errors.write-permission'));
|
2016-11-25 12:54:20 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$tmp_path = $update_cache."/update_".time().".zip";
|
|
|
|
|
|
|
|
session(['tmp_path' => $tmp_path]);
|
|
|
|
|
|
|
|
return json(compact('release_url', 'tmp_path', 'file_size'));
|
|
|
|
|
|
|
|
case 'start-download':
|
|
|
|
|
2018-02-16 17:31:04 +08:00
|
|
|
if (! session()->has('tmp_path')) {
|
|
|
|
return "No temp path is set.";
|
|
|
|
}
|
2016-11-25 12:54:20 +08:00
|
|
|
|
|
|
|
try {
|
|
|
|
Utils::download($release_url, $tmp_path);
|
|
|
|
|
|
|
|
} catch (\Exception $e) {
|
2017-08-05 15:03:58 +08:00
|
|
|
File::delete($tmp_path);
|
2016-11-25 12:54:20 +08:00
|
|
|
|
2017-11-18 20:36:31 +08:00
|
|
|
return response(trans('admin.update.errors.prefix').$e->getMessage());
|
2016-11-25 12:54:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return json(compact('tmp_path'));
|
|
|
|
|
|
|
|
case 'get-file-size':
|
|
|
|
|
2018-02-16 17:31:04 +08:00
|
|
|
if (! session()->has('tmp_path')) {
|
|
|
|
return "No temp path is set.";
|
|
|
|
}
|
2016-11-25 12:54:20 +08:00
|
|
|
|
|
|
|
if (file_exists($tmp_path)) {
|
|
|
|
return json(['size' => filesize($tmp_path)]);
|
|
|
|
}
|
|
|
|
|
|
|
|
case 'extract':
|
2016-11-25 13:13:26 +08:00
|
|
|
|
2018-02-16 17:31:04 +08:00
|
|
|
if (! file_exists($tmp_path)) {
|
2017-11-18 20:36:31 +08:00
|
|
|
return response('No file available');
|
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) {
|
|
|
|
Log::info("[ZipArchive] Extracting file $tmp_path");
|
|
|
|
|
2017-11-18 20:36:31 +08:00
|
|
|
if ($zip->extractTo($extract_dir) === false) {
|
|
|
|
return response(trans('admin.update.errors.prefix').'Cannot unzip file.');
|
2016-11-25 13:13:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
2017-11-18 20:36:31 +08:00
|
|
|
return response(trans('admin.update.errors.unzip').$res);
|
2016-11-25 13:13:26 +08:00
|
|
|
}
|
|
|
|
$zip->close();
|
|
|
|
|
2017-08-05 16:00:14 +08:00
|
|
|
try {
|
|
|
|
File::copyDirectory("$extract_dir/vendor", base_path('vendor'));
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
Log::error('[Extracter] Unable to extract vendors', [$e]);
|
|
|
|
// 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
|
|
|
|
2017-08-05 15:03:58 +08:00
|
|
|
Log::info("[Extracter] Covering files");
|
2016-11-25 13:13:26 +08:00
|
|
|
|
2017-08-05 15:03:58 +08:00
|
|
|
} catch (\Exception $e) {
|
2017-08-05 15:28:12 +08:00
|
|
|
Log::error("[Extracter] Error occured when covering files", [$e]);
|
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
|
|
|
|
return response(trans('admin.update.errors.overwrite').$e->getMessage());
|
|
|
|
} finally {
|
2017-08-05 15:03:58 +08:00
|
|
|
File::deleteDirectory(storage_path('update_cache'));
|
2017-11-18 20:36:31 +08:00
|
|
|
Log::info("[Extracter] Cleaning cache");
|
2016-11-25 13:13:26 +08:00
|
|
|
}
|
|
|
|
|
2017-01-01 12:22:18 +08:00
|
|
|
return json(trans('admin.update.complete'), 0);
|
2016-11-25 13:13:26 +08:00
|
|
|
|
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')
|
|
|
|
? $this->updateSource."?v=".substr(time(), 0, -3)
|
|
|
|
: $this->updateSource;
|
2016-11-25 12:54:20 +08:00
|
|
|
|
|
|
|
try {
|
|
|
|
$response = file_get_contents($url);
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
Log::error("[CheckingUpdate] Failed to get update information: ".$e->getMessage());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($response)) {
|
|
|
|
$this->updateInfo = json_decode($response, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->latestVersion = Arr::get($this->updateInfo, 'latest_version', $this->currentVersion);
|
|
|
|
|
2018-02-16 17:31:04 +08:00
|
|
|
if (! is_null($key)) {
|
2016-11-25 12:54:20 +08:00
|
|
|
return Arr::get($this->updateInfo, $key);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->updateInfo;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function getReleaseInfo($version)
|
|
|
|
{
|
|
|
|
return Arr::get($this->getUpdateInfo('releases'), $version);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|