From af4056ef995a2fa05a6dadc4ba37df5980295ce1 Mon Sep 17 00:00:00 2001 From: Cyperghost Date: Mon, 11 Nov 2024 09:51:48 +0100 Subject: [PATCH] Import the avatar in `UserImporter` and remove `UserAvatarImporter` --- com.woltlab.wcf/fileDelete.xml | 1 + com.woltlab.wcf/objectType.xml | 8 +- .../files/lib/data/file/FileEditor.class.php | 12 +-- .../importer/UserAvatarImporter.class.php | 89 ------------------- .../system/importer/UserImporter.class.php | 13 +++ 5 files changed, 24 insertions(+), 99 deletions(-) delete mode 100644 wcfsetup/install/files/lib/system/importer/UserAvatarImporter.class.php diff --git a/com.woltlab.wcf/fileDelete.xml b/com.woltlab.wcf/fileDelete.xml index 2c119ca963..937404aa91 100644 --- a/com.woltlab.wcf/fileDelete.xml +++ b/com.woltlab.wcf/fileDelete.xml @@ -2771,6 +2771,7 @@ lib/system/html/output/node/HtmlOutputNodeWoltlabSize.class.php lib/system/html/output/node/QuoteHtmlOutputNode.class.php lib/system/image/Thumbnail.class.php + lib/system/importer/UserAvatarImporter.class.php lib/system/language/I18nPlural.class.php lib/system/language/LanguageServerProcessor.class.php lib/system/language/preload/LanguagePreloader.class.php diff --git a/com.woltlab.wcf/objectType.xml b/com.woltlab.wcf/objectType.xml index 567b539407..8886287ade 100644 --- a/com.woltlab.wcf/objectType.xml +++ b/com.woltlab.wcf/objectType.xml @@ -432,11 +432,6 @@ com.woltlab.wcf.importer wcf\system\importer\UserOptionImporter - - com.woltlab.wcf.user.avatar - com.woltlab.wcf.importer - wcf\system\importer\UserAvatarImporter - com.woltlab.wcf.user.comment com.woltlab.wcf.importer @@ -1804,5 +1799,8 @@ com.woltlab.wcf.rebuildData + + com.woltlab.wcf.importer + diff --git a/wcfsetup/install/files/lib/data/file/FileEditor.class.php b/wcfsetup/install/files/lib/data/file/FileEditor.class.php index 50564a6e8d..d3e5ac3d57 100644 --- a/wcfsetup/install/files/lib/data/file/FileEditor.class.php +++ b/wcfsetup/install/files/lib/data/file/FileEditor.class.php @@ -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 index c1bcb2ace0..0000000000 --- a/wcfsetup/install/files/lib/system/importer/UserAvatarImporter.class.php +++ /dev/null @@ -1,89 +0,0 @@ - - */ -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; - } -} diff --git a/wcfsetup/install/files/lib/system/importer/UserImporter.class.php b/wcfsetup/install/files/lib/system/importer/UserImporter.class.php index 7980fc9e45..20b2cf4200 100644 --- a/wcfsetup/install/files/lib/system/importer/UserImporter.class.php +++ b/wcfsetup/install/files/lib/system/importer/UserImporter.class.php @@ -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); -- 2.20.1