From: joshuaruesweg Date: Fri, 12 Mar 2021 15:04:50 +0000 (+0100) Subject: Clean up code and unify code style X-Git-Tag: 5.4.0_Alpha_1~146^2~3 X-Git-Url: https://git.stricted.de/?a=commitdiff_plain;h=4fef9c78bff998565469e1094a26894e437f3e57;p=GitHub%2FWoltLab%2FWCF.git Clean up code and unify code style --- diff --git a/wcfsetup/install/files/acp/database/update_com.woltlab.wcf_5.4.php b/wcfsetup/install/files/acp/database/update_com.woltlab.wcf_5.4.php index 9ca933b5e4..5e5dcf1801 100644 --- a/wcfsetup/install/files/acp/database/update_com.woltlab.wcf_5.4.php +++ b/wcfsetup/install/files/acp/database/update_com.woltlab.wcf_5.4.php @@ -233,7 +233,7 @@ return [ NotNullInt10DatabaseTableColumn::create('height'), VarcharDatabaseTableColumn::create('imageExtension') ->length(4), - DefaultFalseBooleanDatabaseTableColumn::create('isStored') + DefaultFalseBooleanDatabaseTableColumn::create('isStored'), ]) ->indices([ DatabaseTablePrimaryIndex::create() diff --git a/wcfsetup/install/files/lib/system/background/job/UnfurlUrlBackgroundJob.class.php b/wcfsetup/install/files/lib/system/background/job/UnfurlUrlBackgroundJob.class.php index c549825afe..121e75269b 100644 --- a/wcfsetup/install/files/lib/system/background/job/UnfurlUrlBackgroundJob.class.php +++ b/wcfsetup/install/files/lib/system/background/job/UnfurlUrlBackgroundJob.class.php @@ -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 diff --git a/wcfsetup/install/files/lib/system/message/unfurl/UnfurlResponse.class.php b/wcfsetup/install/files/lib/system/message/unfurl/UnfurlResponse.class.php index dff908d0c4..42f369b989 100644 --- a/wcfsetup/install/files/lib/system/message/unfurl/UnfurlResponse.class.php +++ b/wcfsetup/install/files/lib/system/message/unfurl/UnfurlResponse.class.php @@ -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")); } }