mirror of
https://github.com/crater-invoice/crater.git
synced 2026-02-04 09:41:44 -05:00
add auto update feature
This commit is contained in:
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' => []
|
||||
];
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user