added module:download command

This commit is contained in:
Denis Duliçi
2020-12-24 02:16:00 +03:00
parent 5b0a50c758
commit 531dcd8dab
16 changed files with 793 additions and 515 deletions

View File

@ -294,268 +294,6 @@ trait Modules
return $response->json();
}
public function downloadModule($path)
{
if (empty($path)) {
return [
'success' => false,
'error' => true,
'message' => trans('modules.errors.download', ['module' => '']),
'data' => null,
];
}
if (!$response = static::getResponse('GET', $path)) {
return [
'success' => false,
'error' => true,
'message' => trans('modules.errors.download', ['module' => '']),
'data' => null,
];
}
$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,
'error' => true,
'message' => trans('modules.errors.download', ['module' => '']),
'data' => null,
];
}
return [
'success' => true,
'error' => false,
'message' => null,
'data' => [
'path' => $path,
],
];
}
public function unzipModule($path)
{
if (empty($path)) {
return [
'success' => false,
'error' => true,
'message' => trans('modules.errors.unzip', ['module' => '']),
'data' => null,
];
}
$temp_path = storage_path('app/temp/' . $path);
$file = $temp_path . '/upload.zip';
// Unzip the file
$zip = new ZipArchive();
if (!$zip->open($file) || !$zip->extractTo($temp_path)) {
return [
'success' => false,
'error' => true,
'message' => trans('modules.errors.unzip', ['module' => '']),
'data' => null,
];
}
$zip->close();
// Remove Zip
File::delete($file);
return [
'success' => true,
'error' => false,
'message' => null,
'data' => [
'path' => $path,
],
];
}
public function copyModule($path)
{
$temp_path = storage_path('app/temp/' . $path);
if (empty($path) || !is_file($temp_path . '/module.json')) {
return [
'success' => false,
'error' => true,
'message' => trans('modules.errors.finish', ['module' => '']),
'data' => null,
];
}
$modules_path = config('module.paths.modules');
// Create modules directory
if (!File::isDirectory($modules_path)) {
File::makeDirectory($modules_path);
}
$module = json_decode(file_get_contents($temp_path . '/module.json'));
$module_path = $modules_path . '/' . Str::studly($module->alias);
// Create module directory
if (!File::isDirectory($module_path)) {
File::makeDirectory($module_path);
}
// Move all files/folders from temp path then delete it
File::copyDirectory($temp_path, $module_path);
File::deleteDirectory($temp_path);
event(new \App\Events\Module\Copied($module->alias, session('company_id')));
return [
'success' => true,
'error' => false,
'message' => null,
'data' => [
'path' => $path,
'alias' => $module->alias,
],
];
}
public function installModule($alias)
{
$company_id = session('company_id');
$locale = app()->getLocale();
$command = "module:install {$alias} {$company_id} {$locale}";
if (true !== $result = Console::run($command)) {
$message = !empty($result) ? $result : trans('modules.errors.finish', ['module' => $alias]);
return [
'success' => false,
'error' => true,
'message' => $message,
'data' => null,
];
}
return [
'success' => true,
'redirect' => route('apps.app.show', $alias),
'error' => false,
'message' => null,
'data' => [
'path' => '',
'name' => module($alias)->getName(),
'alias' => $alias,
],
];
}
public function uninstallModule($alias)
{
$module = module($alias);
$name = $module->getName();
$version = $module->get('version');
$company_id = session('company_id');
$locale = app()->getLocale();
$command = "module:uninstall {$alias} {$company_id} {$locale}";
if (true !== $result = Console::run($command)) {
return [
'success' => false,
'error' => true,
'message' => $result,
'data' => null,
];
}
return [
'success' => true,
'error' => false,
'message' => null,
'data' => [
'name' => $name,
'version' => $version,
],
];
}
public function enableModule($alias)
{
$module = module($alias);
$company_id = session('company_id');
$locale = app()->getLocale();
$command = "module:enable {$alias} {$company_id} {$locale}";
if (true !== $result = Console::run($command)) {
return [
'success' => false,
'error' => true,
'message' => $result,
'data' => null,
];
}
return [
'success' => true,
'error' => false,
'message' => null,
'data' => [
'name' => $module->getName(),
'version' => $module->get('version'),
],
];
}
public function disableModule($alias)
{
$module = module($alias);
$company_id = session('company_id');
$locale = app()->getLocale();
$command = "module:disable {$alias} {$company_id} {$locale}";
if (true !== $result = Console::run($command)) {
return [
'success' => false,
'error' => true,
'message' => $result,
'data' => null,
];
}
return [
'success' => true,
'error' => false,
'message' => null,
'data' => [
'name' => $module->getName(),
'version' => $module->get('version'),
],
];
}
public function moduleExists($alias)
{
if (!module($alias) instanceof \Akaunting\Module\Module) {