]> _ Git - fluidbook-toolbox.git/commitdiff
wip #6775 @2
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Mon, 11 Mar 2024 19:25:07 +0000 (20:25 +0100)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Mon, 11 Mar 2024 19:25:07 +0000 (20:25 +0100)
app/Console/Commands/WebflowMirrorSite.php [new file with mode: 0644]
app/Http/Controllers/Admin/Operations/Tools/StaticSiteUploader.php
app/Http/Controllers/Admin/Operations/Tools/WebflowOperation.php [new file with mode: 0644]
app/Jobs/WebflowPublish.php [new file with mode: 0644]
app/Models/ToolWebflow.php
app/Slack/Slack.php

diff --git a/app/Console/Commands/WebflowMirrorSite.php b/app/Console/Commands/WebflowMirrorSite.php
new file mode 100644 (file)
index 0000000..ee8fd28
--- /dev/null
@@ -0,0 +1,18 @@
+<?php
+
+namespace App\Console\Commands;
+
+use App\Console\Commands\Base\ToolboxCommand;
+use App\Jobs\WebflowMirror;
+use App\Models\ToolWebflow;
+
+class WebflowMirrorSite extends ToolboxCommand
+{
+    protected $signature = 'webflow:mirror {id} {--force} {--slow}';
+
+    public function handle()
+    {
+        $job=new WebflowMirror($this->argument('id'),$this->option('force'),$this->option('slow'));
+        $job->handle();
+    }
+}
index 155b2faf6b0f21ed7b1cb089406f5ec78bf1a9d5..e3496b4b8d92715363709ea0dd145f5a5f3dfc40 100644 (file)
@@ -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 (file)
index 0000000..5c7ea03
--- /dev/null
@@ -0,0 +1,21 @@
+<?php
+
+namespace App\Http\Controllers\Admin\Operations\Tools;
+
+use App\Http\Middleware\CheckIfAdmin;
+use App\Http\Middleware\VerifyCsrfToken;
+use App\Jobs\WebflowMirror;
+use App\Models\ToolWebflow;
+use Illuminate\Support\Facades\Log;
+use Illuminate\Support\Facades\Route;
+
+trait WebflowOperation
+{
+    protected function setupWebflowRoutes($segment, $routeName, $controller)
+    {
+        Route::match(['get', 'post'], 'webhook/webflow/{id}', function ($id) {
+            ToolWebflow::withoutGlobalScopes()->find($id)->onPublish();
+        })->withoutMiddleware([VerifyCsrfToken::class, CheckIfAdmin::class]);
+    }
+
+}
diff --git a/app/Jobs/WebflowPublish.php b/app/Jobs/WebflowPublish.php
new file mode 100644 (file)
index 0000000..ffd3f03
--- /dev/null
@@ -0,0 +1,21 @@
+<?php
+
+namespace App\Jobs;
+
+use App\Http\Controllers\Admin\Operations\Tools\StaticSiteUploader;
+use App\Models\ToolWebflow;
+use App\Slack\Slack;
+
+class WebflowPublish extends Base
+{
+    public function __construct($id)
+    {
+        /** @var ToolWebflow $wf */
+        $wf = ToolWebflow::withoutGlobalScopes()->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']]);
+    }
+}
index 02f4e90b011e18057cecf2338e22a538f2ac9d84..f9c1ef601558fa6e467ee9ad4a291ce8ee0231f7 100644 (file)
@@ -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();
     }
 
index b9fa119c857c428a8af2aa2f93cef4da42422a72..978aa7c0f8af6fc65428c581f786cd1af192aef3 100644 (file)
@@ -10,6 +10,7 @@ class Slack
     const fluidbookQuoteChannel = 'C045CH0UB47';
     const fluidbookPreviewAlertsChannel = 'C04Q9LZNPT2';
     const fluidbookFilesChannel='C04QJSY7CEM';
+    const cubedesignersWebsiteChannel='C8Q4C8TC5';
 
     /**
      * @var \JoliCode\Slack\Client|null