Revert "Import the avatar in `UserImporter` and remove `UserAvatarImporter`"
authorCyperghost <olaf_schmitz_1@t-online.de>
Mon, 18 Nov 2024 10:58:24 +0000 (11:58 +0100)
committerCyperghost <olaf_schmitz_1@t-online.de>
Mon, 18 Nov 2024 10:58:24 +0000 (11:58 +0100)
This reverts commit af4056ef995a2fa05a6dadc4ba37df5980295ce1.

com.woltlab.wcf/fileDelete.xml
com.woltlab.wcf/objectType.xml
wcfsetup/install/files/lib/data/file/FileEditor.class.php
wcfsetup/install/files/lib/system/importer/UserAvatarImporter.class.php [new file with mode: 0644]
wcfsetup/install/files/lib/system/importer/UserImporter.class.php

index 937404aa91da4a7e2499374dfb99c68292f1e0a1..2c119ca963875bd3bfcb60b218a49fe3fd2817f5 100644 (file)
                <file>lib/system/html/output/node/HtmlOutputNodeWoltlabSize.class.php</file>
                <file>lib/system/html/output/node/QuoteHtmlOutputNode.class.php</file>
                <file>lib/system/image/Thumbnail.class.php</file>
-               <file>lib/system/importer/UserAvatarImporter.class.php</file>
                <file>lib/system/language/I18nPlural.class.php</file>
                <file>lib/system/language/LanguageServerProcessor.class.php</file>
                <file>lib/system/language/preload/LanguagePreloader.class.php</file>
index 8886287ade9d187db5b6001a1f31ace03ec384f9..567b539407f1a1406b4d3684955afeebb94b133c 100644 (file)
                        <definitionname>com.woltlab.wcf.importer</definitionname>
                        <classname>wcf\system\importer\UserOptionImporter</classname>
                </type>
+               <type>
+                       <name>com.woltlab.wcf.user.avatar</name>
+                       <definitionname>com.woltlab.wcf.importer</definitionname>
+                       <classname>wcf\system\importer\UserAvatarImporter</classname>
+               </type>
                <type>
                        <name>com.woltlab.wcf.user.comment</name>
                        <definitionname>com.woltlab.wcf.importer</definitionname>
                <type name="com.woltlab.wcf.sitemap">
                        <definitionname>com.woltlab.wcf.rebuildData</definitionname>
                </type>
-               <type name="com.woltlab.wcf.user.avatar">
-                       <definitionname>com.woltlab.wcf.importer</definitionname>
-               </type>
        </delete>
 </data>
index d3e5ac3d5783e0d3bbe7e0ebd0855cf239ce1d85..50564a6e8d82923d4f46da81d8e4be936fe42446 100644 (file)
@@ -117,8 +117,7 @@ class FileEditor extends DatabaseObjectEditor
     public static function createFromExistingFile(
         string $pathname,
         string $originalFilename,
-        string $objectTypeName,
-        bool $copy = false
+        string $objectTypeName
     ): ?File {
         if (!\is_readable($pathname)) {
             return null;
@@ -175,11 +174,10 @@ class FileEditor extends DatabaseObjectEditor
             \mkdir($filePath, recursive: true);
         }
 
-        if ($copy) {
-            \copy($pathname, $filePath . $file->getSourceFilename());
-        } else {
-            \rename($pathname, $filePath . $file->getSourceFilename());
-        }
+        \rename(
+            $pathname,
+            $filePath . $file->getSourceFilename()
+        );
 
         return $file;
     }
