Skip to content

Commit

Permalink
Merge pull request #23919 from owncloud/cyclyc-share-dep-example
Browse files Browse the repository at this point in the history
SharedStorage to new sharing code + cleanup
  • Loading branch information
DeepDiver1975 committed Apr 20, 2016
2 parents cdcabbd + b53d659 commit cd01c44
Show file tree
Hide file tree
Showing 37 changed files with 1,083 additions and 1,244 deletions.
2 changes: 1 addition & 1 deletion apps/dav/lib/connector/sabre/node.php
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ public function getSharePermissions() {

if ($storage->instanceOfStorage('\OC\Files\Storage\Shared')) {
/** @var \OC\Files\Storage\Shared $storage */
$permissions = (int)$storage->getShare()['permissions'];
$permissions = (int)$storage->getShare()->getPermissions();
} else {
$permissions = $storage->getPermissions($path);
}
Expand Down
3 changes: 2 additions & 1 deletion apps/files_sharing/appinfo/application.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@ public function __construct(array $urlParams = array()) {
/** @var \OCP\IServerContainer $server */
$server = $c->query('ServerContainer');
return new MountProvider(
$server->getConfig()
$server->getConfig(),
$server->getShareManager()
);
});

Expand Down
25 changes: 17 additions & 8 deletions apps/files_sharing/lib/mountprovider.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,30 @@

namespace OCA\Files_Sharing;

use OC\Files\Filesystem;
use OC\User\NoUserException;
use OCP\Files\Config\IMountProvider;
use OCP\Files\Storage\IStorageFactory;
use OCP\IConfig;
use OCP\IUser;
use OCP\Share\IManager;

