Add DownloadStyleLogoJob
authorTim Düsterhus <duesterhus@woltlab.com>
Tue, 1 Sep 2020 14:45:40 +0000 (16:45 +0200)
committerTim Düsterhus <duesterhus@woltlab.com>
Tue, 1 Sep 2020 14:46:01 +0000 (16:46 +0200)
wcfsetup/install/files/lib/system/background/job/DownloadStyleLogoJob.class.php [new file with mode: 0644]

diff --git a/wcfsetup/install/files/lib/system/background/job/DownloadStyleLogoJob.class.php b/wcfsetup/install/files/lib/system/background/job/DownloadStyleLogoJob.class.php
new file mode 100644 (file)
index 0000000..b4b203c
--- /dev/null
@@ -0,0 +1,78 @@
+<?php
+namespace wcf\system\background\job;
+use GuzzleHttp\Psr7\Request;
+use wcf\data\style\Style;
+use wcf\data\style\StyleEditor;
+use wcf\system\io\HttpFactory;
+use wcf\util\Url;
+
+/**
+ * Downloads the style's logo and stores it locally within the style's asset path.
+ * 
+ * @author     Tim Duesterhus
+ * @copyright  2001-2020 WoltLab GmbH
+ * @license    GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ * @package    WoltLabSuite\Core\System\Background\Job
+ * @since      5.3
+ * @deprecated 5.3 - This background job is used for the upgrade from 5.2 to 5.3.
+ */
+class DownloadStyleLogoJob extends AbstractBackgroundJob {
+       /**
+        * @inheritDoc
+        */
+       const MAX_FAILURES = 5;
+       
+       /**
+        * @var int
+        */
+       protected $styleID;
+       
+       public function __construct(Style $style) {
+               $this->styleID = $style->styleID;
+       }
+       
+       /**
+        * @return      int     every 10 minutes
+        */
+       public function retryAfter() {
+               return 10 * 60;
+       }
+       
+       /**
+        * @inheritDoc
+        */
+       public function perform() {
+               $style = new Style($this->styleID);
+               if (!$style->styleID) return;
+               $styleEditor = new StyleEditor($style);
+               
+               $style->loadVariables();
+               $variables = $style->getVariables();
+               
+               $http = HttpFactory::makeClient([
+                       'timeout' => 10,
+               ]);
+               
+               foreach (['pageLogo', 'pageLogoMobile'] as $type) {
+                       if ($variables[$type] && Url::is($variables[$type])) {
+                               $extension = pathinfo(Url::parse($variables[$type])['path'], PATHINFO_EXTENSION);
+                               
+                               if (in_array($extension, ['gif','png','jpg','jpeg','svg','webp'])) {
+                                       $newLocation = $type . '.' . $extension;
+                                       
+                                       $http->send(new Request('GET', $variables[$type]), [
+                                               'sink' => $style->getAssetPath() . $newLocation,
+                                       ]);
+                                       
+                                       $variables[$type] = $newLocation;
+                               }
+                               else {
+                                       $variables[$type] = '';
+                               }
+                               
+                               $styleEditor->setVariables($variables);
+                       }
+               }
+               StyleEditor::resetCache();
+       }
+}