Properly migrate from new owncloud avatar location

as the files are not scanned we cannot use the OCP\Files api

Signed-off-by: Julius Härtl <jus@bitgrid.net>
This commit is contained in:
Julius Härtl 2020-12-09 12:04:14 +01:00
parent 7c3d3c58d3
commit 368dac3ba3
No known key found for this signature in database
GPG Key ID: 4C614C6ED2CDE6DF
1 changed files with 43 additions and 74 deletions

View File

@ -25,38 +25,36 @@
namespace OC\Repair\Owncloud; namespace OC\Repair\Owncloud;
use OC\BackgroundJob\QueuedJob; use OC\BackgroundJob\QueuedJob;
use OCP\Files\File;
use OCP\Files\Folder;
use OCP\Files\IAppData;
use OCP\Files\IRootFolder; use OCP\Files\IRootFolder;
use OCP\Files\NotFoundException; use OCP\Files\NotFoundException;
use OCP\Files\SimpleFS\ISimpleFolder; use OCP\Files\Storage;
use OCP\ILogger; use OCP\IAvatarManager;
use OCP\IUser; use OCP\IUser;
use OCP\IUserManager; use OCP\IUserManager;
use Psr\Log\LoggerInterface;
class MoveAvatarsBackgroundJob extends QueuedJob { class MoveAvatarsBackgroundJob extends QueuedJob {
/** @var IUserManager */ /** @var IUserManager */
private $userManager; private $userManager;
/** @var IRootFolder */ /** @var LoggerInterface */
private $rootFolder;
/** @var IAppData */
private $appData;
/** @var ILogger */
private $logger; private $logger;
/** /** @var IAvatarManager */
* MoveAvatars constructor. private $avatarManager;
*/
public function __construct() { /** @var Storage */
$this->userManager = \OC::$server->getUserManager(); private $owncloudAvatarStorage;
$this->rootFolder = \OC::$server->getRootFolder();
$this->logger = \OC::$server->getLogger(); public function __construct(IUserManager $userManager, LoggerInterface $logger, IAvatarManager $avatarManager, IRootFolder $rootFolder) {
$this->appData = \OC::$server->getAppDataDir('avatar'); $this->userManager = $userManager;
$this->logger = $logger;
$this->avatarManager = $avatarManager;
try {
$this->owncloudAvatarStorage = $rootFolder->get('avatars')->getStorage();
} catch (\Exception $e) {
}
} }
public function run($arguments) { public function run($arguments) {
@ -65,43 +63,31 @@ class MoveAvatarsBackgroundJob extends QueuedJob {
$this->logger->info('All avatars migrated to AppData folder'); $this->logger->info('All avatars migrated to AppData folder');
} }
private function moveAvatars() { private function moveAvatars(): void {
try { if (!$this->owncloudAvatarStorage) {
$ownCloudAvatars = $this->rootFolder->get('avatars');
} catch (NotFoundException $e) {
$this->logger->info('No legacy avatars available, skipping migration'); $this->logger->info('No legacy avatars available, skipping migration');
return; return;
} }
$counter = 0; $counter = 0;
$this->userManager->callForSeenUsers(function (IUser $user) use ($counter, $ownCloudAvatars) { $this->userManager->callForSeenUsers(function (IUser $user) use ($counter) {
$uid = $user->getUID(); $uid = $user->getUID();
\OC\Files\Filesystem::initMountPoints($uid); $path = 'avatars/' . $this->buildOwnCloudAvatarPath($uid);
/** @var Folder $userFolder */ $avatar = $this->avatarManager->getAvatar($uid);
$userFolder = $this->rootFolder->get($uid);
try { try {
$userData = $this->appData->getFolder($uid); $avatarPath = $path . '/avatar.' . $this->getExtension($path);
} catch (NotFoundException $e) { $resource = $this->owncloudAvatarStorage->fopen($avatarPath, 'r');
$userData = $this->appData->newFolder($uid); if ($resource) {
} $avatar->set($resource);
fclose($resource);
$foundAvatars = $this->copyAvatarsFromFolder($userFolder, $userData); } else {
throw new \Exception('Failed to open old avatar file for reading');
// ownCloud migration?
if ($foundAvatars === 0 && $ownCloudAvatars instanceof Folder) {
$parts = $this->buildOwnCloudAvatarPath($uid);
$userOwnCloudAvatar = $ownCloudAvatars;
foreach ($parts as $part) {
try {
$userOwnCloudAvatar = $userOwnCloudAvatar->get($part);
} catch (NotFoundException $e) {
return;
}
} }
} catch (NotFoundException $e) {
$this->copyAvatarsFromFolder($userOwnCloudAvatar, $userData); // In case there is no avatar we can just skip
} catch (\Throwable $e) {
$this->logger->error('Failed to migrate avatar for user ' . $uid, ['exception' => $e]);
} }
$counter++; $counter++;
@ -112,36 +98,19 @@ class MoveAvatarsBackgroundJob extends QueuedJob {
} }
/** /**
* @param Folder $source
* @param ISimpleFolder $target
* @return int
* @throws \OCP\Files\NotPermittedException
* @throws NotFoundException * @throws NotFoundException
*/ */
protected function copyAvatarsFromFolder(Folder $source, ISimpleFolder $target) { private function getExtension(string $path): string {
$foundAvatars = 0; if ($this->owncloudAvatarStorage->file_exists("{$path}/avatar.jpg")) {
$avatars = $source->getDirectoryListing(); return 'jpg';
$regex = '/^avatar\.([0-9]+\.)?(jpg|png)$/';
foreach ($avatars as $avatar) {
/** @var File $avatar */
if (preg_match($regex, $avatar->getName())) {
/*
* This is not the most effective but it is the most abstract way
* to handle this. Avatars should be small anyways.
*/
$newAvatar = $target->newFile($avatar->getName());
$newAvatar->putContent($avatar->getContent());
$avatar->delete();
$foundAvatars++;
}
} }
if ($this->owncloudAvatarStorage->file_exists("{$path}/avatar.png")) {
return $foundAvatars; return 'png';
}
throw new NotFoundException("{$path}/avatar.jpg|png");
} }
protected function buildOwnCloudAvatarPath($userId) { protected function buildOwnCloudAvatarPath(string $userId): string {
$avatar = substr_replace(substr_replace(md5($userId), '/', 4, 0), '/', 2, 0); return substr_replace(substr_replace(md5($userId), '/', 4, 0), '/', 2, 0);
return explode('/', $avatar);
} }
} }