From e404a3a9afb6f9a098baba1edda65f48e04ad413 Mon Sep 17 00:00:00 2001 From: joshuaruesweg Date: Fri, 12 Mar 2021 14:55:57 +0100 Subject: [PATCH] Use imageUrl as imageHash --- .../database/update_com.woltlab.wcf_5.4.php | 7 ++++--- .../lib/data/unfurl/url/UnfurlUrl.class.php | 8 +++++--- .../job/UnfurlUrlBackgroundJob.class.php | 18 ++++++++---------- wcfsetup/setup/db/install.sql | 5 +++-- 4 files changed, 20 insertions(+), 18 deletions(-) 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 2e1bd84ea9..9ca933b5e4 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 @@ -226,20 +226,21 @@ return [ ObjectIdDatabaseTableColumn::create('imageID'), TextDatabaseTableColumn::create('imageUrl') ->notNull(), - VarcharDatabaseTableColumn::create('imageHash') + VarcharDatabaseTableColumn::create('imageUrlHash') ->notNull() ->length(40), NotNullInt10DatabaseTableColumn::create('width'), NotNullInt10DatabaseTableColumn::create('height'), VarcharDatabaseTableColumn::create('imageExtension') ->length(4), + DefaultFalseBooleanDatabaseTableColumn::create('isStored') ]) ->indices([ DatabaseTablePrimaryIndex::create() ->columns(['imageID']), - DatabaseTableIndex::create('imageHash') + DatabaseTableIndex::create('imageUrlHash') ->type(DatabaseTableIndex::UNIQUE_TYPE) - ->columns(['imageHash']), + ->columns(['imageUrlHash']), ]), DatabaseTable::create('wcf1_unfurl_url') diff --git a/wcfsetup/install/files/lib/data/unfurl/url/UnfurlUrl.class.php b/wcfsetup/install/files/lib/data/unfurl/url/UnfurlUrl.class.php index b06db35058..e3c8ebc6df 100644 --- a/wcfsetup/install/files/lib/data/unfurl/url/UnfurlUrl.class.php +++ b/wcfsetup/install/files/lib/data/unfurl/url/UnfurlUrl.class.php @@ -25,11 +25,13 @@ use wcf\util\Url; * @property-read string $description * @property-read string $imageHash * @property-read string $imageUrl + * @property-read string $imageUrlHash * @property-read string $imageExtension * @property-read int $width * @property-read int $height * @property-read int $lastFetch * @property-read int $imageID + * @property-read int $isStored */ class UnfurlUrl extends DatabaseObject { @@ -100,9 +102,9 @@ class UnfurlUrl extends DatabaseObject */ public function getImageUrl(): ?string { - if (!empty($this->imageHash)) { - $imageFolder = self::IMAGE_DIR . \substr($this->imageHash, 0, 2) . "/"; - $imageName = $this->imageHash . '.' . $this->imageExtension; + if ($this->isStored) { + $imageFolder = self::IMAGE_DIR . \substr($this->imageUrlHash, 0, 2) . "/"; + $imageName = $this->imageUrlHash . '.' . $this->imageExtension; return WCF::getPath() . $imageFolder . $imageName; } elseif (!empty($this->imageUrl)) { 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 b1a114f665..c549825afe 100644 --- a/wcfsetup/install/files/lib/system/background/job/UnfurlUrlBackgroundJob.class.php +++ b/wcfsetup/install/files/lib/system/background/job/UnfurlUrlBackgroundJob.class.php @@ -131,12 +131,14 @@ final class UnfurlUrlBackgroundJob extends AbstractBackgroundJob if ($imageData !== false) { if ($this->validateImage($imageData)) { - $imageSaveData['imageUrl'] = StringUtil::trim($unfurlResponse->getImageUrl()); + $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)) { - $imageSaveData['imageHash'] = $this->saveImage($imageData, $image); + $this->saveImage($imageData, $image, $imageSaveData['imageUrlHash']); $imageSaveData['imageExtension'] = $this->getImageExtension($imageData); + $imageSaveData['isStored'] = 1; } } } @@ -151,9 +153,9 @@ final class UnfurlUrlBackgroundJob extends AbstractBackgroundJob { $sql = "SELECT imageID FROM wcf" . WCF_N . "_unfurl_url_image - WHERE imageUrl = ?"; + WHERE imageUrlHash = ?"; $statement = WCF::getDB()->prepareStatement($sql); - $statement->execute([$url]); + $statement->execute([\sha1($url)]); $imageID = $statement->fetchSingleColumn(); @@ -199,13 +201,9 @@ final class UnfurlUrlBackgroundJob extends AbstractBackgroundJob return true; } - private function saveImage(array $imageData, string $image): string + private function saveImage(array $imageData, string $image, string $imageHash): string { - do { - $imageHash = StringUtil::getRandomID(); - - $path = WCF_DIR . UnfurlUrl::IMAGE_DIR . \substr($imageHash, 0, 2) . '/'; - } while (!empty(glob($path . $imageHash . '.*'))); + $path = WCF_DIR . UnfurlUrl::IMAGE_DIR . \substr($imageHash, 0, 2) . '/'; FileUtil::makePath($path); diff --git a/wcfsetup/setup/db/install.sql b/wcfsetup/setup/db/install.sql index bf04c662c2..ef4ed4a4fe 100644 --- a/wcfsetup/setup/db/install.sql +++ b/wcfsetup/setup/db/install.sql @@ -1464,12 +1464,13 @@ DROP TABLE IF EXISTS wcf1_unfurl_url_image; CREATE TABLE wcf1_unfurl_url_image ( imageID INT(10) NOT NULL AUTO_INCREMENT PRIMARY KEY, imageUrl TEXT NOT NULL, - imageHash VARCHAR(40) DEFAULT NULL, + imageUrlHash VARCHAR(40) DEFAULT NULL, width INT(10) NOT NULL, height INT(10) NOT NULL, imageExtension VARCHAR(4) DEFAULT NULL, + isStored TINYINT(1) NOT NULL DEFAULT 0, - UNIQUE KEY imageHash (imageHash) + UNIQUE KEY imageUrlHash (imageUrlHash) ); DROP TABLE IF EXISTS wcf1_user; -- 2.20.1