Move the old user avatar to the new file storage in the `UserRebuildDataWorker`.
authorCyperghost <olaf_schmitz_1@t-online.de>
Fri, 8 Nov 2024 08:32:50 +0000 (09:32 +0100)
committerCyperghost <olaf_schmitz_1@t-online.de>
Fri, 8 Nov 2024 08:32:50 +0000 (09:32 +0100)
wcfsetup/install/files/lib/system/worker/UserRebuildDataWorker.class.php

index 729f710430322423b1a9b75494f99ce183848cc3..4bab50556f5065e262a0c7671c1a71ae2061cebf 100644 (file)
@@ -2,8 +2,8 @@
 
 namespace wcf\system\worker;
 
+use wcf\data\file\FileEditor;
 use wcf\data\reaction\type\ReactionTypeCache;
-use wcf\data\user\avatar\UserAvatar;
 use wcf\data\user\avatar\UserAvatarEditor;
 use wcf\data\user\avatar\UserAvatarList;
 use wcf\data\user\cover\photo\DefaultUserCoverPhoto;
@@ -15,9 +15,7 @@ use wcf\data\user\UserProfileAction;
 use wcf\data\user\UserProfileList;
 use wcf\system\bbcode\BBCodeHandler;
 use wcf\system\database\util\PreparedStatementConditionBuilder;
-use wcf\system\exception\SystemException;
 use wcf\system\html\input\HtmlInputProcessor;
-use wcf\system\image\ImageHandler;
 use wcf\system\user\storage\UserStorageHandler;
 use wcf\system\WCF;
 
@@ -214,20 +212,14 @@ final class UserRebuildDataWorker extends AbstractLinearRebuildDataWorker
             // update old/imported avatars
             $avatarList = new UserAvatarList();
             $avatarList->getConditionBuilder()->add('user_avatar.userID IN (?)', [$userIDs]);
-            $avatarList->getConditionBuilder()->add(
-                '(
-                    (user_avatar.width <> ? OR user_avatar.height <> ?)
-                    OR (user_avatar.hasWebP = ? AND user_avatar.avatarExtension <> ?)
-                )',
-                [
-                    UserAvatar::AVATAR_SIZE,
-                    UserAvatar::AVATAR_SIZE,
-                    0,
-                    "gif",
-                ]
-            );
             $avatarList->readObjects();
             $resetAvatarCache = [];
+
+            $sql = "UPDATE wcf1_user 
+                    SET    avatarFileID = ?
+                    WHERE  userID = ?";
+            $avatarUpdateStatement = WCF::getDB()->prepare($sql);
+
             foreach ($avatarList as $avatar) {
                 $resetAvatarCache[] = $avatar->userID;
 
@@ -238,49 +230,20 @@ final class UserRebuildDataWorker extends AbstractLinearRebuildDataWorker
                     continue;
                 }
 
-                $width = $avatar->width;
-                $height = $avatar->height;
-                if ($width != $height) {
-                    // make avatar quadratic
-                    $width = $height = \min($width, $height, UserAvatar::AVATAR_SIZE);
-                    $adapter = ImageHandler::getInstance()->getAdapter();
-
-                    try {
-                        $adapter->loadFile($avatar->getLocation());
-                    } catch (SystemException $e) {
-                        // broken image
-                        $editor->delete();
-                        continue;
-                    }
-
-                    $thumbnail = $adapter->createThumbnail($width, $height, false);
-                    $adapter->writeImage($thumbnail, $avatar->getLocation());
-                    // Clear thumbnail as soon as possible to free up the memory.
-                    $thumbnail = null;
-                }
+                $file = FileEditor::createFromExistingFile(
+                    $avatar->getLocation(),
+                    $avatar->avatarName,
+                    'com.woltlab.wcf.user.avatar'
+                );
+                $editor->delete();
 
-                if ($width != UserAvatar::AVATAR_SIZE || $height != UserAvatar::AVATAR_SIZE) {
-                    // resize avatar
-                    $adapter = ImageHandler::getInstance()->getAdapter();
-
-                    try {
-                        $adapter->loadFile($avatar->getLocation());
-                    } catch (SystemException $e) {
-                        // broken image
-                        $editor->delete();
-                        continue;
-                    }
-
-                    $adapter->resize(0, 0, $width, $height, UserAvatar::AVATAR_SIZE, UserAvatar::AVATAR_SIZE);
-                    $adapter->writeImage($adapter->getImage(), $avatar->getLocation());
-                    $width = $height = UserAvatar::AVATAR_SIZE;
+                if ($file === null) {
+                    continue;
                 }
 
-                $editor->createAvatarVariant();
-
-                $editor->update([
-                    'width' => $width,
-                    'height' => $height,
+                $avatarUpdateStatement->execute([
+                    $file->fileID,
+                    $avatar->userID
                 ]);
             }