Clean up constructor for OC_Share, add some error handling for sharing files

This commit is contained in:
Michael Gapczynski 2011-08-12 17:22:32 -04:00
parent a8e6be6016
commit 497789cdd8
3 changed files with 45 additions and 37 deletions

View File

@ -8,7 +8,14 @@ $sources = explode(";", $_POST['sources']);
$uid_shared_with = $_POST['uid_shared_with']; $uid_shared_with = $_POST['uid_shared_with'];
$permissions = $_POST['permissions']; $permissions = $_POST['permissions'];
foreach ($sources as $source) { foreach ($sources as $source) {
new OC_Share($source, $uid_shared_with, $permissions); if ($source && OC_FILESYSTEM::file_exists($source) && OC_FILESYSTEM::is_readable($source)) {
$source = "/".OC_User::getUser()."/files".$source;
try {
new OC_Share($source, $uid_shared_with, $permissions);
} catch (Exception $exception) {
echo "false";
}
}
} }
?> ?>

View File

@ -67,8 +67,10 @@ $(document).ready(function() {
url: OC.linkTo('files_sharing','ajax/share.php'), url: OC.linkTo('files_sharing','ajax/share.php'),
cache: false, cache: false,
data: data, data: data,
success: function() { success: function(result) {
addUser(uid_shared_with, permissions, false); if (result !== 'false') {
addUser(uid_shared_with, permissions, false);
}
} }
}); });
}); });

View File

@ -32,43 +32,42 @@ class OC_Share {
const DELETE = 2; const DELETE = 2;
/** /**
* TODO notify user a file is being shared with them?
* Share an item, adds an entry into the database * Share an item, adds an entry into the database
* @param string $item * @param $source The source location of the item
* @param user item shared with $uid_shared_with * @param $uid_shared_with The user to share the item with
* @param $permissions The permissions, use the constants WRITE and DELETE
*/ */
public function __construct($source, $uid_shared_with, $permissions, $public = false) { public function __construct($source, $uid_shared_with, $permissions) {
if ($source && OC_FILESYSTEM::file_exists($source) && OC_FILESYSTEM::is_readable($source)) { $uid_owner = OC_User::getUser();
$source = "/".OC_User::getUser()."/files".$source; $target = "/".$uid_shared_with."/files/Share/".basename($source);
$uid_owner = OC_User::getUser(); // Check if this item is already shared with the user
if ($public) { $checkSource = OC_DB::prepare("SELECT source FROM *PREFIX*sharing WHERE source = ? AND uid_shared_with = ?");
// TODO create token for public file $resultCheckSource = $checkSource->execute(array($source, $uid_shared_with))->fetchAll();
$token = sha1("$uid_owner-$item"); // TODO Check if the source is inside a folder
} else { if (count($resultCheckSource) > 0) {
$query = OC_DB::prepare("INSERT INTO *PREFIX*sharing VALUES(?,?,?,?,?)"); throw new Exception("This item is already shared with the specified user");
$target = "/".$uid_shared_with."/files/Share/".basename($source);
$check = OC_DB::prepare("SELECT target FROM *PREFIX*sharing WHERE target = ? AND uid_shared_with = ?");
$result = $check->execute(array($target, $uid_shared_with))->fetchAll();
// Check if target already exists for the user, if it does append a number to the name
if (count($result) > 0) {
if ($pos = strrpos($target, ".")) {
$name = substr($target, 0, $pos);
$ext = substr($target, $pos);
} else {
$name = $target;
$ext = "";
}
$counter = 1;
while (count($result) > 0) {
$newTarget = $name."_".$counter.$ext;
$result = $check->execute(array($newTarget, $uid_shared_with))->fetchAll();
$counter++;
}
$target = $newTarget;
}
$query->execute(array($uid_owner, $uid_shared_with, $source, $target, $permissions));
}
} }
// Check if target already exists for the user, if it does append a number to the name
$checkTarget = OC_DB::prepare("SELECT target FROM *PREFIX*sharing WHERE target = ? AND uid_shared_with = ?");
$resultCheckTarget = $checkTarget->execute(array($target, $uid_shared_with))->fetchAll();
if (count($resultCheckTarget) > 0) {
if ($pos = strrpos($target, ".")) {
$name = substr($target, 0, $pos);
$ext = substr($target, $pos);
} else {
$name = $target;
$ext = "";
}
$counter = 1;
while (count($result) > 0) {
$newTarget = $name."_".$counter.$ext;
$resultCheckTarget = $checkTarget->execute(array($newTarget, $uid_shared_with))->fetchAll();
$counter++;
}
$target = $newTarget;
}
$query = OC_DB::prepare("INSERT INTO *PREFIX*sharing VALUES(?,?,?,?,?)");
$query->execute(array($uid_owner, $uid_shared_with, $source, $target, $permissions));
} }
/** /**