Akaunting and module update function re-factor
This commit is contained in:
parent
260a823764
commit
2455175293
@ -3,13 +3,10 @@
|
|||||||
namespace App\Http\Controllers\Install;
|
namespace App\Http\Controllers\Install;
|
||||||
|
|
||||||
use App\Http\Controllers\Controller;
|
use App\Http\Controllers\Controller;
|
||||||
use App\Events\UpdateFinished;
|
|
||||||
use App\Utilities\Updater;
|
use App\Utilities\Updater;
|
||||||
use App\Utilities\Versions;
|
use App\Utilities\Versions;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
use Artisan;
|
|
||||||
use Module;
|
use Module;
|
||||||
use File;
|
|
||||||
|
|
||||||
class Updates extends Controller
|
class Updates extends Controller
|
||||||
{
|
{
|
||||||
@ -157,45 +154,11 @@ class Updates extends Controller
|
|||||||
{
|
{
|
||||||
set_time_limit(600); // 10 minutes
|
set_time_limit(600); // 10 minutes
|
||||||
|
|
||||||
$status = true;
|
|
||||||
|
|
||||||
if ($request['alias'] != 'core') {
|
if ($request['alias'] != 'core') {
|
||||||
$this->checkApiToken();
|
$this->checkApiToken();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Download file
|
$json = Updater::download($request['name'], $request['alias'], $request['version']);
|
||||||
if (!$data = Updater::download($request['alias'], $request['version'])) {
|
|
||||||
$status = false;
|
|
||||||
|
|
||||||
$message = trans('modules.errors.download', ['module' => $request['name']]);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create temp directory
|
|
||||||
$path = 'temp-' . md5(mt_rand());
|
|
||||||
$temp_path = storage_path('app/temp') . '/' . $path;
|
|
||||||
|
|
||||||
if (!File::isDirectory($temp_path)) {
|
|
||||||
File::makeDirectory($temp_path);
|
|
||||||
}
|
|
||||||
|
|
||||||
$file = $temp_path . '/upload.zip';
|
|
||||||
|
|
||||||
// Add content to the Zip file
|
|
||||||
$uploaded = is_int(file_put_contents($file, $data)) ? true : false;
|
|
||||||
|
|
||||||
if (!$uploaded) {
|
|
||||||
$status = false;
|
|
||||||
|
|
||||||
$message = trans('modules.errors.upload', ['module' => $request['name']]);
|
|
||||||
}
|
|
||||||
|
|
||||||
$json = [
|
|
||||||
'success' => ($status) ? true : false,
|
|
||||||
'errors' => (!$status) ? $message : false,
|
|
||||||
'data' => [
|
|
||||||
'path' => $path
|
|
||||||
]
|
|
||||||
];
|
|
||||||
|
|
||||||
return response()->json($json);
|
return response()->json($json);
|
||||||
}
|
}
|
||||||
@ -215,19 +178,7 @@ class Updates extends Controller
|
|||||||
$this->checkApiToken();
|
$this->checkApiToken();
|
||||||
}
|
}
|
||||||
|
|
||||||
$path = storage_path('app/temp') . '/' . $request['path'];
|
$json = Updater::unzip($request['name'], $request['path']);
|
||||||
|
|
||||||
$file = $path . '/upload.zip';
|
|
||||||
|
|
||||||
$result = Updater::unzip($file, $path);
|
|
||||||
|
|
||||||
$json = [
|
|
||||||
'success' => ($result) ? true : false,
|
|
||||||
'errors' => (!$result) ? trans('modules.errors.unzip', ['module' => $request['name']]) : false,
|
|
||||||
'data' => [
|
|
||||||
'path' => $request['path']
|
|
||||||
]
|
|
||||||
];
|
|
||||||
|
|
||||||
return response()->json($json);
|
return response()->json($json);
|
||||||
}
|
}
|
||||||
@ -247,17 +198,7 @@ class Updates extends Controller
|
|||||||
$this->checkApiToken();
|
$this->checkApiToken();
|
||||||
}
|
}
|
||||||
|
|
||||||
$path = storage_path('app/temp') . '/' . $request['path'];
|
$json = Updater::fileCopy($request['name'], $request['alias'], $request['path'], $request['version']);
|
||||||
|
|
||||||
$result = Updater::fileCopy($request['alias'], $path, $request['version']);
|
|
||||||
|
|
||||||
$json = [
|
|
||||||
'success' => ($result) ? true : false,
|
|
||||||
'errors' => (!$result) ? trans('modules.errors.file_copy', ['module' => $request['name']]) : false,
|
|
||||||
'data' => [
|
|
||||||
'path' => $request['path']
|
|
||||||
]
|
|
||||||
];
|
|
||||||
|
|
||||||
return response()->json($json);
|
return response()->json($json);
|
||||||
}
|
}
|
||||||
@ -271,35 +212,7 @@ class Updates extends Controller
|
|||||||
*/
|
*/
|
||||||
public function migrate(Request $request)
|
public function migrate(Request $request)
|
||||||
{
|
{
|
||||||
// Check if the file mirror was successful
|
$json = Updater::migrate($request['name'], $request['alias'], $request['version'], $request['installed']);
|
||||||
if (($request['alias'] == 'core') && (version('short') != $request['version'])) {
|
|
||||||
$json = [
|
|
||||||
'success' => false,
|
|
||||||
'errors' => trans('modules.errors.migrate core', ['module' => $request['name']]),
|
|
||||||
'data' => []
|
|
||||||
];
|
|
||||||
|
|
||||||
return response()->json($json);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Clear cache after update
|
|
||||||
Artisan::call('cache:clear');
|
|
||||||
|
|
||||||
try {
|
|
||||||
event(new UpdateFinished($request['alias'], $request['installed'], $request['version']));
|
|
||||||
|
|
||||||
$json = [
|
|
||||||
'success' => true,
|
|
||||||
'errors' => false,
|
|
||||||
'data' => []
|
|
||||||
];
|
|
||||||
} catch (\Exception $e) {
|
|
||||||
$json = [
|
|
||||||
'success' => false,
|
|
||||||
'errors' => trans('modules.errors.migrate', ['module' => $request['name']]),
|
|
||||||
'data' => []
|
|
||||||
];
|
|
||||||
}
|
|
||||||
|
|
||||||
return response()->json($json);
|
return response()->json($json);
|
||||||
}
|
}
|
||||||
@ -313,13 +226,11 @@ class Updates extends Controller
|
|||||||
*/
|
*/
|
||||||
public function finish(Request $request)
|
public function finish(Request $request)
|
||||||
{
|
{
|
||||||
$json = [
|
return response()->json([
|
||||||
'success' => true,
|
'success' => true,
|
||||||
'errors' => false,
|
'errors' => false,
|
||||||
'redirect' => url("install/updates"),
|
'redirect' => url("install/updates"),
|
||||||
'data' => [],
|
'data' => [],
|
||||||
];
|
]);
|
||||||
|
|
||||||
return response()->json($json);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
namespace App\Utilities;
|
namespace App\Utilities;
|
||||||
|
|
||||||
|
use App\Events\UpdateFinished;
|
||||||
use App\Models\Module\Module as Model;
|
use App\Models\Module\Module as Model;
|
||||||
use App\Models\Module\ModuleHistory as ModelHistory;
|
use App\Models\Module\ModuleHistory as ModelHistory;
|
||||||
use App\Traits\SiteApi;
|
use App\Traits\SiteApi;
|
||||||
@ -92,9 +93,10 @@ class Updater
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function download($alias, $version)
|
public static function download($name, $alias, $version)
|
||||||
{
|
{
|
||||||
$file = null;
|
$file = null;
|
||||||
|
$path = null;
|
||||||
|
|
||||||
// Check core first
|
// Check core first
|
||||||
$info = Info::all();
|
$info = Info::all();
|
||||||
@ -109,23 +111,78 @@ class Updater
|
|||||||
|
|
||||||
// Exception
|
// Exception
|
||||||
if ($response instanceof RequestException) {
|
if ($response instanceof RequestException) {
|
||||||
return false;
|
return [
|
||||||
|
'success' => false,
|
||||||
|
'errors' => trans('modules.errors.download', ['module' => $name]),
|
||||||
|
'data' => [
|
||||||
|
'path' => $path
|
||||||
|
]
|
||||||
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($response && ($response->getStatusCode() == 200)) {
|
if ($response && ($response->getStatusCode() == 200)) {
|
||||||
$file = $response->getBody()->getContents();
|
$file = $response->getBody()->getContents();
|
||||||
|
|
||||||
|
$path = 'temp-' . md5(mt_rand());
|
||||||
|
$temp_path = storage_path('app/temp') . '/' . $path;
|
||||||
|
|
||||||
|
$file_path = $temp_path . '/upload.zip';
|
||||||
|
|
||||||
|
// Create tmp directory
|
||||||
|
if (!File::isDirectory($temp_path)) {
|
||||||
|
File::makeDirectory($temp_path);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add content to the Zip file
|
||||||
|
$uploaded = is_int(file_put_contents($file_path, $file)) ? true : false;
|
||||||
|
|
||||||
|
if (!$uploaded) {
|
||||||
|
return [
|
||||||
|
'success' => false,
|
||||||
|
'errors' => trans('modules.errors.upload', ['module' => $name]),
|
||||||
|
'data' => [
|
||||||
|
'path' => $path
|
||||||
|
]
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
$data = [
|
||||||
|
'path' => $path
|
||||||
|
];
|
||||||
|
|
||||||
|
return [
|
||||||
|
'success' => true,
|
||||||
|
'errors' => false,
|
||||||
|
'data' => $data,
|
||||||
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
return $file;
|
return [
|
||||||
|
'success' => false,
|
||||||
|
'errors' => trans('modules.errors.download', ['module' => $name]),
|
||||||
|
'data' => [
|
||||||
|
'path' => $path
|
||||||
|
]
|
||||||
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function unzip($file, $temp_path)
|
public static function unzip($name, $path)
|
||||||
{
|
{
|
||||||
|
$temp_path = storage_path('app/temp') . '/' . $path;
|
||||||
|
|
||||||
|
$file = $temp_path . '/upload.zip';
|
||||||
|
|
||||||
// Unzip the file
|
// Unzip the file
|
||||||
$zip = new ZipArchive();
|
$zip = new ZipArchive();
|
||||||
|
|
||||||
if (($zip->open($file) !== true) || !$zip->extractTo($temp_path)) {
|
if (($zip->open($file) !== true) || !$zip->extractTo($temp_path)) {
|
||||||
return false;
|
return [
|
||||||
|
'success' => false,
|
||||||
|
'errors' => trans('modules.errors.unzip', ['module' => $name]),
|
||||||
|
'data' => [
|
||||||
|
'path' => $path
|
||||||
|
]
|
||||||
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
$zip->close();
|
$zip->close();
|
||||||
@ -133,23 +190,50 @@ class Updater
|
|||||||
// Delete zip file
|
// Delete zip file
|
||||||
File::delete($file);
|
File::delete($file);
|
||||||
|
|
||||||
return true;
|
return [
|
||||||
|
'success' => true,
|
||||||
|
'errors' => false,
|
||||||
|
'data' => [
|
||||||
|
'path' => $path
|
||||||
|
]
|
||||||
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function fileCopy($alias, $temp_path, $version)
|
public static function fileCopy($name, $alias, $path, $version)
|
||||||
{
|
{
|
||||||
|
$temp_path = storage_path('app/temp') . '/' . $path;
|
||||||
|
|
||||||
if ($alias == 'core') {
|
if ($alias == 'core') {
|
||||||
// Move all files/folders from temp path
|
// Move all files/folders from temp path
|
||||||
if (!File::copyDirectory($temp_path, base_path())) {
|
if (!File::copyDirectory($temp_path, base_path())) {
|
||||||
return false;
|
return [
|
||||||
|
'success' => false,
|
||||||
|
'errors' => trans('modules.errors.file_copy', ['module' => $name]),
|
||||||
|
'data' => [
|
||||||
|
'path' => $path
|
||||||
|
]
|
||||||
|
];
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// Get module instance
|
// Get module instance
|
||||||
$module = Module::findByAlias($alias);
|
$module = Module::findByAlias($alias);
|
||||||
|
|
||||||
|
$module_path = module_path($module->get('name'));
|
||||||
|
|
||||||
|
// Create module directory
|
||||||
|
if (!File::isDirectory($module_path)) {
|
||||||
|
File::makeDirectory($module_path);
|
||||||
|
}
|
||||||
|
|
||||||
// Move all files/folders from temp path
|
// Move all files/folders from temp path
|
||||||
if (!File::copyDirectory($temp_path, module_path($module->get('name')))) {
|
if (!File::copyDirectory($temp_path, $module_path)) {
|
||||||
return false;
|
return [
|
||||||
|
'success' => false,
|
||||||
|
'errors' => trans('modules.errors.file_copy', ['module' => $name]),
|
||||||
|
'data' => [
|
||||||
|
'path' => $path
|
||||||
|
]
|
||||||
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
$model = Model::where('alias', $alias)->first();
|
$model = Model::where('alias', $alias)->first();
|
||||||
@ -169,7 +253,46 @@ class Updater
|
|||||||
// Delete temp directory
|
// Delete temp directory
|
||||||
File::deleteDirectory($temp_path);
|
File::deleteDirectory($temp_path);
|
||||||
|
|
||||||
return true;
|
Artisan::call('cache:clear');
|
||||||
|
|
||||||
|
return [
|
||||||
|
'success' => true,
|
||||||
|
'errors' => false,
|
||||||
|
'data' => [
|
||||||
|
'path' => $path
|
||||||
|
]
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function migrate($name, $alias, $version, $installed)
|
||||||
|
{
|
||||||
|
// Check if the file mirror was successful
|
||||||
|
if (($alias == 'core') && (version('short') != $version)) {
|
||||||
|
return [
|
||||||
|
'success' => false,
|
||||||
|
'errors' => trans('modules.errors.migrate core', ['module' => $name]),
|
||||||
|
'data' => []
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
// Clear cache after update
|
||||||
|
Artisan::call('cache:clear');
|
||||||
|
|
||||||
|
try {
|
||||||
|
event(new UpdateFinished($alias, $installed, $version));
|
||||||
|
|
||||||
|
return [
|
||||||
|
'success' => true,
|
||||||
|
'errors' => false,
|
||||||
|
'data' => []
|
||||||
|
];
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
return [
|
||||||
|
'success' => false,
|
||||||
|
'errors' => trans('modules.errors.migrate', ['module' => $name]),
|
||||||
|
'data' => []
|
||||||
|
];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function all()
|
public static function all()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user