Skip to content

Commit

Permalink
Insert the avatar into the new file system during import, instead of …
Browse files Browse the repository at this point in the history
…the `wcf1_user_avatar` table
  • Loading branch information
Cyperghost committed Nov 18, 2024
1 parent e3c7bb4 commit bfcd4d3
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 57 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace wcf\system\importer;

use wcf\data\file\File;
use wcf\data\file\FileEditor;

/**
* Import files.
*
* @author Olaf Braun
* @copyright 2001-2024 WoltLab GmbH
* @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
*/
abstract class AbstractFileImporter extends AbstractImporter
{
/**
* @inheritDoc
*/
protected $className = File::class;

/**
* object type for `com.woltlab.wcf.file`
*/
protected string $objectType;


protected function importFile(string $fileLocation, ?string $filename = null): ?File
{
// check file location
if (!\is_readable($fileLocation)) {
return null;
}

$filename = $filename ?: \basename($fileLocation);
$file = FileEditor::createFromExistingFile($fileLocation, $filename, $this->objectType, true);

if ($file === null) {
return null;
}

if ($this->isValidFile($file)) {
return $file;
}

return null;
}

abstract protected function isValidFile(File $file): bool;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,8 @@

namespace wcf\system\importer;

use wcf\data\user\avatar\UserAvatar;
use wcf\data\user\avatar\UserAvatarEditor;
use wcf\system\exception\SystemException;
use wcf\data\file\File;
use wcf\system\WCF;
use wcf\util\FileUtil;
use wcf\util\ImageUtil;

/**
* Imports user avatars.
Expand All @@ -16,74 +12,41 @@
* @copyright 2001-2019 WoltLab GmbH
* @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
*/
class UserAvatarImporter extends AbstractImporter
class UserAvatarImporter extends AbstractFileImporter
{
/**
* @inheritDoc
*/
protected $className = UserAvatar::class;
protected string $objectType = 'com.woltlab.wcf.user.avatar';

/**
* @inheritDoc
*/
#[\Override]
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);
$file = $this->importFile($additionalData['fileLocation'], $data['avatarName']);
if ($file === null) {
return 0;
}

// copy file
try {
if (!\copy($additionalData['fileLocation'], $avatar->getLocation())) {
throw new SystemException();
}
$sql = "UPDATE wcf1_user
SET avatarFileID = ?
WHERE userID = ?";
$statement = WCF::getDB()->prepare($sql);
$statement->execute([
$file->fileID,
$data['userID']
]);

// 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 $file->fileID;
}

return 0;
protected function isValidFile(File $file): bool
{
return $file->isImage();
}
}

0 comments on commit bfcd4d3

Please sign in to comment.