Skip to content

Commit bfcd4d3

Browse files
committed
Insert the avatar into the new file system during import, instead of the wcf1_user_avatar table
1 parent e3c7bb4 commit bfcd4d3

File tree

2 files changed

+70
-57
lines changed

2 files changed

+70
-57
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
namespace wcf\system\importer;
4+
5+
use wcf\data\file\File;
6+
use wcf\data\file\FileEditor;
7+
8+
/**
9+
* Import files.
10+
*
11+
* @author Olaf Braun
12+
* @copyright 2001-2024 WoltLab GmbH
13+
* @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
14+
*/
15+
abstract class AbstractFileImporter extends AbstractImporter
16+
{
17+
/**
18+
* @inheritDoc
19+
*/
20+
protected $className = File::class;
21+
22+
/**
23+
* object type for `com.woltlab.wcf.file`
24+
*/
25+
protected string $objectType;
26+
27+
28+
protected function importFile(string $fileLocation, ?string $filename = null): ?File
29+
{
30+
// check file location
31+
if (!\is_readable($fileLocation)) {
32+
return null;
33+
}
34+
35+
$filename = $filename ?: \basename($fileLocation);
36+
$file = FileEditor::createFromExistingFile($fileLocation, $filename, $this->objectType, true);
37+
38+
if ($file === null) {
39+
return null;
40+
}
41+
42+
if ($this->isValidFile($file)) {
43+
return $file;
44+
}
45+
46+
return null;
47+
}
48+
49+
abstract protected function isValidFile(File $file): bool;
50+
}

wcfsetup/install/files/lib/system/importer/UserAvatarImporter.class.php

Lines changed: 20 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,8 @@
22

33
namespace wcf\system\importer;
44

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;
86
use wcf\system\WCF;
9-
use wcf\util\FileUtil;
10-
use wcf\util\ImageUtil;
117

128
/**
139
* Imports user avatars.
@@ -16,74 +12,41 @@
1612
* @copyright 2001-2019 WoltLab GmbH
1713
* @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
1814
*/
19-
class UserAvatarImporter extends AbstractImporter
15+
class UserAvatarImporter extends AbstractFileImporter
2016
{
2117
/**
2218
* @inheritDoc
2319
*/
24-
protected $className = UserAvatar::class;
20+
protected string $objectType = 'com.woltlab.wcf.user.avatar';
2521

26-
/**
27-
* @inheritDoc
28-
*/
22+
#[\Override]
2923
public function import($oldID, array $data, array $additionalData = [])
3024
{
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-
5125
// get user id
5226
$data['userID'] = ImportHandler::getInstance()->getNewID('com.woltlab.wcf.user', $data['userID']);
5327
if (!$data['userID']) {
5428
return 0;
5529
}
5630

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;
6534
}
6635

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+
]);
7244

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+
}
8647

87-
return 0;
48+
protected function isValidFile(File $file): bool
49+
{
50+
return $file->isImage();
8851
}
8952
}

0 commit comments

Comments
 (0)