From 52a70f86044ce02f7614192c8533add3196df65c Mon Sep 17 00:00:00 2001 From: Vincent Vanwaelscappel Date: Mon, 11 Mar 2024 20:25:07 +0100 Subject: [PATCH] wip #6775 @2 --- app/Console/Commands/WebflowMirrorSite.php | 18 ++++++ .../Operations/Tools/StaticSiteUploader.php | 22 +++++-- .../Operations/Tools/WebflowOperation.php | 21 +++++++ app/Jobs/WebflowPublish.php | 21 +++++++ app/Models/ToolWebflow.php | 61 ++++++++++++++----- app/Slack/Slack.php | 1 + 6 files changed, 123 insertions(+), 21 deletions(-) create mode 100644 app/Console/Commands/WebflowMirrorSite.php create mode 100644 app/Http/Controllers/Admin/Operations/Tools/WebflowOperation.php create mode 100644 app/Jobs/WebflowPublish.php diff --git a/app/Console/Commands/WebflowMirrorSite.php b/app/Console/Commands/WebflowMirrorSite.php new file mode 100644 index 000000000..ee8fd2816 --- /dev/null +++ b/app/Console/Commands/WebflowMirrorSite.php @@ -0,0 +1,18 @@ +argument('id'),$this->option('force'),$this->option('slow')); + $job->handle(); + } +} diff --git a/app/Http/Controllers/Admin/Operations/Tools/StaticSiteUploader.php b/app/Http/Controllers/Admin/Operations/Tools/StaticSiteUploader.php index 155b2faf6..e3496b4b8 100644 --- a/app/Http/Controllers/Admin/Operations/Tools/StaticSiteUploader.php +++ b/app/Http/Controllers/Admin/Operations/Tools/StaticSiteUploader.php @@ -12,10 +12,10 @@ use Cubist\Util\Zip; trait StaticSiteUploader { - protected function _getSites() + public static function getSites() { return [ - 'cubedesigners-v8' => ['label' => __('Cubedesigners V8'), 'server' => 'godzilla.cubedesigners.com', 'url' => 'https://v8.cubedesigners.com/'] + 'cubedesigners-v8' => ['id' => 'cubedesigners-v8', 'label' => __('Cubedesigners V8'), 'server' => 'godzilla.cubedesigners.com', 'url' => 'https://v8.cubedesigners.com/'] ]; } @@ -30,7 +30,7 @@ trait StaticSiteUploader $form->setSubmitLabel(__('Charger')); $form->setSubmitIcon('lab la-js-square'); $form->addField('file', StandardFile::class, __('Fichier zip'), ['accept' => '.zip', 'hint' => __('La racine du zip doit correspondre à la racine du site')]); - $s = $this->_getSites(); + $s = static::getSites(); $sites = []; foreach ($s as $k => $item) { $sites[$k] = $item['label']; @@ -53,10 +53,20 @@ trait StaticSiteUploader rename($file->getPathname(), $zip); $tmp = Files::tmpdir(); Zip::extract($zip, $tmp); - $dest = 'staticupload@' . $site['server'] . ':/home/staticupload/' . $siteId; - $rsync = new Rsync($tmp, $dest); - $rsync->execute(); + + self::rsync($tmp, $site); return redirect($site['url']); } + + public static function rsync($from, $site) + { + if (is_string($site)) { + $site = static::getSites()[$site]; + } + + $dest = 'staticupload@' . $site['server'] . ':/home/staticupload/' . $site['id']; + $rsync = new Rsync($from, $dest); + $rsync->execute(); + } } diff --git a/app/Http/Controllers/Admin/Operations/Tools/WebflowOperation.php b/app/Http/Controllers/Admin/Operations/Tools/WebflowOperation.php new file mode 100644 index 000000000..5c7ea0391 --- /dev/null +++ b/app/Http/Controllers/Admin/Operations/Tools/WebflowOperation.php @@ -0,0 +1,21 @@ +find($id)->onPublish(); + })->withoutMiddleware([VerifyCsrfToken::class, CheckIfAdmin::class]); + } + +} diff --git a/app/Jobs/WebflowPublish.php b/app/Jobs/WebflowPublish.php new file mode 100644 index 000000000..ffd3f03ca --- /dev/null +++ b/app/Jobs/WebflowPublish.php @@ -0,0 +1,21 @@ +find($id); + $wf->mirror(false, true)->debug(); + + StaticSiteUploader::rsync(protected_path('webflow/' . $id), $wf->upload); + + Slack::send($wf->slack, __('Site publié'), __('Le site vient d\'être républié suite à une mise à jour de webflow ou manuellement'), ['Visiter' => $wf->getLocales()[0]['url']]); + } +} diff --git a/app/Models/ToolWebflow.php b/app/Models/ToolWebflow.php index 02f4e90b0..f9c1ef601 100644 --- a/app/Models/ToolWebflow.php +++ b/app/Models/ToolWebflow.php @@ -2,10 +2,15 @@ namespace App\Models; +use App\Http\Controllers\Admin\Operations\Tools\StaticSiteUploader; +use App\Http\Controllers\Admin\Operations\Tools\WebflowOperation; +use App\Jobs\WebflowPublish; use App\Models\Base\ToolboxModel; +use Cubist\Backpack\Magic\Fields\SelectFromArray; use Cubist\Backpack\Magic\Fields\Table; use Cubist\Backpack\Magic\Fields\Text; +use Cubist\Backpack\Magic\Fields\Textarea; use Cubist\Backpack\Magic\Fields\URL; use Cubist\Util\CommandLine; use Cubist\Util\Files\Files; @@ -22,7 +27,7 @@ class ToolWebflow extends ToolboxModel 'singular' => 'site', 'plural' => 'sites']; - protected $_operations = []; + protected $_operations = [WebflowOperation::class]; public function setFields() { @@ -30,7 +35,15 @@ class ToolWebflow extends ToolboxModel $this->addField('name', Text::class, __('Projet'), ['column' => true, 'tab' => __('Paramètres')]); $this->addField('webflow', Text::class, __('URL du projet webflow'), ['prefix' => 'https://', 'suffix' => '.webflow.io', 'column' => true, 'hint' => __('ex : :url', ['url' => 'https://projet.webflow.io']), 'tab' => __('Paramètres')]); + $this->addField('domains', Textarea::class, __('Domaines à télécharger'), ['tab' => __('Paramètres')]); $this->addField('locales', Table::class, __('Langues'), ['columns' => ['locale' => __('Code langue'), 'url' => __('URL')], 'tab' => __('Paramètres')]); + $this->addField('slack', Text::class, __('Notification slack'), ['tab' => __('Paramètres')]); + $s = StaticSiteUploader::getSites(); + $sites = []; + foreach ($s as $k => $item) { + $sites[$k] = $item['label']; + } + $this->addField('upload', SelectFromArray::class, __('Uploader sur'), ['options' => $sites, 'tab' => __('Paramètres')]); //$this->addField('texts'); //$this->addField('images'); //$this->addField('seo'); @@ -41,10 +54,21 @@ class ToolWebflow extends ToolboxModel return Cache::get('webflow_' . $this->id . '_locales', []); } - public function mirror($slow = true, $reset = false) + + public function onPublish() + { + WebflowPublish::dispatch($this->id); + } + + /** + * @param $slow bool + * @param $force bool + * @return CommandLine + */ + public function mirror($slow = true, $force = false) { $path = Files::mkdir(protected_path('webflow/' . $this->id)); - if ($reset) { + if ($force) { $path = Files::emptyDir($path); } $wget = new CommandLine('wget'); @@ -55,25 +79,29 @@ class ToolWebflow extends ToolboxModel $wget->setArg('user-agent', 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:124.0) Gecko/20100101 Firefox/124.0'); $wget->setArg("directory-prefix", $path); $wget->setArg("span-hosts"); + $wget->setArg("no-host-directories"); if ($slow) { - $wget->setArg("w", $reset ? 2 : 10); + $wget->setArg("wait", $force ? 2 : 10); $wget->setArg("random-wait"); } - $domains = [$this->webflow . '.webflow.io', - 'assets-global.website-files.com', - 'cdn.jsdelivr.net', - 'unpkg.com', - 'cdnjs.cloudflare.com', - 'ajax.googleapis.com', - ]; + $domains = array_unique([$this->webflow . '.webflow.io', + 'assets-global.website-files.com', + 'cdn.jsdelivr.net', + 'unpkg.com', + 'cdnjs.cloudflare.com', + 'ajax.googleapis.com', + 'uploads-ssl.webflow.com', + 'uploads.webflow.com', + ] + \Cubist\Util\Text::splitLines($this->domains)); + $wget->setArg("domains", implode(',', $domains)); $wget->setArg("compression", 'auto'); - if (!$reset) { + if (!$force) { $wget->setArg('N'); } $wget->setArg(null, 'https://' . $this->webflow . '.webflow.io'); $wget->execute(); - $wget->dd(); + return $wget; } @@ -84,8 +112,11 @@ class ToolWebflow extends ToolboxModel public function onRetrieved(): bool { - Cache::put('webflow_' . $this->id . '_locales', $this->locales); - $this->mirror(); + $locales = $this->locales; + if (is_string($locales)) { + $locales = json_decode($locales, true); + } + Cache::put('webflow_' . $this->id . '_locales', $locales); return parent::onRetrieved(); } diff --git a/app/Slack/Slack.php b/app/Slack/Slack.php index b9fa119c8..978aa7c0f 100644 --- a/app/Slack/Slack.php +++ b/app/Slack/Slack.php @@ -10,6 +10,7 @@ class Slack const fluidbookQuoteChannel = 'C045CH0UB47'; const fluidbookPreviewAlertsChannel = 'C04Q9LZNPT2'; const fluidbookFilesChannel='C04QJSY7CEM'; + const cubedesignersWebsiteChannel='C8Q4C8TC5'; /** * @var \JoliCode\Slack\Client|null -- 2.39.5