ocs api for file sharing
This commit is contained in:
parent
3861c9bce1
commit
14437ffd15
|
@ -4,10 +4,9 @@ function() {
|
||||||
require_once __DIR__ . '/../ajax/publicpreview.php';
|
require_once __DIR__ . '/../ajax/publicpreview.php';
|
||||||
});
|
});
|
||||||
|
|
||||||
//TODO: SET: unshare
|
// OCS API
|
||||||
//TODO: SET: expire date
|
|
||||||
//TODO: SET: mail notification
|
//TODO: SET: mail notification, waiting for PR #4689 to be accepted
|
||||||
//TODO: SET: can upload
|
|
||||||
|
|
||||||
OC_API::register('get',
|
OC_API::register('get',
|
||||||
'/apps/files_sharing/api/share/{path}',
|
'/apps/files_sharing/api/share/{path}',
|
||||||
|
@ -41,25 +40,10 @@ OC_API::register('post',
|
||||||
array('path' => ''),
|
array('path' => ''),
|
||||||
array('path' => '.+'));
|
array('path' => '.+'));
|
||||||
|
|
||||||
/*
|
OC_API::register('post',
|
||||||
OC_API::register('get',
|
'/apps/files_sharing/api/unshare/{path}',
|
||||||
'/apps/files_sharing/api/permission/{path}',
|
array('\OCA\Files\Share\Api', 'setUnshare'),
|
||||||
array('\OCA\Files\Share\Api', 'getShare'),
|
|
||||||
'files_sharing',
|
'files_sharing',
|
||||||
OC_API::USER_AUTH,
|
OC_API::USER_AUTH,
|
||||||
array('path' => ''));
|
array('path' => ''),
|
||||||
|
array('path' => '.+'));
|
||||||
OC_API::register('get',
|
|
||||||
'/apps/files_sharing/api/expire/{path}',
|
|
||||||
array('\OCA\Files\Share\Api', 'getShare'),
|
|
||||||
'files_sharing',
|
|
||||||
OC_API::USER_AUTH,
|
|
||||||
array('path' => ''));
|
|
||||||
|
|
||||||
OC_API::register('get',
|
|
||||||
'/apps/files_sharing/api/notify/{path}',
|
|
||||||
array('\OCA\Files\Share\Api', 'getShare'),
|
|
||||||
'files_sharing',
|
|
||||||
OC_API::USER_AUTH,
|
|
||||||
array('path' => ''));
|
|
||||||
*/
|
|
||||||
|
|
|
@ -50,7 +50,8 @@ class Api {
|
||||||
/**
|
/**
|
||||||
* @brief share file with a user/group, path to file is encoded in URL
|
* @brief share file with a user/group, path to file is encoded in URL
|
||||||
*
|
*
|
||||||
* @param array $params with following parameters 'shareWith', 'shareType'
|
* @param array $params with following parameters 'shareWith', 'shareType', 'path'
|
||||||
|
* optional 'publicUpload' and 'password' for public shares
|
||||||
* @return \OC_OCS_Result result of share operation
|
* @return \OC_OCS_Result result of share operation
|
||||||
*/
|
*/
|
||||||
public static function setShare($params) {
|
public static function setShare($params) {
|
||||||
|
@ -69,25 +70,19 @@ class Api {
|
||||||
switch($shareType) {
|
switch($shareType) {
|
||||||
case \OCP\Share::SHARE_TYPE_USER:
|
case \OCP\Share::SHARE_TYPE_USER:
|
||||||
$permission = 31;
|
$permission = 31;
|
||||||
if (!\OCP\User::userExists($shareWith)) {
|
|
||||||
return new \OC_OCS_Result(null, 404, "user doesn't exist");
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
case \OCP\Share::SHARE_TYPE_GROUP:
|
case \OCP\Share::SHARE_TYPE_GROUP:
|
||||||
$permission = 31;
|
$permission = 31;
|
||||||
if (!\OC_Group::groupExists($shareWith)) {
|
|
||||||
return new \OC_OCS_Result(null, 404, "group doesn't exist");
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
case \OCP\Share::SHARE_TYPE_LINK:
|
case \OCP\Share::SHARE_TYPE_LINK:
|
||||||
$permission = 1;
|
//allow password protection
|
||||||
$shareWith = null;
|
$shareWith = isset($_POST['password']) ? $_POST['password'] : null;
|
||||||
|
$publicUpload = isset($_POST['publicUpload']) ? $_POST['publicUpload'] : 'no';
|
||||||
|
$permission = self::getPublicLinkSharePermissions($publicUpload);
|
||||||
break;
|
break;
|
||||||
default:
|
|
||||||
return new \OC_OCS_Result(null, 404, "unknown share type");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
$token = \OCP\Share::shareItem(
|
$token = \OCP\Share::shareItem(
|
||||||
$itemType,
|
$itemType,
|
||||||
$itemSource,
|
$itemSource,
|
||||||
|
@ -95,6 +90,9 @@ class Api {
|
||||||
$shareWith,
|
$shareWith,
|
||||||
$permission
|
$permission
|
||||||
);
|
);
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
return new \OC_OCS_Result(null, 404, $e->getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
if ($token) {
|
if ($token) {
|
||||||
$data = null;
|
$data = null;
|
||||||
|
@ -127,24 +125,7 @@ class Api {
|
||||||
$shareType = isset($_POST['shareType']) ? (int)$_POST['shareType'] : null;
|
$shareType = isset($_POST['shareType']) ? (int)$_POST['shareType'] : null;
|
||||||
$permission = isset($_POST['permission']) ? (int)$_POST['permission'] : null;
|
$permission = isset($_POST['permission']) ? (int)$_POST['permission'] : null;
|
||||||
|
|
||||||
switch($shareType) {
|
try {
|
||||||
case \OCP\Share::SHARE_TYPE_USER:
|
|
||||||
if (!\OCP\User::userExists($shareWith)) {
|
|
||||||
return new \OC_OCS_Result(null, 404, "user doesn't exist");
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case \OCP\Share::SHARE_TYPE_GROUP:
|
|
||||||
if (!\OC_Group::groupExists($shareWith)) {
|
|
||||||
return new \OC_OCS_Result(null, 404, "group doesn't exist");
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case \OCP\Share::SHARE_TYPE_LINK:
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
return new \OC_OCS_Result(null, 404, "unknown share type");
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
$return = \OCP\Share::setPermissions(
|
$return = \OCP\Share::setPermissions(
|
||||||
$itemType,
|
$itemType,
|
||||||
$itemSource,
|
$itemSource,
|
||||||
|
@ -152,6 +133,9 @@ class Api {
|
||||||
$shareWith,
|
$shareWith,
|
||||||
$permission
|
$permission
|
||||||
);
|
);
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
return new \OC_OCS_Result(null, 404, $e->getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
if ($return) {
|
if ($return) {
|
||||||
return new \OC_OCS_Result();
|
return new \OC_OCS_Result();
|
||||||
|
@ -187,8 +171,63 @@ class Api {
|
||||||
$msg = "Failed, please check the expire date, expected format 'DD-MM-YYYY'.";
|
$msg = "Failed, please check the expire date, expected format 'DD-MM-YYYY'.";
|
||||||
return new \OC_OCS_Result(null, 404, $msg);
|
return new \OC_OCS_Result(null, 404, $msg);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief unshare a file/folder
|
||||||
|
* @param array $params with following parameters 'shareWith', 'shareType', 'path'
|
||||||
|
* @return \OC_OCS_Result
|
||||||
|
*/
|
||||||
|
public static function setUnshare($params) {
|
||||||
|
$path = $params['path'];
|
||||||
|
$itemSource = self::getFileId($path);
|
||||||
|
$itemType = self::getItemType($path);
|
||||||
|
|
||||||
|
if($itemSource === null) {
|
||||||
|
return new \OC_OCS_Result(null, 404, "wrong path, file/folder doesn't exist.");
|
||||||
|
}
|
||||||
|
|
||||||
|
$shareWith = isset($_POST['shareWith']) ? $_POST['shareWith'] : null;
|
||||||
|
$shareType = isset($_POST['shareType']) ? (int)$_POST['shareType'] : null;
|
||||||
|
|
||||||
|
if( $shareType == \OCP\Share::SHARE_TYPE_LINK) {
|
||||||
|
$shareWith = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
$return = \OCP\Share::unshare(
|
||||||
|
$itemType,
|
||||||
|
$itemSource,
|
||||||
|
$shareType,
|
||||||
|
$shareWith);
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
return new \OC_OCS_Result(null, 404, $e->getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($return) {
|
||||||
|
return new \OC_OCS_Result();
|
||||||
|
} else {
|
||||||
|
$msg = "Unshare Failed";
|
||||||
|
return new \OC_OCS_Result(null, 404, $msg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief get public link share permissions to allow/forbid public uploads
|
||||||
|
* @param string $publicUpload 'yes' or 'no'
|
||||||
|
* @return int permissions read (1) or create,update,read (7)
|
||||||
|
*/
|
||||||
|
private static function getPublicLinkSharePermissions($publicUpload) {
|
||||||
|
|
||||||
|
$publicUploadEnabled = \OC_Appconfig::getValue('core', 'shareapi_allow_public_upload', 'yes');
|
||||||
|
|
||||||
|
if(\OC_App::isEnabled('files_encryption') ||
|
||||||
|
$publicUploadEnabled !== 'yes' ||
|
||||||
|
$publicUpload === 'no') {
|
||||||
|
return 1; // read
|
||||||
|
} else {
|
||||||
|
return 7; // create, update, read
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in New Issue