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

@ -3,13 +3,10 @@
namespace App\Http\Controllers\Install;
use App\Http\Controllers\Controller;
use App\Events\UpdateFinished;
use App\Utilities\Updater;
use App\Utilities\Versions;
use Illuminate\Http\Request;
use Artisan;
use Module;
use File;
class Updates extends Controller
{
@ -157,45 +154,11 @@ class Updates extends Controller
{
set_time_limit(600); // 10 minutes
$status = true;
if ($request['alias'] != 'core') {
$this->checkApiToken();
}
// Download file
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
]
];
$json = Updater::download($request['name'], $request['alias'], $request['version']);
return response()->json($json);
}
@ -215,19 +178,7 @@ class Updates extends Controller
$this->checkApiToken();
}
$path = storage_path('app/temp') . '/' . $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']
]
];
$json = Updater::unzip($request['name'], $request['path']);
return response()->json($json);
}
@ -247,17 +198,7 @@ class Updates extends Controller
$this->checkApiToken();
}
$path = storage_path('app/temp') . '/' . $request['path'];
$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']
]
];
$json = Updater::fileCopy($request['name'], $request['alias'], $request['path'], $request['version']);
return response()->json($json);
}
@ -271,35 +212,7 @@ class Updates extends Controller
*/
public function migrate(Request $request)
{
// Check if the file mirror was successful
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' => []
];
}
$json = Updater::migrate($request['name'], $request['alias'], $request['version'], $request['installed']);
return response()->json($json);
}
@ -313,13 +226,11 @@ class Updates extends Controller
*/
public function finish(Request $request)
{
$json = [
return response()->json([
'success' => true,
'errors' => false,
'redirect' => url("install/updates"),
'data' => [],
];
return response()->json($json);
]);
}
}

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()