Clean up code and unify code style
authorjoshuaruesweg <ruesweg@woltlab.com>
Fri, 12 Mar 2021 15:04:50 +0000 (16:04 +0100)
committerjoshuaruesweg <ruesweg@woltlab.com>
Tue, 16 Mar 2021 15:19:16 +0000 (16:19 +0100)
wcfsetup/install/files/acp/database/update_com.woltlab.wcf_5.4.php
wcfsetup/install/files/lib/system/background/job/UnfurlUrlBackgroundJob.class.php
wcfsetup/install/files/lib/system/message/unfurl/UnfurlResponse.class.php

index 9ca933b5e40a95d531a0830a572b9182c5b08df0..5e5dcf180155651180fd6b03dd8c80cf6b784428 100644 (file)
@@ -233,7 +233,7 @@ return [
             NotNullInt10DatabaseTableColumn::create('height'),
             VarcharDatabaseTableColumn::create('imageExtension')
                 ->length(4),
-            DefaultFalseBooleanDatabaseTableColumn::create('isStored')
+            DefaultFalseBooleanDatabaseTableColumn::create('isStored'),
         ])
         ->indices([
             DatabaseTablePrimaryIndex::create()
index c549825afe5aed6d12680cee028bb601ae7faacd..121e75269b3050ca53606b21c73bc4e234bca42a 100644 (file)
@@ -4,6 +4,7 @@ namespace wcf\system\background\job;
 
 use BadMethodCallException;
 use GuzzleHttp\Psr7\Response;
+use LogicException;
 use wcf\data\unfurl\url\UnfurlUrl;
 use wcf\data\unfurl\url\UnfurlUrlAction;
 use wcf\system\message\unfurl\exception\DownloadFailed;
@@ -34,8 +35,6 @@ final class UnfurlUrlBackgroundJob extends AbstractBackgroundJob
 
     /**
      * UnfurlURLJob constructor.
-     *
-     * @param UnfurlUrl $url
      */
     public function __construct(UnfurlUrl $url)
     {
@@ -70,23 +69,22 @@ final class UnfurlUrlBackgroundJob extends AbstractBackgroundJob
         try {
             $unfurlResponse = UnfurlResponse::fetch($unfurlUrl->url);
 
-            if (empty(StringUtil::trim($unfurlResponse->getTitle()))) {
+            if (empty($unfurlResponse->getTitle())) {
                 $this->save(UnfurlUrl::STATUS_REJECTED);
 
                 return;
             }
 
-            $title = StringUtil::truncate(StringUtil::trim($unfurlResponse->getTitle()), 255);
+            $title = StringUtil::truncate($unfurlResponse->getTitle(), 255);
             $description = "";
             if ($unfurlResponse->getDescription()) {
-                $description = StringUtil::truncate(StringUtil::trim($unfurlResponse->getDescription()), 160);
+                $description = StringUtil::truncate($unfurlResponse->getDescription(), 160);
             }
 
             $imageData = [];
             $imageID = null;
-            if ($unfurlResponse->getImageUrl()) {
-                $imageUrl = $unfurlResponse->getImageUrl();
-
+            $imageUrl = $unfurlResponse->getImageUrl();
+            if ($imageUrl) {
                 if (
                     \strpos($imageUrl, '\\') === false
                     && \strpos($imageUrl, "'") === false
@@ -118,8 +116,6 @@ final class UnfurlUrlBackgroundJob extends AbstractBackgroundJob
 
     private function getImageData(UnfurlResponse $unfurlResponse): array
     {
-        $imageSaveData = [];
-
         if (empty($unfurlResponse->getImageUrl()) || !Url::is($unfurlResponse->getImageUrl())) {
             throw new \InvalidArgumentException("The given response does not have an image.");
         }
@@ -129,24 +125,33 @@ final class UnfurlUrlBackgroundJob extends AbstractBackgroundJob
             $image = $this->downloadImage($imageResponse);
             $imageData = \getimagesizefromstring($image);
 
-            if ($imageData !== false) {
-                if ($this->validateImage($imageData)) {
-                    $imageSaveData['imageUrl'] = $unfurlResponse->getImageUrl();
-                    $imageSaveData['imageUrlHash'] = \sha1($unfurlResponse->getImageUrl());
-                    $imageSaveData['width'] = $imageData[0];
-                    $imageSaveData['height'] = $imageData[1];
-                    if (!(MODULE_IMAGE_PROXY || IMAGE_ALLOW_EXTERNAL_SOURCE)) {
-                        $this->saveImage($imageData, $image, $imageSaveData['imageUrlHash']);
-                        $imageSaveData['imageExtension'] = $this->getImageExtension($imageData);
-                        $imageSaveData['isStored'] = 1;
-                    }
-                }
+            if ($imageData === false) {
+                return [];
             }
+
+            if (!$this->validateImage($imageData)) {
+                return [];
+            }
+
+            $imageSaveData = [
+                'imageUrl' => $unfurlResponse->getImageUrl(),
+                'imageUrlHash' => \sha1($unfurlResponse->getImageUrl()),
+                'width' => $imageData[0],
+                'height' => $imageData[1],
+            ];
+
+            if (!(MODULE_IMAGE_PROXY || IMAGE_ALLOW_EXTERNAL_SOURCE)) {
+                $this->saveImage($imageData, $image, $imageSaveData['imageUrlHash']);
+                $imageSaveData['imageExtension'] = $this->getImageExtension($imageData);
+                $imageSaveData['isStored'] = 1;
+            }
+
+            return $imageSaveData;
         } catch (UrlInaccessible | DownloadFailed $e) {
-            $imageSaveData = [];
+            return [];
         }
 
-        return $imageSaveData;
+        throw new LogicException("Unreachable");
     }
 
     private static function getImageIdByUrl(string $url): ?int
index dff908d0c4ae7a2fbe85200824321c92af46fb64..42f369b98952ce56af87094ef9a83b16a62ecc2a 100644 (file)
@@ -212,13 +212,13 @@ final class UnfurlResponse
             $list = $this->domXPath->query("//meta[@property='og:title']");
             foreach ($list as $node) {
                 /** @var \DOMElement $node */
-                return $node->getAttribute("content");
+                return StringUtil::trim($node->getAttribute("content"));
             }
 
             // title tag
             $list = $this->domXPath->query("//title");
             foreach ($list as $node) {
-                return $node->nodeValue;
+                return StringUtil::trim($node->nodeValue);
             }
         }
 
@@ -235,7 +235,7 @@ final class UnfurlResponse
             $list = $this->domXPath->query("//meta[@property='og:description']");
             foreach ($list as $node) {
                 /** @var \DOMElement $node */
-                return $node->getAttribute("content");
+                return StringUtil::trim($node->getAttribute("content"));
             }
         }