diff --git a/wcfsetup/install/files/lib/system/importer/UserAvatarImporter.class.php b/wcfsetup/install/files/lib/system/importer/UserAvatarImporter.class.php
new file mode 100644 (file)
index 0000000..c1bcb2a
--- /dev/null
@@ -0,0 +1,89 @@
+<?php
+
+namespace wcf\system\importer;
+
+use wcf\data\user\avatar\UserAvatar;
+use wcf\data\user\avatar\UserAvatarEditor;
+use wcf\system\exception\SystemException;
+use wcf\system\WCF;
+use wcf\util\FileUtil;
+use wcf\util\ImageUtil;
+
+/**
+ * Imports user avatars.
+ *
+ * @author  Marcel Werk
+ * @copyright   2001-2019 WoltLab GmbH
+ * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ */
+class UserAvatarImporter extends AbstractImporter
+{
+    /**
+     * @inheritDoc
+     */
+    protected $className = UserAvatar::class;
+
+    /**
+     * @inheritDoc
+     */
+    public function import($oldID, array $data, array $additionalData = [])
+    {
+        // check file location
+        if (!\is_readable($additionalData['fileLocation'])) {
+            return 0;
+        }
+
+        // get image size
+        $imageData = @\getimagesize($additionalData['fileLocation']);
+        if ($imageData === false) {
+            return 0;
+        }
+        $data['width'] = $imageData[0];
+        $data['height'] = $imageData[1];
+        $data['avatarExtension'] = ImageUtil::getExtensionByMimeType($imageData['mime']);
+        $data['fileHash'] = \sha1_file($additionalData['fileLocation']);
+
+        // check image type
+        if ($imageData[2] != \IMAGETYPE_GIF && $imageData[2] != \IMAGETYPE_JPEG && $imageData[2] != \IMAGETYPE_PNG) {
+            return 0;
+        }
+
+        // get user id
+        $data['userID'] = ImportHandler::getInstance()->getNewID('com.woltlab.wcf.user', $data['userID']);
+        if (!$data['userID']) {
+            return 0;
+        }
+
+        // save avatar
+        $avatar = UserAvatarEditor::create($data);
+
+        // check avatar directory
+        // and create subdirectory if necessary
+        $dir = \dirname($avatar->getLocation());
+        if (!@\file_exists($dir)) {
+            FileUtil::makePath($dir);
+        }
+
+        // copy file
+        try {
+            if (!\copy($additionalData['fileLocation'], $avatar->getLocation())) {
+                throw new SystemException();
+            }
+
+            // update owner
+            $sql = "UPDATE  wcf1_user
+                    SET     avatarID = ?
+                    WHERE   userID = ?";
+            $statement = WCF::getDB()->prepare($sql);
+            $statement->execute([$avatar->avatarID, $data['userID']]);
+
+            return $avatar->avatarID;
+        } catch (SystemException $e) {
+            // copy failed; delete avatar
+            $editor = new UserAvatarEditor($avatar);
+            $editor->delete();
+        }
+
+        return 0;
+    }
+}
index 20b2cf4200bccc8689db1af2d4aca47c2daf398e..7980fc9e456d20207f8b6819c3164003ffee0018 100644 (file)
@@ -2,7 +2,6 @@
 
 namespace wcf\system\importer;
 
-use wcf\data\file\FileEditor;
 use wcf\data\user\group\UserGroup;
 use wcf\data\user\option\UserOption;
 use wcf\data\user\option\UserOptionList;
@@ -202,18 +201,6 @@ class UserImporter extends AbstractImporter
         // assign an interface language
         $data['languageID'] = \reset($languageIDs);
 
-        if (!empty($additionalData['avatarLocation']) && \is_readable($additionalData['avatarLocation'])) {
-            $avatarFile = FileEditor::createFromExistingFile(
-                $additionalData['avatarLocation'],
-                $additionalData['avatarFilename'] ?? \pathinfo($additionalData['avatarLocation'], \PATHINFO_BASENAME),
-                'com.woltlab.wcf.user.avatar',
-                true
-            );
-            if ($avatarFile !== null) {
-                $data['avatarFileID'] = $avatarFile->fileID;
-            }
-        }
-
         // create user
         $user = UserEditor::create($data);
         $userEditor = new UserEditor($user);