Add pullOutOfFolder() function for use by unlink() and rename()

This commit is contained in:
Michael Gapczynski 2011-07-14 21:04:09 -04:00
parent 7920e706ea
commit 8ed0223bd6
2 changed files with 33 additions and 10 deletions

View File

@ -49,6 +49,22 @@ class OC_SHARE {
} }
} }
/**
* Create a new entry in the database for a file inside a shared folder
*
* $oldTarget and $newTarget may be the same value. $oldTarget exists in case the file is being moved outside of the folder
*
* @param $oldTarget The current target location
* @param $newTarget The new target location
*/
public static function pullOutOfFolder($oldTarget, $newTarget) {
$folders = self::getParentFolders($oldTarget);
$source = $folders['source'].substr($target, strlen($folders['target']));
$item = self::getItem($folders['target']);
$query = OC_DB::prepare("INSERT INTO *PREFIX*sharing VALUES(?,?,?,?,?)");
$query->execute(array($item[0]['uid_owner'], $_SESSION['user_id'], $source, $newTarget, $item[0]['is_writeable']));
}
/** /**
* Get the item with the specified target location * Get the item with the specified target location
* @param $target The target location of the item * @param $target The target location of the item
@ -98,12 +114,11 @@ class OC_SHARE {
// Prevent searching for user directory e.g. '/MTGap/files' // Prevent searching for user directory e.g. '/MTGap/files'
$userDirectory = substr($target, 0, strpos($target, "files") + 5); $userDirectory = substr($target, 0, strpos($target, "files") + 5);
while ($target != "" && $target != "/" && $target != "." && $target != $userDirectory) { while ($target != "" && $target != "/" && $target != "." && $target != $userDirectory) {
// Check if the parent directory of this target location is shared
$target = dirname($target);
$result = $query->execute(array($target, $_SESSION['user_id']))->fetchAll(); $result = $query->execute(array($target, $_SESSION['user_id']))->fetchAll();
if (count($result) > 0) { if (count($result) > 0) {
break; break;
} else {
// Check if the parent directory of this target location is shared
$target = dirname($target);
} }
} }
if (count($result) > 0) { if (count($result) > 0) {
@ -172,7 +187,7 @@ class OC_SHARE {
/** /**
* Set the target location to a new value * Set the target location to a new value
* *
* You must construct a new shared item to change the target location of a file inside a shared folder if the target location differs from the folder * You must use the pullOutOfFolder() function to change the target location of a file inside a shared folder if the target location differs from the folder
* *
* @param $oldTarget The current target location * @param $oldTarget The current target location
* @param $newTarget The new target location * @param $newTarget The new target location
@ -201,7 +216,7 @@ class OC_SHARE {
/** /**
* Unshare the item, removes it from all specified users * Unshare the item, removes it from all specified users
* *
* You must construct a new shared item to unshare a file inside a shared folder and set target to nothing * You must use the pullOutOfFolder() function to unshare a file inside a shared folder and set $newTarget to nothing
* *
* @param $source The source location of the item * @param $source The source location of the item
* @param $uid_shared_with Array of users to unshare the item from * @param $uid_shared_with Array of users to unshare the item from
@ -216,7 +231,7 @@ class OC_SHARE {
/** /**
* Unshare the item from the current user, removes it only from the database and doesn't touch the source file * Unshare the item from the current user, removes it only from the database and doesn't touch the source file
* *
* You must construct a new shared item to unshare a file inside a shared folder and set target to nothing * You must use the pullOutOfFolder() function to unshare a file inside a shared folder and set $newTarget to nothing
* *
* @param $target The target location of the item * @param $target The target location of the item
*/ */

View File

@ -346,15 +346,23 @@ class OC_FILESTORAGE_SHARED extends OC_FILESTORAGE {
} }
public function unlink($path) { public function unlink($path) {
// The file will be removed from the database, but won't be deleted from the owner's filesystem // The file will be removed from the database, but won't be touched on the owner's filesystem
OC_SHARE::unshareFromMySelf($this->datadir.$path); $target = $this->datadir.$path;
if (OC_SHARE::getItem($target)) {
OC_SHARE::unshareFromMySelf($target);
} else {
OC_SHARE::pullOutOfFolder($target, "");
}
} }
public function rename($path1, $path2) { public function rename($path1, $path2) {
// The file will be renamed in the database, but won't be touched on the owner's filesystem
$oldTarget = $this->datadir.$path1;
$newTarget = $this->datadir.$path2;
if (dirname($path1) == dirname($path2)) { if (dirname($path1) == dirname($path2)) {
OC_SHARE::setTarget($this->datadir.$path1, $this->datadir.$path2); OC_SHARE::setTarget($oldTarget, $newTarget);
} else { } else {
// TODO Construct new shared item OC_SHARE::pullOutOfFolder($oldTarget, $newTarget);
} }
} }