Skip to content

Commit

Permalink
Repair step to move avatars to the new location
Browse files Browse the repository at this point in the history
  • Loading branch information
Vincent Petry committed Aug 19, 2016
1 parent c54e5c6 commit e684a76
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/private/AvatarManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public function getAvatar($userId) {
throw new \Exception('user does not exist');
}

$avatarsFolder = $this->getFolder($this->rootFolder, '.avatars');
$avatarsFolder = $this->getFolder($this->rootFolder, 'metadata-avatars');
$usersAvatarsFolder = $this->getFolder($avatarsFolder, $userId);

return new Avatar($usersAvatarsFolder, $this->l, $user, $this->logger);
Expand Down
2 changes: 2 additions & 0 deletions lib/private/Encryption/Util.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ public function __construct(
$this->config = $config;

$this->excludedPaths[] = 'files_encryption';
$this->excludedPaths[] = 'avatar.png';
$this->excludedPaths[] = 'avatar.jpg';
}

/**
Expand Down
6 changes: 6 additions & 0 deletions lib/private/Repair.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
use OCP\Migration\IRepairStep;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\EventDispatcher\GenericEvent;
use OC\Repair\MoveAvatarOutsideHome;

class Repair implements IOutput{
/* @var IRepairStep[] */
Expand Down Expand Up @@ -138,6 +139,11 @@ public static function getRepairSteps() {
new SharePropagation(\OC::$server->getConfig()),
new RemoveOldShares(\OC::$server->getDatabaseConnection()),
new AvatarPermissions(\OC::$server->getDatabaseConnection()),
new MoveAvatarOutsideHome(
\OC::$server->getConfig(),
\OC::$server->getDatabaseConnection(),
\OC::$server->getUserManager()
),
new RemoveRootShares(\OC::$server->getDatabaseConnection(), \OC::$server->getUserManager(), \OC::$server->getLazyRootFolder()),
new RepairUnmergedShares(
\OC::$server->getConfig(),
Expand Down
132 changes: 132 additions & 0 deletions lib/private/Repair/MoveAvatarOutsideHome.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
<?php
/**
* @author Vincent Petry <pvince81@owncloud.com>
*
* @copyright Copyright (c) 2016, ownCloud, Inc.
* @license AGPL-3.0
*
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License, version 3,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/
namespace OC\Repair;

use OCP\IDBConnection;
use OCP\Migration\IOutput;
use OCP\Migration\IRepairStep;
use OCP\IUser;
use OC\Avatar;
use OCP\IConfig;
use OCP\Files\Folder;

/**
* Move avatars outside of their homes to the new location
*
* @package OC\Repair
*/
class MoveAvatarOutsideHome implements IRepairStep {
/** @var \OCP\IConfig */
protected $config;

/** @var IDBConnection */
private $connection;

/** @var IUserManager */
private $userManager;

/**
* @param IConfig $config config
* @param IDBConnection $connection database connection
* @param IUserManager $userManager user manager
*/
public function __construct(
IConfig $config,
IDBConnection $connection,
IUserManager $userManager
) {
$this->config = $config;
$this->connection = $connection;
$this->userManager = $userManager;
}

/**
* @return string
*/
public function getName() {
return 'Move user avatars outside the homes to the new location';
}

/**
* Move avatars outside of their homes
*/
private function moveAvatars(IOutput $out, IUser $user, Folder $newAvatarsFolder) {
$userId = $user->getUID();

\OC\Files\Filesystem::initMountPoints($userId);

// TODO: inject
$rootFolder = \OC::$server->getRootFolder();

$oldAvatarUserFolder = $rootFolder->get('/' . $userId);
$oldAvatar = new Avatar($oldAvatarUserFolder, $this->l, $user, $this->logger);
if ($oldAvatar->exists()) {
$newAvatarsUserFolder = $newAvatarsFolder->newFolder($userId);

// get original file
$oldAvatarFile = $oldAvatar->getFile(-1);
$oldAvatarFile->move($newAvatarsUserFolder->getPath() . '/' . $oldAvatarFile->getName());
$oldAvatar->remove();
}

\OC_Util::tearDownFS();
}

/**
* Count all the users
*
* @return int
*/
private function countUsers() {
$allCount = $this->userManager->countUsers();

$totalCount = 0;
foreach ($allCount as $backend => $count) {
$totalCount += $count;
}

return $totalCount;
}

/**
* @param IOutput $output
*/
public function run(IOutput $output) {
$ocVersionFromBeforeUpdate = $this->config->getSystemValue('version', '0.0.0');
if (version_compare($ocVersionFromBeforeUpdate, '9.1.1.1', '<')) {
$rootFolder = \OC::$server->getRootFolder();
$newAvatarsFolder = $rootFolder->newFolder('metadata-avatars');

$function = function(IUser $user) use ($output, $newAvatarsFolder) {
$this->moveAvatars($output, $user, $newAvatarsFolder);
$output->advance();
};

$userCount = $this->countUsers();
$output->startProgress($userCount);

$this->userManager->callForAllUsers($function);

$output->finishProgress();
}
}
}

2 changes: 1 addition & 1 deletion version.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
// We only can count up. The 4. digit is only for the internal patchlevel to trigger DB upgrades
// between betas, final and RCs. This is _not_ the public version number. Reset minor/patchlevel
// when updating major/minor version number.
$OC_Version = array(9, 1, 1, 0);
$OC_Version = array(9, 1, 1, 1);

// The human readable string
$OC_VersionString = '9.1.1 RC1';
Expand Down

0 comments on commit e684a76

Please sign in to comment.