]> source.dussan.org Git - nextcloud-server.git/commitdiff
intermediate result ocs api
authorBjoern Schiessle <schiessle@owncloud.com>
Fri, 6 Sep 2013 08:49:21 +0000 (10:49 +0200)
committerBjoern Schiessle <schiessle@owncloud.com>
Fri, 6 Sep 2013 08:49:21 +0000 (10:49 +0200)
apps/files_sharing/appinfo/routes.php
apps/files_sharing/lib/api.php

index 2e26033cad0c77424139c7e3cf750d0fcce262d1..1c7f5b4a1fc3f07bd59122c4040835e7b0bfa1a0 100644 (file)
@@ -4,12 +4,11 @@ function() {
        require_once __DIR__ . '/../ajax/publicpreview.php';
 });
 
-//TODO: GET: share status of a given file/folder
-//TODO: GET: share status of all files in a given folder?
-//TODO: SET: share (unshare)
+//TODO: SET: unshare
 //TODO: SET: permissions
 //TODO: SET: expire date
 //TODO: SET: mail notification
+//TODO: SET: can upload
 
 OC_API::register('get',
                '/apps/files_sharing/api/share/{path}',
@@ -18,6 +17,23 @@ OC_API::register('get',
                OC_API::USER_AUTH,
                array('path' => ''),
                array('path' => '.+')); //allow slashes in parameter path
+
+OC_API::register('post',
+               '/apps/files_sharing/api/share/{path}',
+               array('\OCA\Files\Share\Api', 'setShare'),
+               'files_sharing',
+               OC_API::USER_AUTH,
+               array('path' => ''),
+               array('path' => '.+'));
+
+OC_API::register('post',
+               '/apps/files_sharing/api/permission/{path}',
+               array('\OCA\Files\Share\Api', 'setPermission'),
+               'files_sharing',
+               OC_API::USER_AUTH,
+               array('path' => ''),
+               array('path' => '.+'));
+
 /*
 OC_API::register('get',
                '/apps/files_sharing/api/permission/{path}',
index cfe1fc2da46f0ce693f3ce32a184feff5ac42eeb..7f7f925eb231ba719d1cb20b11ffb338a7b71868 100644 (file)
@@ -33,16 +33,108 @@ class Api {
        public static function getShare($params) {
                $path = $params['path'];
 
+               $fileId = self::getFileId($path);
+               if ($fileId !== null) {
+                       $share = \OCP\Share::getItemShared('file', $fileId);
+               } else {
+                       $share = null;
+               }
+
+               if ($share !== null) {
+                       return new \OC_OCS_Result($share);
+               } else {
+                       return new \OC_OCS_Result(null, 404, 'file/folder doesn\'t exists');
+               }
+       }
+
+       /**
+        * @brief share file with a user/group
+        *
+        * @param array $params which contains a 'path' to a file/folder
+        * @return \OC_OCS_Result result of share operation
+        */
+       public static function setShare($params) {
+               $path = $params['path'];
+               $errorMessage = '';
+
+               $itemSource = self::getFileId($path);
+               $itemType = self::getItemType($path);
+
+               $shareWith = isset($_POST['shareWith']) ? $_POST['shareWith'] : null;
+               $shareType = isset($_POST['shareType']) ? (int)$_POST['shareType'] : null;
+
+               if($shareType === \OCP\Share::SHARE_TYPE_LINK) {
+                       $permissions = 1;
+                       $shareWith = null;
+               } else {
+                       $permissions = 31;
+               }
+
+
+               $token = null;
+               if (($shareWith !== null || $shareType === \OCP\Share::SHARE_TYPE_LINK)
+                               && $shareType !== false
+                               && $itemType !== false) {
+                       $token = \OCP\Share::shareItem(
+                                       $itemType,
+                                       $itemSource,
+                                       $shareType,
+                                       $shareWith,
+                                       $permissions
+                                       );
+               } else {
+                       $errorMessage = "You need to specify at least 'shareType' and provide a correct file/folder path."
+                               . " For non public shares you also need specify 'shareWith'.";
+               }
+
+
+               if ($token) {
+                       $data = null;
+                       if(is_string($token)) { //public link share
+                               $url = \OCP\Util::linkToPublic('files&t='.$token);
+                               $data = array('url' => $url, // '&' gets encoded to $amp;
+                                       'token' => $token);
+
+                       }
+                       return new \OC_OCS_Result($data);
+               } else {
+                       return new \OC_OCS_Result(null, 404, $errorMessage);
+               }
+       }
+
+       /**
+        * @brief get file ID from a given path
+        * @param string $path
+        * @return string fileID or null
+        */
+       private static function getFileId($path) {
                $view = new \OC\Files\View('/'.\OCP\User::getUser().'/files');
+               $fileId = null;
+
                $fileInfo = $view->getFileInfo($path);
                if ($fileInfo) {
-                       $share = \OCP\Share::getItemShared('file', $fileInfo['fileid']);
-               } else {
-                       \OCP\Util::writeLog('files_sharing', 'OCS API getShare, file ' . $path . ' does not exists', \OCP\Util::WARN);
-                       $share = array();
+                       $fileId = $fileInfo['fileid'];
+               }
+
+               return $fileId;
+       }
+
+       /**
+        * @brief get itemType
+        * @param string $path
+        * @return string type 'file', 'folder' or null of file/folder doesn't exists
+        */
+       private static function getItemType($path) {
+               $view = new \OC\Files\View('/'.\OCP\User::getUser().'/files');
+               $itemType = null;
+
+               if ($view->is_dir($path)) {
+                       $itemType = "folder";
+               } elseif ($view->is_file($path)) {
+                       $itemType = "file";
                }
 
-               return new \OC_OCS_Result($share);
+               return $itemType;
        }
 
 }
\ No newline at end of file