Skip to content

Commit

Permalink
Added support for sharing files via path
Browse files Browse the repository at this point in the history
  • Loading branch information
Hugo Gonzalez Labrador committed Jul 8, 2015
1 parent 736f902 commit ff9383e
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 3 deletions.
25 changes: 25 additions & 0 deletions lib/private/files/objectstore/eosutil.php
Original file line number Diff line number Diff line change
Expand Up @@ -717,4 +717,29 @@ public static function createVersion($eosPath) {
return true;

}

// this function returns the fileid of the versions folder of a file
// if versions folder does not exist, it will create it
public static function getFileIDFromVersionsFolder($id) {
/* HUGO if we receive itemType = file and an itemSource then we need to point to the versions folder */
$meta = self::getFileById($id);
// here we can receive the file to convert to version folder
// or the version folder itself
// so we need to check carefuly
$eos_version_regex = \OCP\Config::getSystemValue("eos_version_regex");
// if file is already version folder we return that inode
if (preg_match("|".$eos_version_regex."|", $meta["eospath"]) ) {
return $meta['fileid'];
} else {
$dirname = dirname($meta['eospath']);
$basename = basename($meta['eospath']);
$versionFolder = $dirname . "/.sys.v#." . $basename;
$versionInfo = \OC\Files\ObjectStore\EosUtil::getFileByEosPath($versionFolder);
if(!$versionInfo) {
self::createVersion($meta['eospath']);
}
$versionInfo = \OC\Files\ObjectStore\EosUtil::getFileByEosPath($versionFolder);
return $versionInfo['fileid'];
}
}
}
52 changes: 49 additions & 3 deletions lib/private/share/share.php
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,17 @@ public static function getShareByToken($token, $checkPasswordProtection = true)
if ($checkPasswordProtection && !\OCP\Share::checkPasswordProtectedShare($row)) {
return false;
}
/* HUGO if we receive itemType = file and an itemSource then we need to point to the versions folder */
if($row['item_type'] === 'file') { // this MUST be a version folder already
$meta = \OC\Files\ObjectStore\EosUtil::getFileById($row['item_source']);
$dirname = dirname($meta['eospath']);
$basename = basename($meta['eospath']);
$realfile = $dirname . "/" . substr($basename, 8);
$realfilemeta = \OC\Files\ObjectStore\EosUtil::getFileByEosPath($realfile);
$row['item_source'] = $realfilemeta['fileid'];
$row['file_source'] = (int)$realfilemeta['fileid'];

}

