From 531dcd8dabf62f9f6884d9a30a2a9e18e5bd8b22 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Denis=20Duli=C3=A7i?= Date: Thu, 24 Dec 2020 02:16:00 +0300 Subject: [PATCH] added module:download command --- app/Console/Commands/DownloadModule.php | 131 ++++++++++++ app/Console/Commands/Update.php | 25 ++- app/Http/Controllers/Install/Updates.php | 31 ++- app/Http/Controllers/Modules/Item.php | 165 ++++++++++---- app/Http/ViewComposers/Header.php | 16 +- app/Jobs/Install/CopyFiles.php | 77 +++++++ app/Jobs/Install/DisableModule.php | 45 ++++ app/Jobs/Install/DownloadFile.php | 75 +++++++ app/Jobs/Install/EnableModule.php | 45 ++++ app/Jobs/Install/FinishUpdate.php | 56 +++++ app/Jobs/Install/InstallModule.php | 47 ++++ app/Jobs/Install/UninstallModule.php | 45 ++++ app/Jobs/Install/UnzipFile.php | 56 +++++ app/Traits/Modules.php | 262 ----------------------- app/Utilities/Updater.php | 190 ---------------- app/Utilities/Versions.php | 42 +++- 16 files changed, 793 insertions(+), 515 deletions(-) create mode 100644 app/Console/Commands/DownloadModule.php create mode 100644 app/Jobs/Install/CopyFiles.php create mode 100644 app/Jobs/Install/DisableModule.php create mode 100644 app/Jobs/Install/DownloadFile.php create mode 100644 app/Jobs/Install/EnableModule.php create mode 100644 app/Jobs/Install/FinishUpdate.php create mode 100644 app/Jobs/Install/InstallModule.php create mode 100644 app/Jobs/Install/UninstallModule.php create mode 100644 app/Jobs/Install/UnzipFile.php delete mode 100644 app/Utilities/Updater.php diff --git a/app/Console/Commands/DownloadModule.php b/app/Console/Commands/DownloadModule.php new file mode 100644 index 000000000..463a1b6f5 --- /dev/null +++ b/app/Console/Commands/DownloadModule.php @@ -0,0 +1,131 @@ +alias = $this->argument('alias'); + $this->company = $this->argument('company'); + + session(['company_id' => $this->company]); + setting()->setExtraColumns(['company_id' => $this->company]); + + if (!$path = $this->download()) { + return self::CMD_ERROR; + } + + if (!$this->unzip($path)) { + return self::CMD_ERROR; + } + + if (!$this->copyFiles($path)) { + return self::CMD_ERROR; + } + + $this->info("Module [{$this->alias}] downloaded!"); + + return self::CMD_SUCCESS; + } + + public function download() + { + $this->info('Downloading module...'); + + try { + $path = $this->dispatch(new DownloadFile($this->alias, $this->getVersion())); + } catch (\Exception $e) { + $this->error($e->getMessage()); + + return false; + } + + return $path; + } + + public function unzip($path) + { + $this->info('Unzipping module...'); + + try { + $this->dispatch(new UnzipFile($this->alias, $path)); + } catch (\Exception $e) { + $this->error($e->getMessage()); + + return false; + } + + return true; + } + + public function copyFiles($path) + { + $this->info('Copying module files...'); + + try { + $this->dispatch(new CopyFiles($this->alias, $path)); + + event(new \App\Events\Module\Copied($this->alias, $this->company)); + } catch (\Exception $e) { + $this->error($e->getMessage()); + + return false; + } + + return true; + } + + protected function getVersion() + { + $version = Versions::latest($this->alias); + + if (empty($version)) { + $current = '1.0.0'; + + $url = 'apps/' . $this->alias . '/version/' . $current . '/' . version('short'); + + $version = Versions::getLatestVersion($url, $current); + } + + return $version; + } +} diff --git a/app/Console/Commands/Update.php b/app/Console/Commands/Update.php index dbd02ff15..63a1ebf0c 100644 --- a/app/Console/Commands/Update.php +++ b/app/Console/Commands/Update.php @@ -2,12 +2,21 @@ namespace App\Console\Commands; -use App\Utilities\Updater; +use App\Events\Install\UpdateCopied; +use App\Events\Install\UpdateDownloaded; +use App\Events\Install\UpdateUnzipped; +use App\Jobs\Install\CopyFiles; +use App\Jobs\Install\DownloadFile; +use App\Jobs\Install\FinishUpdate; +use App\Jobs\Install\UnzipFile; +use App\Traits\Jobs; use App\Utilities\Versions; use Illuminate\Console\Command; class Update extends Command { + use Jobs; + const CMD_SUCCESS = 0; const CMD_ERROR = 1; @@ -96,7 +105,9 @@ class Update extends Command $this->info('Downloading update...'); try { - $path = Updater::download($this->alias, $this->new, $this->old); + $path = $this->dispatch(new DownloadFile($this->alias, $this->new)); + + event(new UpdateDownloaded($this->alias, $this->new, $this->old)); } catch (\Exception $e) { $this->error($e->getMessage()); @@ -111,7 +122,9 @@ class Update extends Command $this->info('Unzipping update...'); try { - Updater::unzip($path, $this->alias, $this->new, $this->old); + $this->dispatch(new UnzipFile($this->alias, $path)); + + event(new UpdateUnzipped($this->alias, $this->new, $this->old)); } catch (\Exception $e) { $this->error($e->getMessage()); @@ -126,7 +139,9 @@ class Update extends Command $this->info('Copying update files...'); try { - Updater::copyFiles($path, $this->alias, $this->new, $this->old); + $this->dispatch(new CopyFiles($this->alias, $path)); + + event(new UpdateCopied($this->alias, $this->new, $this->old)); } catch (\Exception $e) { $this->error($e->getMessage()); @@ -141,7 +156,7 @@ class Update extends Command $this->info('Finishing update...'); try { - Updater::finish($this->alias, $this->new, $this->old); + $this->dispatch(new FinishUpdate($this->alias, $this->new, $this->old)); } catch (\Exception $e) { $this->error($e->getMessage()); diff --git a/app/Http/Controllers/Install/Updates.php b/app/Http/Controllers/Install/Updates.php index e4b1cabb0..69d798380 100644 --- a/app/Http/Controllers/Install/Updates.php +++ b/app/Http/Controllers/Install/Updates.php @@ -3,8 +3,16 @@ namespace App\Http\Controllers\Install; use App\Abstracts\Http\Controller; -use App\Utilities\Updater; +use App\Events\Install\UpdateCacheCleared; +use App\Events\Install\UpdateCopied; +use App\Events\Install\UpdateDownloaded; +use App\Events\Install\UpdateUnzipped; +use App\Jobs\Install\CopyFiles; +use App\Jobs\Install\DownloadFile; +use App\Jobs\Install\FinishUpdate; +use App\Jobs\Install\UnzipFile; use App\Utilities\Versions; +use Illuminate\Support\Facades\Cache; use Illuminate\Http\Request; class Updates extends Controller @@ -16,7 +24,7 @@ class Updates extends Controller */ public function index() { - $updates = Updater::all(); + $updates = Versions::getUpdates(); $core = null; @@ -62,7 +70,10 @@ class Updates extends Controller public function check() { // Clear cache in order to check for updates - Updater::clear(); + Cache::forget('updates'); + Cache::forget('versions'); + + event(new UpdateCacheCleared(session('company_id'))); return redirect()->back(); } @@ -155,7 +166,9 @@ class Updates extends Controller set_time_limit(900); // 15 minutes try { - $path = Updater::download($request['alias'], $request['version'], $request['installed']); + $path = $this->dispatch(new DownloadFile($request['alias'], $request['version'])); + + event(new UpdateDownloaded($request['alias'], $request['version'], $request['installed'])); $json = [ 'success' => true, @@ -189,7 +202,9 @@ class Updates extends Controller set_time_limit(900); // 15 minutes try { - $path = Updater::unzip($request['path'], $request['alias'], $request['version'], $request['installed']); + $path = $this->dispatch(new UnzipFile($request['alias'], $request['path'])); + + event(new UpdateUnzipped($request['alias'], $request['version'], $request['installed'])); $json = [ 'success' => true, @@ -223,7 +238,9 @@ class Updates extends Controller set_time_limit(900); // 15 minutes try { - $path = Updater::copyFiles($request['path'], $request['alias'], $request['version'], $request['installed']); + $path = $this->dispatch(new CopyFiles($request['alias'], $request['path'])); + + event(new UpdateCopied($request['alias'], $request['version'], $request['installed'])); $json = [ 'success' => true, @@ -257,7 +274,7 @@ class Updates extends Controller set_time_limit(900); // 15 minutes try { - Updater::finish($request['alias'], $request['version'], $request['installed']); + $this->dispatch(new FinishUpdate($request['alias'], $request['version'], $request['installed'])); $json = [ 'success' => true, diff --git a/app/Http/Controllers/Modules/Item.php b/app/Http/Controllers/Modules/Item.php index c1e58412c..190968de3 100644 --- a/app/Http/Controllers/Modules/Item.php +++ b/app/Http/Controllers/Modules/Item.php @@ -3,11 +3,16 @@ namespace App\Http\Controllers\Modules; use App\Abstracts\Http\Controller; +use App\Jobs\Install\CopyFiles; +use App\Jobs\Install\DisableModule; +use App\Jobs\Install\DownloadFile; +use App\Jobs\Install\EnableModule; +use App\Jobs\Install\InstallModule; +use App\Jobs\Install\UninstallModule; +use App\Jobs\Install\UnzipFile; use App\Models\Module\Module; use App\Traits\Modules; -use File; use Illuminate\Http\Request; -use Illuminate\Support\Str; class Item extends Controller { @@ -83,9 +88,13 @@ class Item extends Controller $name = $request['name']; $alias = $request['alias']; - $module_path = config('module.paths.modules') . '/' . Str::studly($alias); - - if (!File::isDirectory($module_path)) { + if ($this->moduleExists($alias)) { + // Install + $steps[] = [ + 'text' => trans('modules.installation.install', ['module' => $name]), + 'url' => route('apps.install') + ]; + } else { // Download $steps[] = [ 'text' => trans('modules.installation.download', ['module' => $name]), @@ -104,12 +113,6 @@ class Item extends Controller 'url' => route('apps.copy') ]; - // Install - $steps[] = [ - 'text' => trans('modules.installation.install', ['module' => $name]), - 'url' => route('apps.install') - ]; - } else { // Install $steps[] = [ 'text' => trans('modules.installation.install', ['module' => $name]), @@ -134,13 +137,25 @@ class Item extends Controller */ public function download(Request $request) { - $path = $request['path']; + try { + $path = $this->dispatch(new DownloadFile($request['alias'], $request['version'])); - $version = $request['version']; - - $path .= '/' . $version . '/' . version('short') . '/' . setting('apps.api_key'); - - $json = $this->downloadModule($path); + $json = [ + 'success' => true, + 'error' => false, + 'message' => null, + 'data' => [ + 'path' => $path, + ], + ]; + } catch (\Exception $e) { + $json = [ + 'success' => false, + 'error' => true, + 'message' => $e->getMessage(), + 'data' => [], + ]; + } return response()->json($json); } @@ -154,9 +169,25 @@ class Item extends Controller */ public function unzip(Request $request) { - $path = $request['path']; + try { + $path = $this->dispatch(new UnzipFile($request['alias'], $request['path'])); - $json = $this->unzipModule($path); + $json = [ + 'success' => true, + 'error' => false, + 'message' => null, + 'data' => [ + 'path' => $path, + ], + ]; + } catch (\Exception $e) { + $json = [ + 'success' => false, + 'error' => true, + 'message' => $e->getMessage(), + 'data' => [], + ]; + } return response()->json($json); } @@ -170,9 +201,27 @@ class Item extends Controller */ public function copy(Request $request) { - $path = $request['path']; + try { + $this->dispatch(new CopyFiles($request['alias'], $request['path'])); - $json = $this->copyModule($path); + event(new \App\Events\Module\Copied($request['alias'], session('company_id'))); + + $json = [ + 'success' => true, + 'error' => false, + 'message' => null, + 'data' => [ + 'alias' => $request['alias'], + ], + ]; + } catch (\Exception $e) { + $json = [ + 'success' => false, + 'error' => true, + 'message' => $e->getMessage(), + 'data' => [], + ]; + } return response()->json($json); } @@ -186,16 +235,36 @@ class Item extends Controller */ public function install(Request $request) { - $alias = $request['alias']; + try { + $this->dispatch(new InstallModule($request['alias'])); - $json = $this->installModule($alias); + $name = module($request['alias'])->getName(); - if ($json['success']) { - $message = trans('modules.installed', ['module' => $json['data']['name']]); + $message = trans('modules.installed', ['module' => $name]); flash($message)->success(); - } else { - flash($json['message'])->error(); + + $json = [ + 'success' => true, + 'error' => false, + 'message' => null, + 'redirect' => route('apps.app.show', $request['alias']), + 'data' => [ + 'name' => $name, + 'alias' => $request['alias'], + ], + ]; + } catch (\Exception $e) { + $message = $e->getMessage(); + + flash($message)->error(); + + $json = [ + 'success' => false, + 'error' => true, + 'message' => $message, + 'data' => [], + ]; } return response()->json($json); @@ -203,14 +272,18 @@ class Item extends Controller public function uninstall($alias) { - $json = $this->uninstallModule($alias); + try { + $name = module($alias)->getName(); - if ($json['success']) { - $message = trans('modules.uninstalled', ['module' => $json['data']['name']]); + $this->dispatch(new UninstallModule($alias)); + + $message = trans('modules.uninstalled', ['module' => $name]); flash($message)->success(); - } else { - flash($json['message'])->error(); + } catch (\Exception $e) { + $message = $e->getMessage(); + + flash($message)->error(); } return redirect()->route('apps.app.show', $alias)->send(); @@ -218,14 +291,18 @@ class Item extends Controller public function enable($alias) { - $json = $this->enableModule($alias); + try { + $name = module($alias)->getName(); - if ($json['success']) { - $message = trans('modules.enabled', ['module' => $json['data']['name']]); + $this->dispatch(new EnableModule($alias)); + + $message = trans('modules.enabled', ['module' => $name]); flash($message)->success(); - } else { - flash($json['message'])->error(); + } catch (\Exception $e) { + $message = $e->getMessage(); + + flash($message)->error(); } return redirect()->route('apps.app.show', $alias)->send(); @@ -233,14 +310,18 @@ class Item extends Controller public function disable($alias) { - $json = $this->disableModule($alias); + try { + $name = module($alias)->getName(); - if ($json['success']) { - $message = trans('modules.disabled', ['module' => $json['data']['name']]); + $this->dispatch(new DisableModule($alias)); + + $message = trans('modules.disabled', ['module' => $name]); flash($message)->success(); - } else { - flash($json['message'])->error(); + } catch (\Exception $e) { + $message = $e->getMessage(); + + flash($message)->error(); } return redirect()->route('apps.app.show', $alias)->send(); diff --git a/app/Http/ViewComposers/Header.php b/app/Http/ViewComposers/Header.php index cf01dcc1e..f0250408c 100644 --- a/app/Http/ViewComposers/Header.php +++ b/app/Http/ViewComposers/Header.php @@ -2,13 +2,13 @@ namespace App\Http\ViewComposers; -use App\Utilities\Updater; +use App\Utilities\Versions; +use App\Traits\Modules; use Illuminate\View\View; -use App\Traits\Modules as RemoteModules; class Header { - use RemoteModules; + use Modules; /** * Bind data to the view. @@ -35,12 +35,12 @@ class Header ]; } - $undereads = $user->unreadNotifications; + $unreads = $user->unreadNotifications; - foreach ($undereads as $underead) { - $data = $underead->getAttribute('data'); + foreach ($unreads as $unread) { + $data = $unread->getAttribute('data'); - switch ($underead->getAttribute('type')) { + switch ($unread->getAttribute('type')) { case 'App\Notifications\Purchase\Bill': $bills[$data['bill_id']] = $data['amount']; $notifications++; @@ -52,7 +52,7 @@ class Header } } - $updates = count(Updater::all()); + $updates = count(Versions::getUpdates()); $this->loadSuggestions(); } diff --git a/app/Jobs/Install/CopyFiles.php b/app/Jobs/Install/CopyFiles.php new file mode 100644 index 000000000..536262c2c --- /dev/null +++ b/app/Jobs/Install/CopyFiles.php @@ -0,0 +1,77 @@ +alias = $alias; + $this->path = $path; + } + + /** + * Execute the job. + * + * @return string + */ + public function handle() + { + if (empty($this->path)) { + throw new \Exception(trans('modules.errors.file_copy', ['module' => $this->alias])); + } + + $source = storage_path('app/temp/' . $this->path); + + $destination = $this->getDestination($source); + + // Move all files/folders from temp path + if (!File::copyDirectory($source, $destination)) { + throw new \Exception(trans('modules.errors.file_copy', ['module' => $this->alias])); + } + + // Delete temp directory + File::deleteDirectory($source); + } + + protected function getDestination($source) + { + if ($this->alias == 'core') { + return base_path(); + } + + if (!is_file($source . '/module.json')) { + throw new \Exception(trans('modules.errors.file_copy', ['module' => $this->alias])); + } + + $modules_path = config('module.paths.modules'); + + // Create modules directory + if (!File::isDirectory($modules_path)) { + File::makeDirectory($modules_path); + } + + $module_path = $modules_path . '/' . Str::studly($this->alias); + + // Create module directory + if (!File::isDirectory($module_path)) { + File::makeDirectory($module_path); + } + + return $module_path; + } +} diff --git a/app/Jobs/Install/DisableModule.php b/app/Jobs/Install/DisableModule.php new file mode 100644 index 000000000..c561bc49a --- /dev/null +++ b/app/Jobs/Install/DisableModule.php @@ -0,0 +1,45 @@ +alias = $alias; + $this->company_id = $company_id ?: session('company_id'); + $this->locale = $locale ?: app()->getLocale(); + } + + /** + * Execute the job. + * + * @return string + */ + public function handle() + { + $command = "module:disable {$this->alias} {$this->company_id} {$this->locale}"; + + $result = Console::run($command); + + if ($result !== true) { + throw new \Exception($result); + } + } +} diff --git a/app/Jobs/Install/DownloadFile.php b/app/Jobs/Install/DownloadFile.php new file mode 100644 index 000000000..aa6f6ce0d --- /dev/null +++ b/app/Jobs/Install/DownloadFile.php @@ -0,0 +1,75 @@ +alias = $alias; + $this->version = $version; + } + + /** + * Execute the job. + * + * @return string + */ + public function handle() + { + if (!$response = static::getResponse('GET', $this->getUrl(), ['timeout' => 50, 'track_redirects' => true])) { + throw new \Exception(trans('modules.errors.download', ['module' => $this->alias])); + } + + $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) { + throw new \Exception(trans('modules.errors.download', ['module' => $this->alias])); + } + + return $path; + } + + protected function getUrl() + { + if ($this->alias == 'core') { + $info = Info::all(); + + $url = 'core/download/' . $this->version . '/' . $info['php'] . '/' . $info['mysql']; + } else { + $url = 'apps/' . $this->alias . '/download/' . $this->version . '/' . version('short') . '/' . setting('apps.api_key'); + } + + return $url; + } +} diff --git a/app/Jobs/Install/EnableModule.php b/app/Jobs/Install/EnableModule.php new file mode 100644 index 000000000..6acb41d81 --- /dev/null +++ b/app/Jobs/Install/EnableModule.php @@ -0,0 +1,45 @@ +alias = $alias; + $this->company_id = $company_id ?: session('company_id'); + $this->locale = $locale ?: app()->getLocale(); + } + + /** + * Execute the job. + * + * @return string + */ + public function handle() + { + $command = "module:enable {$this->alias} {$this->company_id} {$this->locale}"; + + $result = Console::run($command); + + if ($result !== true) { + throw new \Exception($result); + } + } +} diff --git a/app/Jobs/Install/FinishUpdate.php b/app/Jobs/Install/FinishUpdate.php new file mode 100644 index 000000000..fa6d606f3 --- /dev/null +++ b/app/Jobs/Install/FinishUpdate.php @@ -0,0 +1,56 @@ +alias = $alias; + $this->new = $new; + $this->old = $old; + } + + /** + * Execute the job. + * + * @return string + */ + public function handle() + { + if ($this->alias == 'core') { + $companies = [session('company_id')]; + } else { + $companies = Module::alias($this->alias)->allCompanies()->cursor(); + } + + foreach ($companies as $company) { + $company_id = is_object($company) ? $company->id : $company; + + $command = "update:finish {$this->alias} {$company_id} {$this->new} {$this->old}"; + + if (true !== $result = Console::run($command)) { + $message = !empty($result) ? $result : trans('modules.errors.finish', ['module' => $this->alias]); + + throw new \Exception($message); + } + } + } +} diff --git a/app/Jobs/Install/InstallModule.php b/app/Jobs/Install/InstallModule.php new file mode 100644 index 000000000..72d60a1dd --- /dev/null +++ b/app/Jobs/Install/InstallModule.php @@ -0,0 +1,47 @@ +alias = $alias; + $this->company_id = $company_id ?: session('company_id'); + $this->locale = $locale ?: app()->getLocale(); + } + + /** + * Execute the job. + * + * @return string + */ + public function handle() + { + $command = "module:install {$this->alias} {$this->company_id} {$this->locale}"; + + $result = Console::run($command); + + if ($result !== true) { + $message = !empty($result) ? $result : trans('modules.errors.finish', ['module' => $this->alias]); + + throw new \Exception($message); + } + } +} diff --git a/app/Jobs/Install/UninstallModule.php b/app/Jobs/Install/UninstallModule.php new file mode 100644 index 000000000..f9eac64ca --- /dev/null +++ b/app/Jobs/Install/UninstallModule.php @@ -0,0 +1,45 @@ +alias = $alias; + $this->company_id = $company_id ?: session('company_id'); + $this->locale = $locale ?: app()->getLocale(); + } + + /** + * Execute the job. + * + * @return string + */ + public function handle() + { + $command = "module:uninstall {$this->alias} {$this->company_id} {$this->locale}"; + + $result = Console::run($command); + + if ($result !== true) { + throw new \Exception($result); + } + } +} diff --git a/app/Jobs/Install/UnzipFile.php b/app/Jobs/Install/UnzipFile.php new file mode 100644 index 000000000..5ae79992f --- /dev/null +++ b/app/Jobs/Install/UnzipFile.php @@ -0,0 +1,56 @@ +alias = $alias; + $this->path = $path; + } + + /** + * Execute the job. + * + * @return string + */ + public function handle() + { + if (empty($this->path)) { + throw new \Exception(trans('modules.errors.unzip', ['module' => $this->alias])); + } + + $temp_path = storage_path('app/temp/' . $this->path); + + $file = $temp_path . '/upload.zip'; + + // Unzip the file + $zip = new ZipArchive(); + + if (!$zip->open($file) || !$zip->extractTo($temp_path)) { + throw new \Exception(trans('modules.errors.unzip', ['module' => $this->alias])); + } + + $zip->close(); + + // Remove Zip + File::delete($file); + + return $this->path; + } +} diff --git a/app/Traits/Modules.php b/app/Traits/Modules.php index 2686e7c78..a714747e1 100644 --- a/app/Traits/Modules.php +++ b/app/Traits/Modules.php @@ -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) { diff --git a/app/Utilities/Updater.php b/app/Utilities/Updater.php deleted file mode 100644 index e9d04f9ee..000000000 --- a/app/Utilities/Updater.php +++ /dev/null @@ -1,190 +0,0 @@ - 50, 'track_redirects' => true])) { - throw new \Exception(trans('modules.errors.download', ['module' => $alias])); - } - - $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) { - throw new \Exception(trans('modules.errors.zip', ['module' => $alias])); - } - - event(new UpdateDownloaded($alias, $new, $old)); - - return $path; - } - - public static function unzip($path, $alias, $new, $old) - { - $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)) { - throw new \Exception(trans('modules.errors.unzip', ['module' => $alias])); - } - - $zip->close(); - - // Delete zip file - File::delete($file); - - event(new UpdateUnzipped($alias, $new, $old)); - - return $path; - } - - public static function copyFiles($path, $alias, $new, $old) - { - $temp_path = storage_path('app/temp') . '/' . $path; - - if ($alias == 'core') { - // Move all files/folders from temp path - if (!File::copyDirectory($temp_path, base_path())) { - throw new \Exception(trans('modules.errors.file_copy', ['module' => $alias])); - } - } else { - if ($module = module($alias)) { - $module_path = $module->getPath(); - } else { - $module_path = base_path('modules/' . Str::studly($alias)); - } - - // 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)) { - throw new \Exception(trans('modules.errors.file_copy', ['module' => $alias])); - } - } - - // Delete temp directory - File::deleteDirectory($temp_path); - - event(new UpdateCopied($alias, $new, $old)); - - return $path; - } - - public static function finish($alias, $new, $old) - { - if ($alias == 'core') { - $companies = [session('company_id')]; - } else { - $companies = Module::alias($alias)->where('company_id', '<>', '0')->pluck('company_id')->toArray(); - } - - foreach ($companies as $company) { - $command = "update:finish {$alias} {$company} {$new} {$old}"; - - if (true !== $result = Console::run($command)) { - $message = !empty($result) ? $result : trans('modules.errors.finish', ['module' => $alias]); - - throw new \Exception($message); - } - } - } - - public static function all() - { - // Get data from cache - $updates = Cache::get('updates'); - - if (!empty($updates)) { - return $updates; - } - - $updates = []; - - $modules = module()->all(); - - $versions = Versions::all($modules); - - foreach ($versions as $alias => $latest_version) { - if ($alias == 'core') { - $installed_version = version('short'); - } else { - $module = module($alias); - - if (!$module instanceof \Akaunting\Module\Module) { - continue; - } - - $installed_version = $module->get('version'); - } - - if (version_compare($installed_version, $latest_version, '>=')) { - continue; - } - - $updates[$alias] = $latest_version; - } - - Cache::put('updates', $updates, Date::now()->addHour(6)); - - return $updates; - } -} diff --git a/app/Utilities/Versions.php b/app/Utilities/Versions.php index df7e42109..6a0ac06bf 100644 --- a/app/Utilities/Versions.php +++ b/app/Utilities/Versions.php @@ -105,7 +105,7 @@ class Versions return $versions; } - protected static function getLatestVersion($url, $latest) + public static function getLatestVersion($url, $latest) { if (!$data = static::getResponseData('GET', $url, ['timeout' => 10])) { return $latest; @@ -117,4 +117,44 @@ class Versions return $data->latest; } + + public static function getUpdates() + { + // Get data from cache + $updates = Cache::get('updates'); + + if (!empty($updates)) { + return $updates; + } + + $updates = []; + + $modules = module()->all(); + + $versions = static::all($modules); + + foreach ($versions as $alias => $latest_version) { + if ($alias == 'core') { + $installed_version = version('short'); + } else { + $module = module($alias); + + if (!$module instanceof \Akaunting\Module\Module) { + continue; + } + + $installed_version = $module->get('version'); + } + + if (version_compare($installed_version, $latest_version, '>=')) { + continue; + } + + $updates[$alias] = $latest_version; + } + + Cache::put('updates', $updates, Date::now()->addHour(6)); + + return $updates; + } }