mirror of
https://github.com/crater-invoice/crater.git
synced 2025-10-28 12:11:08 -04:00
add auto update feature
This commit is contained in:
34
app/Events/UpdateFinished.php
Normal file
34
app/Events/UpdateFinished.php
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Laraspace\Events;
|
||||||
|
|
||||||
|
use Illuminate\Broadcasting\Channel;
|
||||||
|
use Illuminate\Broadcasting\InteractsWithSockets;
|
||||||
|
use Illuminate\Broadcasting\PresenceChannel;
|
||||||
|
use Illuminate\Broadcasting\PrivateChannel;
|
||||||
|
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
|
||||||
|
use Illuminate\Foundation\Events\Dispatchable;
|
||||||
|
use Illuminate\Queue\SerializesModels;
|
||||||
|
|
||||||
|
class UpdateFinished
|
||||||
|
{
|
||||||
|
use Dispatchable, InteractsWithSockets, SerializesModels;
|
||||||
|
|
||||||
|
public $alias;
|
||||||
|
|
||||||
|
public $new;
|
||||||
|
|
||||||
|
public $old;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new event instance.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function __construct($alias, $old, $new)
|
||||||
|
{
|
||||||
|
$this->alias = $alias;
|
||||||
|
$this->old = $old;
|
||||||
|
$this->new = $new;
|
||||||
|
}
|
||||||
|
}
|
||||||
18
app/Http/Controllers/UpdateController.php
Normal file
18
app/Http/Controllers/UpdateController.php
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Laraspace\Http\Controllers;
|
||||||
|
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
use Laraspace\Space\Updater;
|
||||||
|
|
||||||
|
class UpdateController extends Controller
|
||||||
|
{
|
||||||
|
public function update(Request $request)
|
||||||
|
{
|
||||||
|
set_time_limit(600); // 10 minutes
|
||||||
|
|
||||||
|
$json = Updater::update($request->alias, $request->installed, $request->version);
|
||||||
|
|
||||||
|
return response()->json($json);
|
||||||
|
}
|
||||||
|
}
|
||||||
31
app/Listeners/Updates/Listener.php
Normal file
31
app/Listeners/Updates/Listener.php
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Laraspace\Listeners\Updates;
|
||||||
|
|
||||||
|
class Listener
|
||||||
|
{
|
||||||
|
const ALIAS = '';
|
||||||
|
|
||||||
|
const VERSION = '';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if should listen.
|
||||||
|
*
|
||||||
|
* @param $event
|
||||||
|
* @return boolean
|
||||||
|
*/
|
||||||
|
protected function check($event)
|
||||||
|
{
|
||||||
|
// Apply only to the specified alias
|
||||||
|
if ($event->alias != static::ALIAS) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Do not apply to the same or newer versions
|
||||||
|
if (version_compare($event->old, static::VERSION, '>=')) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
28
app/Listeners/Updates/V10/Version101.php
Normal file
28
app/Listeners/Updates/V10/Version101.php
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Laraspace\Listeners\Updates\V10;
|
||||||
|
|
||||||
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||||
|
use Illuminate\Queue\InteractsWithQueue;
|
||||||
|
use Laraspace\Listeners\Updates\Listener;
|
||||||
|
use Laraspace\Events\UpdateFinished;
|
||||||
|
|
||||||
|
class Version101 extends Listener
|
||||||
|
{
|
||||||
|
const ALIAS = 'core';
|
||||||
|
|
||||||
|
const VERSION = '1.0.1';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handle the event.
|
||||||
|
*
|
||||||
|
* @param object $event
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function handle(UpdateFinished $event)
|
||||||
|
{
|
||||||
|
if (!$this->check($event)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -13,8 +13,8 @@ class EventServiceProvider extends ServiceProvider
|
|||||||
* @var array
|
* @var array
|
||||||
*/
|
*/
|
||||||
protected $listen = [
|
protected $listen = [
|
||||||
'Laraspace\Events\SomeEvent' => [
|
'Laraspace\Events\UpdateFinished' => [
|
||||||
'Laraspace\Listeners\EventListener',
|
'Laraspace\Listeners\Updates\V10\Version101',
|
||||||
],
|
],
|
||||||
Registered::class => [
|
Registered::class => [
|
||||||
SendEmailVerificationNotification::class,
|
SendEmailVerificationNotification::class,
|
||||||
|
|||||||
35
app/Space/SiteApi.php
Normal file
35
app/Space/SiteApi.php
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Laraspace\Space;
|
||||||
|
|
||||||
|
use GuzzleHttp\Client;
|
||||||
|
use GuzzleHttp\Exception\RequestException;
|
||||||
|
|
||||||
|
trait SiteApi
|
||||||
|
{
|
||||||
|
|
||||||
|
protected static function getRemote($url, $data = array())
|
||||||
|
{
|
||||||
|
$base = 'https://codeload.github.com/';
|
||||||
|
|
||||||
|
$client = new Client(['verify' => false, 'base_uri' => $base]);
|
||||||
|
|
||||||
|
$headers['headers'] = array(
|
||||||
|
'Accept' => 'application/json',
|
||||||
|
'Referer' => url('/'),
|
||||||
|
'crater' => getFullVersion()
|
||||||
|
);
|
||||||
|
|
||||||
|
$data['http_errors'] = false;
|
||||||
|
|
||||||
|
$data = array_merge($data, $headers);
|
||||||
|
|
||||||
|
try {
|
||||||
|
$result = $client->get($url, $data);
|
||||||
|
} catch (RequestException $e) {
|
||||||
|
$result = $e;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $result;
|
||||||
|
}
|
||||||
|
}
|
||||||
97
app/Space/Updater.php
Normal file
97
app/Space/Updater.php
Normal file
@ -0,0 +1,97 @@
|
|||||||
|
<?php
|
||||||
|
namespace Laraspace\Space;
|
||||||
|
|
||||||
|
use File;
|
||||||
|
use ZipArchive;
|
||||||
|
use Artisan;
|
||||||
|
use GuzzleHttp\Exception\RequestException;
|
||||||
|
use Laraspace\Space\SiteApi;
|
||||||
|
use Laraspace\Events\UpdateFinished;
|
||||||
|
|
||||||
|
class Updater
|
||||||
|
{
|
||||||
|
use SiteApi;
|
||||||
|
|
||||||
|
public static function update($alias, $installed, $version)
|
||||||
|
{
|
||||||
|
$data = null;
|
||||||
|
$path = null;
|
||||||
|
|
||||||
|
$url = 'laraspace/laraspace/zip/master';
|
||||||
|
|
||||||
|
$response = static::getRemote($url, ['timeout' => 50, 'track_redirects' => true]);
|
||||||
|
|
||||||
|
// Exception
|
||||||
|
if ($response instanceof RequestException) {
|
||||||
|
return [
|
||||||
|
'success' => false,
|
||||||
|
'errors' => 'Download Exception',
|
||||||
|
'data' => [
|
||||||
|
'path' => $path
|
||||||
|
]
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($response && ($response->getStatusCode() == 200)) {
|
||||||
|
$data = $response->getBody()->getContents();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create temp directory
|
||||||
|
$path = 'temp-' . md5(mt_rand());
|
||||||
|
$path2 = 'temp2-' . md5(mt_rand());
|
||||||
|
$temp_path = storage_path('app/temp') . '/' . $path;
|
||||||
|
$temp_path2 = storage_path('app/temp') . '/' . $path2;
|
||||||
|
|
||||||
|
if (!File::isDirectory($temp_path)) {
|
||||||
|
File::makeDirectory($temp_path);
|
||||||
|
File::makeDirectory($temp_path2);
|
||||||
|
}
|
||||||
|
|
||||||
|
$file = $temp_path . '/upload.zip';
|
||||||
|
|
||||||
|
// Add content to the Zip file
|
||||||
|
$uploaded = is_int(file_put_contents($file, $data)) ? true : false;
|
||||||
|
|
||||||
|
if (!$uploaded) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Unzip the file
|
||||||
|
$zip = new ZipArchive();
|
||||||
|
|
||||||
|
if ($zip->open($file)) {
|
||||||
|
$zip->extractTo($temp_path2);
|
||||||
|
}
|
||||||
|
|
||||||
|
$zip->close();
|
||||||
|
|
||||||
|
// Delete zip file
|
||||||
|
File::delete($file);
|
||||||
|
|
||||||
|
if (!File::copyDirectory($temp_path2, base_path())) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Delete temp directory
|
||||||
|
File::deleteDirectory($temp_path);
|
||||||
|
File::deleteDirectory($temp_path2);
|
||||||
|
|
||||||
|
Artisan::call('cache:clear');
|
||||||
|
|
||||||
|
try {
|
||||||
|
event(new UpdateFinished($alias, $installed, $version));
|
||||||
|
|
||||||
|
return [
|
||||||
|
'success' => true,
|
||||||
|
'errors' => false,
|
||||||
|
'data' => []
|
||||||
|
];
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
return [
|
||||||
|
'success' => false,
|
||||||
|
'errors' => 'Update error',
|
||||||
|
'data' => []
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -106,6 +106,8 @@ Route::group(['middleware' => 'redirect-if-installed'], function () {
|
|||||||
'uses' => 'OnboardingController@companySettings'
|
'uses' => 'OnboardingController@companySettings'
|
||||||
]);
|
]);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
// App version
|
// App version
|
||||||
// ----------------------------------
|
// ----------------------------------
|
||||||
|
|
||||||
@ -120,6 +122,13 @@ Route::group(['middleware' => 'api'], function () {
|
|||||||
'middleware' => 'admin'
|
'middleware' => 'admin'
|
||||||
], function () {
|
], function () {
|
||||||
|
|
||||||
|
// Auto update routes
|
||||||
|
//----------------------------------
|
||||||
|
Route::post('/update', [
|
||||||
|
'as' => 'auto.update',
|
||||||
|
'uses' => 'UpdateController@update'
|
||||||
|
]);
|
||||||
|
|
||||||
Route::get('/bootstrap', [
|
Route::get('/bootstrap', [
|
||||||
'as' => 'bootstrap',
|
'as' => 'bootstrap',
|
||||||
'uses' => 'UsersController@getBootstrap'
|
'uses' => 'UsersController@getBootstrap'
|
||||||
|
|||||||
Reference in New Issue
Block a user