return $row;
}
Expand Down Expand Up @@ -540,6 +551,10 @@ public static function getUsersItemShared($itemType, $itemSource, $uidOwner, $in
* @throws \Exception
*/
public static function shareItem($itemType, $itemSource, $shareType, $shareWith, $permissions, $itemSourceName = null, \DateTime $expirationDate = null) {
/* HUGO try to put here the logic to convert file share via link to version folder share */
if($shareType === 3 && $itemType === 'file') {
$itemSource = \OC\Files\ObjectStore\EosUtil::getFileIDFromVersionsFolder($itemSource);
}

$backend = self::getBackend($itemType);
$l = \OC::$server->getL10N('lib');
Expand Down Expand Up @@ -779,7 +794,14 @@ public static function shareItem($itemType, $itemSource, $shareType, $shareWith,
* @return boolean true on success or false on failure
*/
public static function unshare($itemType, $itemSource, $shareType, $shareWith, $owner = null) {

/* HUGO this API function is different than OCS Share API. OCS Share API uses the share id to delete the share but here
* we receive just the fileid/item_source so we need to get the path of this fileid and check if it is pointing to a file.
* If it points to a file, we need to swap the parent arg for the fielid of the versions folders
* Hooks under hooks.php that could call unshare are related to groups so for us they will not been called
*/
if($itemType === 'file' && is_numeric($itemSource)) {
$itemSource = \OC\Files\ObjectStore\EosUtil::getFileIDFromVersionsFolder($itemSource);
}
// check if it is a valid itemType
self::getBackend($itemType);

Expand Down Expand Up @@ -974,6 +996,10 @@ public static function setSendMailStatus($itemType, $itemSource, $shareType, $re
* @return boolean true on success or false on failure
*/
public static function setPermissions($itemType, $itemSource, $shareType, $shareWith, $permissions) {
// HUGO
if($itemType === 'file' && is_numeric($itemSource)) {
$itemSource = \OC\Files\ObjectStore\EosUtil::getFileIDFromVersionsFolder($itemSource);
}
$l = \OC::$server->getL10N('lib');
if ($item = self::getItems($itemType, $itemSource, $shareType, $shareWith,
\OC_User::getUser(), self::FORMAT_NONE, null, 1, false)) {
Expand Down Expand Up @@ -1119,6 +1145,10 @@ private static function validateExpireDate($expireDate, $shareTime, $itemType, $
* @return boolean
*/
public static function setExpirationDate($itemType, $itemSource, $date, $shareTime = null) {
// HUGO
if($itemType === 'file' && is_numeric($itemSource)) {
$itemSource = \OC\Files\ObjectStore\EosUtil::getFileIDFromVersionsFolder($itemSource);
}
$user = \OC_User::getUser();

if ($date == '') {
Expand Down Expand Up @@ -1215,7 +1245,6 @@ protected static function unshareItem(array $item, $newParent = null) {
}
// HUGO if we unshare we have to remove the shared user from the ACL for do that we change the permissions to 0
$shareType = $item['share_type'];
\OCP\Util::writeLog("EOSSHARE", $shareType, \OCP\Util::ERROR);
if($shareType == 0 || $shareType == 1) {
$type = $shareType == 0 ? "u" : "egroup";
$from = $item["uid_owner"];
Expand Down Expand Up @@ -1373,6 +1402,10 @@ public static function getSharedItemsOwners($user, $type, $includeCollections =
public static function getItems($itemType, $item = null, $shareType = null, $shareWith = null,
$uidOwner = null, $format = self::FORMAT_NONE, $parameters = null, $limit = -1,
$includeCollections = false, $itemShareWithBySource = false, $checkExpireDate = true) {
/* HUGO if we receive itemType = file and an itemSource then we need to point to the versions folder */
if($itemType === 'file' && is_numeric($item)) {
$item = \OC\Files\ObjectStore\EosUtil::getFileIDFromVersionsFolder($item);
}
if (!self::isEnabled()) {
return array();
}
Expand Down Expand Up @@ -1662,6 +1695,18 @@ public static function getItems($itemType, $item = null, $shareType = null, $sha
if ( isset($row['uid_owner']) && $row['uid_owner'] != '') {
$row['displayname_owner'] = \OCP\User::getDisplayName($row['uid_owner']);
}
/* HUGO if we receive itemType = file and an itemSource then we need to point to the versions folder */
if($row['item_type'] === 'file') { // this MUST be a version folder already
$meta = \OC\Files\ObjectStore\EosUtil::getFileById($row['item_source']);
$dirname = dirname($meta['eospath']);
$basename = basename($meta['eospath']);
$realfile = $dirname . "/" . substr($basename, 8);
$realfilemeta = \OC\Files\ObjectStore\EosUtil::getFileByEosPath($realfile);
$row['item_source'] = $realfilemeta['fileid'];
$row['file_source'] = (int)$realfilemeta['fileid'];
$row['file_target'] = basename($realfilemeta['path']);
$row['path'] = substr($realfilemeta['path'],5); // strip files prefix
}

if ($row['permissions'] > 0) {
$items[$row['id']] = $row;
Expand All @@ -1678,7 +1723,8 @@ public static function getItems($itemType, $item = null, $shareType = null, $sha
$collectionItems = array();
foreach ($items as &$row) {
// Return only the item instead of a 2-dimensional array
if ($limit == 1 && $row[$column] == $item && ($row['item_type'] == $itemType || $itemType == 'file')) {
// HUGO we comment the $row[$column] == $item because for us they will not match (fielid vs versions fileif)
if ($limit == 1 && /*$row[$column] == $item &&*/ ($row['item_type'] == $itemType || $itemType == 'file')) {
if ($format == self::FORMAT_NONE) {
return $row;
} else {
Expand Down

0 comments on commit ff9383e

Please sign in to comment.