Akaunting and module update function re-factor

This commit is contained in:
cuneytsenturk
2019-02-06 11:34:13 +03:00
parent 260a823764
commit 2455175293
2 changed files with 140 additions and 106 deletions

View File

@ -2,6 +2,7 @@
namespace App\Utilities;
use App\Events\UpdateFinished;
use App\Models\Module\Module as Model;
use App\Models\Module\ModuleHistory as ModelHistory;
use App\Traits\SiteApi;
@ -92,9 +93,10 @@ class Updater
return true;
}
public static function download($alias, $version)
public static function download($name, $alias, $version)
{
$file = null;
$path = null;
// Check core first
$info = Info::all();
@ -109,23 +111,78 @@ class Updater
// Exception
if ($response instanceof RequestException) {
return false;
return [
'success' => false,
'errors' => trans('modules.errors.download', ['module' => $name]),
'data' => [
'path' => $path
]
];
}
if ($response && ($response->getStatusCode() == 200)) {
$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
$zip = new ZipArchive();
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();
@ -133,23 +190,50 @@ class Updater
// Delete zip 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') {
// Move all files/folders from temp 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 {
// Get module instance
$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
if (!File::copyDirectory($temp_path, module_path($module->get('name')))) {
return false;
if (!File::copyDirectory($temp_path, $module_path)) {
return [
'success' => false,
'errors' => trans('modules.errors.file_copy', ['module' => $name]),
'data' => [
'path' => $path
]
];
}
$model = Model::where('alias', $alias)->first();
@ -169,7 +253,46 @@ class Updater
// Delete temp directory
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()