Use imageUrl as imageHash
authorjoshuaruesweg <ruesweg@woltlab.com>
Fri, 12 Mar 2021 13:55:57 +0000 (14:55 +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/data/unfurl/url/UnfurlUrl.class.php
wcfsetup/install/files/lib/system/background/job/UnfurlUrlBackgroundJob.class.php
wcfsetup/setup/db/install.sql

index 2e1bd84ea9ba7efb4fde9d13e20bfe4a6876a0e6..9ca933b5e40a95d531a0830a572b9182c5b08df0 100644 (file)
@@ -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')
index b06db350585f5258ac1db979feeb6e69210cb32a..e3c8ebc6dfc90b6c4a247a02f06a08e8df52cd2a 100644 (file)
@@ -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)) {
index b1a114f665fc044e625fa3c997f64ecdbba60b88..c549825afe5aed6d12680cee028bb601ae7faacd 100644 (file)
@@ -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);
 
index bf04c662c22f7de3d6357b48675aaa192508f3a4..ef4ed4a4feec4cace34f232e9454ea711fb2b234 100644 (file)
@@ -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;