more rest-style API

This commit is contained in:
Bjoern Schiessle 2013-09-17 11:53:06 +02:00
parent ef3307f099
commit 86dbb13823
2 changed files with 134 additions and 53 deletions

View File

@ -9,40 +9,34 @@ function() {
//TODO: SET: mail notification, waiting for PR #4689 to be accepted //TODO: SET: mail notification, waiting for PR #4689 to be accepted
OC_API::register('get', OC_API::register('get',
'/apps/files_sharing/api/share/{path}', '/apps/files_sharing/api/shares',
array('\OCA\Files\Share\Api', 'getAllShare'),
'files_sharing');
OC_API::register('post',
'/apps/files_sharing/api/shares',
array('\OCA\Files\Share\Api', 'createShare'),
'files_sharing');
OC_API::register('get',
'/apps/files_sharing/api/shares/{path}',
array('\OCA\Files\Share\Api', 'getShare'), array('\OCA\Files\Share\Api', 'getShare'),
'files_sharing', 'files_sharing',
OC_API::USER_AUTH, OC_API::USER_AUTH,
array('path' => ''), array('path' => ''),
array('path' => '.+')); //allow slashes in parameter path array('path' => '.+')); //allow slashes in parameter path
OC_API::register('post', OC_API::register('put',
'/apps/files_sharing/api/share/{path}', '/apps/files_sharing/api/shares/{path}',
array('\OCA\Files\Share\Api', 'setShare'), array('\OCA\Files\Share\Api', 'updateShare'),
'files_sharing', 'files_sharing',
OC_API::USER_AUTH, OC_API::USER_AUTH,
array('path' => ''), array('path' => ''),
array('path' => '.+')); array('path' => '.+'));
OC_API::register('post', OC_API::register('delete',
'/apps/files_sharing/api/permission/{path}', '/apps/files_sharing/api/shares/{path}',
array('\OCA\Files\Share\Api', 'setPermission'), array('\OCA\Files\Share\Api', 'deleteShare'),
'files_sharing',
OC_API::USER_AUTH,
array('path' => ''),
array('path' => '.+'));
OC_API::register('post',
'/apps/files_sharing/api/expire/{path}',
array('\OCA\Files\Share\Api', 'setExpire'),
'files_sharing',
OC_API::USER_AUTH,
array('path' => ''),
array('path' => '.+'));
OC_API::register('post',
'/apps/files_sharing/api/unshare/{path}',
array('\OCA\Files\Share\Api', 'setUnshare'),
'files_sharing', 'files_sharing',
OC_API::USER_AUTH, OC_API::USER_AUTH,
array('path' => ''), array('path' => ''),

View File

@ -24,6 +24,23 @@ namespace OCA\Files\Share;
class Api { class Api {
/**
* @brief get all shares
*
* @param array $params
* @return \OC_OCS_Result share information
*/
public static function getAllShare($params) {
$share = \OCP\Share::getItemShared('file', null);
if ($share !== null) {
return new \OC_OCS_Result($share);
} else {
return new \OC_OCS_Result(null, 404, 'no shares available');
}
}
/** /**
* @brief get share information for a given file/folder path is encoded in URL * @brief get share information for a given file/folder path is encoded in URL
* *
@ -48,14 +65,17 @@ class Api {
} }
/** /**
* @brief share file with a user/group, path to file is encoded in URL * @breif create a new share
* * @param array $params 'path', 'shareWith', 'shareType'
* @param array $params with following parameters 'shareWith', 'shareType', 'path' * @return \OC_OCS_Result
* optional 'publicUpload' and 'password' for public shares
* @return \OC_OCS_Result result of share operation
*/ */
public static function setShare($params) { public static function createShare($params) {
$path = $params['path'];
$path = isset($_POST['path']) ? $_POST['path'] : null;
if($path === null) {
return new \OC_OCS_Result(null, 404, "please specify a file or folder path");
}
$itemSource = self::getFileId($path); $itemSource = self::getFileId($path);
$itemType = self::getItemType($path); $itemType = self::getItemType($path);
@ -69,10 +89,10 @@ class Api {
switch($shareType) { switch($shareType) {
case \OCP\Share::SHARE_TYPE_USER: case \OCP\Share::SHARE_TYPE_USER:
$permission = isset($_POST['permission']) ? (int)$_POST['permission'] : 31; $permissions = isset($_POST['permissions']) ? (int)$_POST['permissions'] : 31;
break; break;
case \OCP\Share::SHARE_TYPE_GROUP: case \OCP\Share::SHARE_TYPE_GROUP:
$permission = isset($_POST['permission']) ? (int)$_POST['permission'] : 31; $permissions = isset($_POST['permissions']) ? (int)$_POST['permissions'] : 31;
break; break;
case \OCP\Share::SHARE_TYPE_LINK: case \OCP\Share::SHARE_TYPE_LINK:
//allow password protection //allow password protection
@ -87,7 +107,7 @@ class Api {
$publicUpload = isset($_POST['publicUpload']) ? $_POST['publicUpload'] : 'no'; $publicUpload = isset($_POST['publicUpload']) ? $_POST['publicUpload'] : 'no';
// read, create, update (7) if public upload is enabled or // read, create, update (7) if public upload is enabled or
// read (1) if public upload is disabled // read (1) if public upload is disabled
$permission = $publicUpload === 'yes' ? 7 : 1; $permissions = $publicUpload === 'yes' ? 7 : 1;
break; break;
} }
@ -97,7 +117,7 @@ class Api {
$itemSource, $itemSource,
$shareType, $shareType,
$shareWith, $shareWith,
$permission $permissions
); );
} catch (\Exception $e) { } catch (\Exception $e) {
return new \OC_OCS_Result(null, 404, $e->getMessage()); return new \OC_OCS_Result(null, 404, $e->getMessage());
@ -116,13 +136,17 @@ class Api {
return new \OC_OCS_Result(null, 404, "couldn't share file"); return new \OC_OCS_Result(null, 404, "couldn't share file");
} }
} }
/** /**
* @brief set permission for a share, path to file is encoded in URL * update shares, e.g. expire date, permissions, etc
* @param array $params contain 'shareWith', 'shareType', 'permission' * @param array $params 'path', 'shareWith', 'shareType' and
* 'permissions' or 'expire' or 'password'
* @return \OC_OCS_Result * @return \OC_OCS_Result
*/ */
public static function setPermission($params) { public static function updateShare($params) {
$path = $params['path']; $path = $params['path'];
$itemSource = self::getFileId($path); $itemSource = self::getFileId($path);
$itemType = self::getItemType($path); $itemType = self::getItemType($path);
@ -130,9 +154,34 @@ class Api {
return new \OC_OCS_Result(null, 404, "wrong path, file/folder doesn't exist."); return new \OC_OCS_Result(null, 404, "wrong path, file/folder doesn't exist.");
} }
$shareWith = isset($_POST['shareWith']) ? $_POST['shareWith'] : null; try {
$shareType = isset($_POST['shareType']) ? (int)$_POST['shareType'] : null; if(isset($params['_put']['permissions'])) {
$permission = isset($_POST['permission']) ? (int)$_POST['permission'] : null; return self::updatePermissions($itemSource, $itemType, $params);
} elseif (isset($params['_put']['expire'])) {
return self::updateExpire($itemSource, $itemType, $params);
} elseif (isset($params['_put']['password'])) {
return self::updatePassword($itemSource, $itemType, $params);
}
} catch (\Exception $e) {
return new \OC_OCS_Result(null, 404, $e->getMessage());
}
return new \OC_OCS_Result(null, 404, "Couldn't find a parameter to update");
}
/**
* @brief update permissions for a share
* @param int $itemSource file ID
* @param string $itemType 'file' or 'folder'
* @param array $params contain 'shareWith', 'shareType', 'permissions'
* @return \OC_OCS_Result
*/
private static function updatePermissions($itemSource, $itemType, $params) {
$shareWith = isset($params['_put']['shareWith']) ? $params['_put']['shareWith'] : null;
$shareType = isset($params['_put']['shareType']) ? (int)$params['_put']['shareType'] : null;
$permissions = isset($params['_put']['permissions']) ? (int)$params['_put']['permissions'] : null;
try { try {
$return = \OCP\Share::setPermissions( $return = \OCP\Share::setPermissions(
@ -140,7 +189,7 @@ class Api {
$itemSource, $itemSource,
$shareType, $shareType,
$shareWith, $shareWith,
$permission $permissions
); );
} catch (\Exception $e) { } catch (\Exception $e) {
return new \OC_OCS_Result(null, 404, $e->getMessage()); return new \OC_OCS_Result(null, 404, $e->getMessage());
@ -153,21 +202,59 @@ class Api {
} }
} }
/**
* @brief update password for public link share
* @param int $itemSource file ID
* @param string $itemType 'file' or 'folder'
* @param type $params 'password'
* @return \OC_OCS_Result
*/
private static function updatePassword($itemSource, $itemType, $params) {
error_log("update password");
$shareWith = isset($params['_put']['password']) ? $params['_put']['password'] : null;
if($shareWith === '') {
$shareWith = null;
}
$items = \OCP\Share::getItemShared($itemType, $itemSource);
$checkExists = false;
foreach ($items as $item) {
if($item['share_type'] === \OCP\Share::SHARE_TYPE_LINK) {
$checkExists = true;
$permissions = $item['permissions'];
}
}
if (!$checkExists) {
return new \OC_OCS_Result(null, 404, "share doesn't exists, can't change password");
}
$result = \OCP\Share::shareItem(
$itemType,
$itemSource,
\OCP\Share::SHARE_TYPE_LINK,
$shareWith,
$permissions
);
if($result) {
return new \OC_OCS_Result();
}
return new \OC_OCS_Result(null, 404, "couldn't set password");
}
/** /**
* @brief set expire date, path to file is encoded in URL * @brief set expire date, path to file is encoded in URL
* @param int $itemSource file ID
* @param string $itemType 'file' or 'folder'
* @param array $params contains 'expire' (format DD-MM-YYYY) * @param array $params contains 'expire' (format DD-MM-YYYY)
* @return \OC_OCS_Result * @return \OC_OCS_Result
*/ */
public static function setExpire($params) { private static function updateExpire($itemSource, $itemType, $params) {
$path = $params['path'];
$itemSource = self::getFileId($path);
$itemType = self::getItemType($path);
if($itemSource === null) { $expire = isset($params['_put']['expire']) ? (int)$params['_put']['expire'] : null;
return new \OC_OCS_Result(null, 404, "wrong path, file/folder doesn't exist.");
}
$expire = isset($_POST['expire']) ? (int)$_POST['expire'] : null;
$return = false; $return = false;
if ($expire) { if ($expire) {
@ -187,7 +274,7 @@ class Api {
* @param array $params with following parameters 'shareWith', 'shareType', 'path' * @param array $params with following parameters 'shareWith', 'shareType', 'path'
* @return \OC_OCS_Result * @return \OC_OCS_Result
*/ */
public static function setUnshare($params) { public static function deleteShare($params) {
$path = $params['path']; $path = $params['path'];
$itemSource = self::getFileId($path); $itemSource = self::getFileId($path);
$itemType = self::getItemType($path); $itemType = self::getItemType($path);
@ -196,8 +283,8 @@ class Api {
return new \OC_OCS_Result(null, 404, "wrong path, file/folder doesn't exist."); return new \OC_OCS_Result(null, 404, "wrong path, file/folder doesn't exist.");
} }
$shareWith = isset($_POST['shareWith']) ? $_POST['shareWith'] : null; $shareWith = isset($params['_delete']['shareWith']) ? $params['_delete']['shareWith'] : null;
$shareType = isset($_POST['shareType']) ? (int)$_POST['shareType'] : null; $shareType = isset($params['_delete']['shareType']) ? (int)$params['_delete']['shareType'] : null;
if( $shareType == \OCP\Share::SHARE_TYPE_LINK) { if( $shareType == \OCP\Share::SHARE_TYPE_LINK) {
$shareWith = null; $shareWith = null;