|
2 | 2 |
|
3 | 3 | namespace wcf\system\importer; |
4 | 4 |
|
5 | | -use wcf\data\user\avatar\UserAvatar; |
6 | | -use wcf\data\user\avatar\UserAvatarEditor; |
7 | | -use wcf\system\exception\SystemException; |
| 5 | +use wcf\data\file\File; |
8 | 6 | use wcf\system\WCF; |
9 | | -use wcf\util\FileUtil; |
10 | | -use wcf\util\ImageUtil; |
11 | 7 |
|
12 | 8 | /** |
13 | 9 | * Imports user avatars. |
|
16 | 12 | * @copyright 2001-2019 WoltLab GmbH |
17 | 13 | * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php> |
18 | 14 | */ |
19 | | -class UserAvatarImporter extends AbstractImporter |
| 15 | +class UserAvatarImporter extends AbstractFileImporter |
20 | 16 | { |
21 | 17 | /** |
22 | 18 | * @inheritDoc |
23 | 19 | */ |
24 | | - protected $className = UserAvatar::class; |
| 20 | + protected string $objectType = 'com.woltlab.wcf.user.avatar'; |
25 | 21 |
|
26 | | - /** |
27 | | - * @inheritDoc |
28 | | - */ |
| 22 | + #[\Override] |
29 | 23 | public function import($oldID, array $data, array $additionalData = []) |
30 | 24 | { |
31 | | - // check file location |
32 | | - if (!\is_readable($additionalData['fileLocation'])) { |
33 | | - return 0; |
34 | | - } |
35 | | - |
36 | | - // get image size |
37 | | - $imageData = @\getimagesize($additionalData['fileLocation']); |
38 | | - if ($imageData === false) { |
39 | | - return 0; |
40 | | - } |
41 | | - $data['width'] = $imageData[0]; |
42 | | - $data['height'] = $imageData[1]; |
43 | | - $data['avatarExtension'] = ImageUtil::getExtensionByMimeType($imageData['mime']); |
44 | | - $data['fileHash'] = \sha1_file($additionalData['fileLocation']); |
45 | | - |
46 | | - // check image type |
47 | | - if ($imageData[2] != \IMAGETYPE_GIF && $imageData[2] != \IMAGETYPE_JPEG && $imageData[2] != \IMAGETYPE_PNG) { |
48 | | - return 0; |
49 | | - } |
50 | | - |
51 | 25 | // get user id |
52 | 26 | $data['userID'] = ImportHandler::getInstance()->getNewID('com.woltlab.wcf.user', $data['userID']); |
53 | 27 | if (!$data['userID']) { |
54 | 28 | return 0; |
55 | 29 | } |
56 | 30 |
|
57 | | - // save avatar |
58 | | - $avatar = UserAvatarEditor::create($data); |
59 | | - |
60 | | - // check avatar directory |
61 | | - // and create subdirectory if necessary |
62 | | - $dir = \dirname($avatar->getLocation()); |
63 | | - if (!@\file_exists($dir)) { |
64 | | - FileUtil::makePath($dir); |
| 31 | + $file = $this->importFile($additionalData['fileLocation'], $data['avatarName']); |
| 32 | + if ($file === null) { |
| 33 | + return 0; |
65 | 34 | } |
66 | 35 |
|
67 | | - // copy file |
68 | | - try { |
69 | | - if (!\copy($additionalData['fileLocation'], $avatar->getLocation())) { |
70 | | - throw new SystemException(); |
71 | | - } |
| 36 | + $sql = "UPDATE wcf1_user |
| 37 | + SET avatarFileID = ? |
| 38 | + WHERE userID = ?"; |
| 39 | + $statement = WCF::getDB()->prepare($sql); |
| 40 | + $statement->execute([ |
| 41 | + $file->fileID, |
| 42 | + $data['userID'] |
| 43 | + ]); |
72 | 44 |
|
73 | | - // update owner |
74 | | - $sql = "UPDATE wcf1_user |
75 | | - SET avatarID = ? |
76 | | - WHERE userID = ?"; |
77 | | - $statement = WCF::getDB()->prepare($sql); |
78 | | - $statement->execute([$avatar->avatarID, $data['userID']]); |
79 | | - |
80 | | - return $avatar->avatarID; |
81 | | - } catch (SystemException $e) { |
82 | | - // copy failed; delete avatar |
83 | | - $editor = new UserAvatarEditor($avatar); |
84 | | - $editor->delete(); |
85 | | - } |
| 45 | + return $file->fileID; |
| 46 | + } |
86 | 47 |
|
87 | | - return 0; |
| 48 | + protected function isValidFile(File $file): bool |
| 49 | + { |
| 50 | + return $file->isImage(); |
88 | 51 | } |
89 | 52 | } |
0 commit comments