class MountProvider implements IMountProvider {
/**
* @var \OCP\IConfig
*/
protected $config;

/**
* @var IManager
*/
protected $shareManager;

/**
* @param \OCP\IConfig $config
* @param IManager $shareManager
*/
public function __construct(IConfig $config) {
public function __construct(IConfig $config, IManager $shareManager) {
$this->config = $config;
$this->shareManager = $shareManager;
}


Expand All @@ -51,18 +57,21 @@ public function __construct(IConfig $config) {
* @return \OCP\Files\Mount\IMountPoint[]
*/
public function getMountsForUser(IUser $user, IStorageFactory $storageFactory) {
$shares = \OCP\Share::getItemsSharedWithUser('file', $user->getUID());
$shares = array_filter($shares, function ($share) {
return $share['permissions'] > 0;
$shares = $this->shareManager->getSharedWith($user->getUID(), \OCP\Share::SHARE_TYPE_USER, null, -1);
$shares = array_merge($shares, $this->shareManager->getSharedWith($user->getUID(), \OCP\Share::SHARE_TYPE_GROUP, null, -1));
$shares = array_filter($shares, function (\OCP\Share\IShare $share) {
return $share->getPermissions() > 0;
});

$mounts = [];
foreach ($shares as $share) {

$mounts[] = new SharedMount(
'\OC\Files\Storage\Shared',
$mounts,
[
'share' => $share,
'user' => $user->getUID()
'user' => $user->getUID(),
'newShare' => $share,
],
$storageFactory
);
Expand Down
6 changes: 4 additions & 2 deletions apps/files_sharing/lib/scanner.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,10 @@ class SharedScanner extends Scanner {
*/
public function getData($path) {
$data = parent::getData($path);
$sourcePath = $this->storage->getSourcePath($path);
list($sourceStorage, $internalPath) = \OC\Files\Filesystem::resolvePath($sourcePath);
if ($data === null) {
return null;
}
list($sourceStorage, $internalPath) = $this->storage->resolvePath($path);
$data['permissions'] = $sourceStorage->getPermissions($internalPath);
return $data;
}
Expand Down
97 changes: 39 additions & 58 deletions apps/files_sharing/lib/sharedmount.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ class SharedMount extends MountPoint implements MoveableMount {
*/
private $user;

/** @var \OCP\Share\IShare */
private $share;

/**
* @param string $storage
* @param SharedMount[] $mountpoints
Expand All @@ -58,23 +61,24 @@ class SharedMount extends MountPoint implements MoveableMount {
public function __construct($storage, array $mountpoints, $arguments = null, $loader = null) {
$this->user = $arguments['user'];
$this->recipientView = new View('/' . $this->user . '/files');
$newMountPoint = $this->verifyMountPoint($arguments['share'], $mountpoints);
$this->share = $arguments['newShare'];
$newMountPoint = $this->verifyMountPoint($this->share, $mountpoints);
$absMountPoint = '/' . $this->user . '/files' . $newMountPoint;
$arguments['ownerView'] = new View('/' . $arguments['share']['uid_owner'] . '/files');
$arguments['ownerView'] = new View('/' . $this->share->getShareOwner() . '/files');
parent::__construct($storage, $absMountPoint, $arguments, $loader);
}

/**
* check if the parent folder exists otherwise move the mount point up
*
* @param array $share
* @param \OCP\Share\IShare $share
* @param SharedMount[] $mountpoints
* @return string
*/
private function verifyMountPoint(&$share, array $mountpoints) {
private function verifyMountPoint(\OCP\Share\IShare $share, array $mountpoints) {

$mountPoint = basename($share['file_target']);
$parent = dirname($share['file_target']);
$mountPoint = basename($share->getTarget());
$parent = dirname($share->getTarget());

if (!$this->recipientView->is_dir($parent)) {
$parent = Helper::getShareFolder();
Expand All @@ -86,15 +90,26 @@ private function verifyMountPoint(&$share, array $mountpoints) {
$mountpoints
);

if ($newMountPoint !== $share['file_target']) {
if ($newMountPoint !== $share->getTarget()) {
$this->updateFileTarget($newMountPoint, $share);
$share['file_target'] = $newMountPoint;
$share['unique_name'] = true;
}

return $newMountPoint;
}

/**
* update fileTarget in the database if the mount point changed
*
* @param string $newPath
* @param \OCP\Share\IShare $share
* @return bool
*/
private function updateFileTarget($newPath, &$share) {
$share->setTarget($newPath);
\OC::$server->getShareManager()->moveShare($share, $this->user);
}


/**
* @param string $path
* @param View $view
Expand All @@ -110,7 +125,7 @@ private function generateUniqueTarget($path, $view, array $mountpoints) {
// Helper function to find existing mount points
$mountpointExists = function($path) use ($mountpoints) {
foreach ($mountpoints as $mountpoint) {
if ($mountpoint->getShare()['file_target'] === $path) {
if ($mountpoint->getShare()->getTarget() === $path) {
return true;
}
}
Expand All @@ -126,38 +141,6 @@ private function generateUniqueTarget($path, $view, array $mountpoints) {
return $path;
}

/**
* update fileTarget in the database if the mount point changed
*
* @param string $newPath
* @param array $share reference to the share which should be modified
* @return bool
*/
private function updateFileTarget($newPath, &$share) {
// if the user renames a mount point from a group share we need to create a new db entry
// for the unique name
if ($share['share_type'] === \OCP\Share::SHARE_TYPE_GROUP && empty($share['unique_name'])) {
$query = \OCP\DB::prepare('INSERT INTO `*PREFIX*share` (`item_type`, `item_source`, `item_target`,'
.' `share_type`, `share_with`, `uid_owner`, `permissions`, `stime`, `file_source`,'
.' `file_target`, `token`, `parent`) VALUES (?,?,?,?,?,?,?,?,?,?,?,?)');
$arguments = array($share['item_type'], $share['item_source'], $share['item_target'],
2, $this->user, $share['uid_owner'], $share['permissions'], $share['stime'], $share['file_source'],
$newPath, $share['token'], $share['id']);
} else {
// rename mount point
$query = \OCP\DB::prepare(
'Update `*PREFIX*share`
SET `file_target` = ?
WHERE `id` = ?'
);
$arguments = array($newPath, $share['id']);
}

$result = $query->execute($arguments);

return $result === 1 ? true : false;
}

/**
* Format a path to be relative to the /user/files/ directory
*
Expand Down Expand Up @@ -197,20 +180,11 @@ public function moveMount($target) {

$result = true;

if (!empty($share['grouped'])) {
foreach ($share['grouped'] as $s) {
$result = $this->updateFileTarget($relTargetPath, $s) && $result;
}
} else {
$result = $this->updateFileTarget($relTargetPath, $share) && $result;
}

if ($result) {
try {
$this->updateFileTarget($relTargetPath, $share);
$this->setMountPoint($target);
$this->storage->setUniqueName();
$this->storage->setMountPoint($relTargetPath);

} else {
} catch (\Exception $e) {
\OCP\Util::writeLog('file sharing',
'Could not rename mount point for shared folder "' . $this->getMountPoint() . '" to "' . $target . '"',
\OCP\Util::ERROR);
Expand All @@ -235,11 +209,18 @@ public function removeMount() {
}

/**
* @return array
* @return \OCP\Share\IShare
*/
public function getShare() {
/** @var $storage \OC\Files\Storage\Shared */
$storage = $this->getStorage();
return $storage->getShare();
return $this->share;
}

/**
* Get the file id of the root of the storage
*
* @return int
*/
public function getStorageRootId() {
return $this->share->getNodeId();
}
}
3 changes: 1 addition & 2 deletions apps/files_sharing/lib/sharedpropagator.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,8 @@ class SharedPropagator extends Propagator {
* @return \array[] all propagated entries
*/
public function propagateChange($internalPath, $time, $sizeDifference = 0) {
$source = $this->storage->getSourcePath($internalPath);
/** @var \OC\Files\Storage\Storage $storage */
list($storage, $sourceInternalPath) = \OC\Files\Filesystem::resolvePath($source);
list($storage, $sourceInternalPath) = $this->storage->resolvePath($internalPath);
return $storage->getPropagator()->propagateChange($sourceInternalPath, $time, $sizeDifference);
}
}
Loading

0 comments on commit cd01c44

Please sign in to comment.