From: Vincent Vanwaelscappel Date: Mon, 17 Jun 2024 14:17:44 +0000 (+0200) Subject: wait #6947 @12 X-Git-Url: http://git.cubedesigners.com/?a=commitdiff_plain;h=e66e53fdd8028eb70b824c6cd22bfeb0e68fb55e;p=fluidbook-toolbox.git wait #6947 @12 --- diff --git a/app/Jobs/WebflowPublish.php b/app/Jobs/WebflowPublish.php index 299781d7a..b57486292 100644 --- a/app/Jobs/WebflowPublish.php +++ b/app/Jobs/WebflowPublish.php @@ -27,11 +27,13 @@ class WebflowPublish extends Base public function handle() { + start_measure('Webflow Publish '.$this->id.' '.$this->mode); /** @var ToolWebflow $wf */ $wf = ToolWebflow::withoutGlobalScopes()->find($this->id); $subject = __('Site :name publié', ['name' => $wf->name]); if ($this->mode === 'webflow') { - $wf->mirror(false, rand(1, 10) === 10); + $wf->refreshFormDataFromAPI(); + $wf->mirror(false); $text = __('Le site vient d\'être républié suite à une mise à jour de webflow'); } else if ($this->mode === 'auto') { $text = __('Le site vient d\'être républié suite à une mise à jour des contenus'); @@ -46,10 +48,11 @@ class WebflowPublish extends Base $actions[__('Visiter [:locale]', ['locale' => $locale['locale']])] = 'https://' . $locale['url']; } if ($this->user === null) { - // Slack::send($wf->slack, $subject, $text, $actions, false); + // Slack::send($wf->slack, $subject, $text, $actions, false); } else { $this->user->notify(new ToolboxNotification($subject, $text, $actions, true)); } + stop_measure('Webflow Publish '.$this->id.' '.$this->mode); } } diff --git a/app/Models/ToolWebflow.php b/app/Models/ToolWebflow.php index e29e42530..f7ecfba07 100644 --- a/app/Models/ToolWebflow.php +++ b/app/Models/ToolWebflow.php @@ -120,8 +120,11 @@ class ToolWebflow extends ToolboxTranslatableModel } $seo = $this->getTranslations('seo'); $images = $this->getTranslations('images'); + $locales = $this->getLocalesCodes(); + $translateAPI = new Api(env('AZURE_TRANSLATE_API_KEY')); $hasChanged = false; + $imagesChanged = false; foreach ($seo[$mainLocale] as $page) { @@ -140,14 +143,32 @@ class ToolWebflow extends ToolboxTranslatableModel } foreach ($images[$mainLocale] as $id => $alt) { - if ($api['images'][$id] !== $alt) { - $api['images'][$id] = $alt; + $apiAlt = $api['images'][$id]['alt'] ?? str_starts_with($api['images'][$id]['alt'], '__wf_') ? '' : $api['images'][$id]['alt']; + if ($apiAlt != $alt) { + $api['images'][$id]['alt'] = $alt; Webflow::saveImageAlt($id, $alt); + $hasChanged = true; + } + + if ($alt) { + foreach ($locales as $locale) { + if ($locale === $mainLocale) { + continue; + } + if (!$images[$locale][$id]) { + $images[$locale][$id] = $translateAPI->translate($alt, $locale, $mainLocale); + $imagesChanged = true; + $hasChanged = true; + } + } } } if ($hasChanged) { $this->api = json_encode($api); + if ($imagesChanged) { + $this->setTranslations('images', $images); + } $this->saveQuietly(); } @@ -175,11 +196,12 @@ class ToolWebflow extends ToolboxTranslatableModel return Cache::lock('webflow_api_refresh_' . $this->id); } - protected function refreshFormDataFromAPI() + public function refreshFormDataFromAPI() { + start_measure("Webflow refresh data from api"); $lock = $this->getLock(); try { - $lock->block(360); + // $lock->block(360); Webflow::clearCache(); $this->api = Webflow::getEditableData($this->webflow); @@ -262,8 +284,10 @@ class ToolWebflow extends ToolboxTranslatableModel } catch (LockTimeoutException $e) { } finally { - $lock?->release(); + $lock?->forceRelease(); } + + stop_measure("Webflow refresh data from api"); } /** @@ -273,8 +297,9 @@ class ToolWebflow extends ToolboxTranslatableModel */ public function mirror($slow = true, $force = false) { + start_measure('Webflow mirror'); + Webflow::setToken($this->webflow_api_token); - $this->refreshFormDataFromAPI(); $path = $this->getMirrorPath(); if ($force) { @@ -310,6 +335,8 @@ class ToolWebflow extends ToolboxTranslatableModel } $wget->setArg(null, 'https://' . $this->webflow . '.webflow.io'); $wget->execute(); + + stop_measure('Webflow mirror'); return $wget; } @@ -390,7 +417,7 @@ class ToolWebflow extends ToolboxTranslatableModel $lock = $this->getLock(); try { - $lock->block(1200); + $lock->block(360); $locales = $this->locales_domains; if (is_string($locales)) { @@ -408,7 +435,7 @@ class ToolWebflow extends ToolboxTranslatableModel } catch (LockTimeoutException $e) { } finally { - $lock?->release(); + $lock?->forceRelease(); } diff --git a/app/Services/Webflow.php b/app/Services/Webflow.php index a9775ab6f..c8a161692 100644 --- a/app/Services/Webflow.php +++ b/app/Services/Webflow.php @@ -78,6 +78,7 @@ class Webflow public static function request($url, $data = [], $method = 'get', $ttl = 86400, $force = false) { + start_measure('Webflow API : ' . $url); $cacheKey = self::getCacheKey($url, $data, $method); if ($force) { Cache::forget($cacheKey); @@ -97,6 +98,7 @@ class Webflow if (null === $res && !$force) { return self::request($url, $data, $method, $ttl, true); } + stop_measure('Webflow API : ' . $url); return $res; } @@ -240,11 +242,13 @@ class Webflow public static function saveImageAlt($id, $alt) { - + return self::request('assets/' . $id, ['altText' => $alt], 'post', 0, true); } public static function getEditableData($shortname) { + start_measure('Webflow : get editable data'); + $key = self::getToken() . '_' . $shortname; if (!isset(self::$_editableData[$key])) { $data = self::getAllData($shortname); @@ -306,7 +310,7 @@ class Webflow if (isset($res['images'][$assetId])) { continue; } - $res['images'][$pageID . '_' . $assetId] = ['page' => $pageID, 'id' => $assetId, 'alt' => $node['image']['alt'], 'url' => $data['assets'][$assetId]['hostedUrl']]; + $res['images'][$assetId] = ['page' => $pageID, 'id' => $assetId, 'alt' => $node['image']['alt'], 'url' => $data['assets'][$assetId]['hostedUrl']]; } } } @@ -324,6 +328,8 @@ class Webflow self::$_editableData[$key] = $res; } + + stop_measure('Webflow : get editable data'); return self::$_editableData[$key]; } } diff --git a/resources/views/fields/webflow/images.blade.php b/resources/views/fields/webflow/images.blade.php index 04d65ea8a..55558ffad 100644 --- a/resources/views/fields/webflow/images.blade.php +++ b/resources/views/fields/webflow/images.blade.php @@ -14,7 +14,11 @@ Texte alternatif @foreach($images as $id=>$image) + @if(is_string($image)) + @continue + @endif @php + $e=explode('/',$image['url']); $ee=array_pop($e); $e=explode('_',$ee,2);