Import the avatar in `UserImporter` and remove `UserAvatarImporter`
authorCyperghost <olaf_schmitz_1@t-online.de>
Mon, 11 Nov 2024 08:51:48 +0000 (09:51 +0100)
committerCyperghost <olaf_schmitz_1@t-online.de>
Mon, 11 Nov 2024 08:51:48 +0000 (09:51 +0100)
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 [deleted file]
wcfsetup/install/files/lib/system/importer/UserImporter.class.php

index 2c119ca963875bd3bfcb60b218a49fe3fd2817f5..937404aa91da4a7e2499374dfb99c68292f1e0a1 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 567b539407f1a1406b4d3684955afeebb94b133c..8886287ade9d187db5b6001a1f31ace03ec384f9 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 50564a6e8d82923d4f46da81d8e4be936fe42446..d3e5ac3d5783e0d3bbe7e0ebd0855cf239ce1d85 100644 (file)
@@ -117,7 +117,8 @@ class FileEditor extends DatabaseObjectEditor
     public static function createFromExistingFile(
         string $pathname,
         string $originalFilename,
-        string $objectTypeName
+        string $objectTypeName,
+        bool $copy = false
     ): ?File {
         if (!\is_readable($pathname)) {
             return null;
@@ -174,10 +175,11 @@ class FileEditor extends DatabaseObjectEditor
             \mkdir($filePath, recursive: true);
         }
 
-        \rename(
-            $pathname,
-            $filePath . $file->getSourceFilename()
-        );
+        if ($copy) {
+            \copy($pathname, $filePath . $file->getSourceFilename());
+        } else {
+            \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
deleted file mode 100644 (file)
index c1bcb2a..0000000
+++ /dev/null
@@ -1,89 +0,0 @@
-<?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 7980fc9e456d20207f8b6819c3164003ffee0018..20b2cf4200bccc8689db1af2d4aca47c2daf398e 100644 (file)
@@ -2,6 +2,7 @@
 
 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;
@@ -201,6 +202,18 @@ 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);