]> source.dussan.org Git - nextcloud-server.git/commitdiff
Add API method for sharing a file, currently only via a link.
authorTom Needham <needham.thomas@gmail.com>
Sun, 28 Oct 2012 11:06:47 +0000 (11:06 +0000)
committerTom Needham <needham.thomas@gmail.com>
Sun, 28 Oct 2012 11:06:47 +0000 (11:06 +0000)
apps/files_sharing/appinfo/app.php
apps/files_sharing/appinfo/routes.php [new file with mode: 0644]
apps/files_sharing/lib/api.php [new file with mode: 0644]
lib/api.php

index 109f86b2e8752b78c9fd53270d383b61d45f4ecf..1402a146454ef58e5c70c6b87cf081fe1996db32 100644 (file)
@@ -3,7 +3,8 @@
 OC::$CLASSPATH['OC_Share_Backend_File'] = "apps/files_sharing/lib/share/file.php";
 OC::$CLASSPATH['OC_Share_Backend_Folder'] = 'apps/files_sharing/lib/share/folder.php';
 OC::$CLASSPATH['OC_Filestorage_Shared'] = "apps/files_sharing/lib/sharedstorage.php";
+OC::$CLASSPATH['OC_Sharing_API'] = "apps/files_sharing/lib/api.php";
 OCP\Util::connectHook('OC_Filesystem', 'setup', 'OC_Filestorage_Shared', 'setup');
 OCP\Share::registerBackend('file', 'OC_Share_Backend_File');
 OCP\Share::registerBackend('folder', 'OC_Share_Backend_Folder', 'file');
-OCP\Util::addScript('files_sharing', 'share');
+OCP\Util::addScript('files_sharing', 'share');
\ No newline at end of file
diff --git a/apps/files_sharing/appinfo/routes.php b/apps/files_sharing/appinfo/routes.php
new file mode 100644 (file)
index 0000000..d10607a
--- /dev/null
@@ -0,0 +1,24 @@
+<?php
+/**
+* ownCloud
+*
+* @author Tom Needham
+* @copyright 2012 Tom Needham tom@owncloud.com
+*
+* This library is free software; you can redistribute it and/or
+* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
+* License as published by the Free Software Foundation; either
+* version 3 of the License, or any later version.
+*
+* This library is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
+*
+* You should have received a copy of the GNU Affero General Public
+* License along with this library.  If not, see <http://www.gnu.org/licenses/>.
+*
+*/
+OCP\API::register('post', '/cloud/files/share/{type}/{path}', array('OC_Sharing_API', 'shareFile'), 'files_sharing', OC_API::USER_AUTH, array(), array('type' => 'user|group|link|email|contact|remote', 'path' => '.*'));
+
+?>
\ No newline at end of file
diff --git a/apps/files_sharing/lib/api.php b/apps/files_sharing/lib/api.php
new file mode 100644 (file)
index 0000000..b1dc0d9
--- /dev/null
@@ -0,0 +1,46 @@
+<?php
+
+class OC_Sharing_API {
+       
+       static public function shareFile($parameters) {
+               $path = '/'.$parameters['path'];
+               $fileid = OC_FileCache::getId($path);
+               $typemap = array(
+                       'user' => OCP\Share::SHARE_TYPE_USER,
+                       'group' => OCP\Share::SHARE_TYPE_GROUP,
+                       'link' => OCP\Share::SHARE_TYPE_LINK,
+                       'email' => OCP\Share::SHARE_TYPE_EMAIL,
+                       'contact' => OCP\Share::SHARE_TYPE_CONTACT,
+                       'remote' => OCP\Share::SHARE_TYPE_USER,
+                       );
+               $type = $typemap[$parameters['type']];
+               $shareWith = isset($_POST['shareWith']) ? $_POST['shareWith'] : '';
+               $permissionstring = isset($_POST['permissions']) ? $_POST['permissions'] : '';
+               $permissionmap = array(
+                       'C' => OCP\Share::PERMISSION_CREATE,
+                       'R' => OCP\Share::PERMISSION_READ,
+                       'U' => OCP\Share::PERMISSION_UPDATE,
+                       'D' => OCP\Share::PERMISSION_DELETE,
+                       'S' => OCP\Share::PERMISSION_SHARE,
+                       );
+               $permissions = 0;
+               foreach($permissionmap as $letter => $permission) {
+                       if(strpos($permissionstring, $letter) !== false) {
+                               $permissions += $permission;
+                       }
+               }
+
+               try {
+                       OCP\Share::shareItem('file', $fileid, $type, $shareWith, $permissions);
+               } catch (Exception $e){
+                       error_log($e->getMessage());
+               }
+               switch($type){
+                       case OCP\Share::SHARE_TYPE_LINK:
+                               return array('url' => OC_Helper::linkToPublic('files') . '&file=' . OC_User::getUser() . '/files' . $path);
+                       break;
+               }
+               
+       }
+       
+}
\ No newline at end of file
index 29403030233f78ed84b50e06d7c8586ac426d6bd..d11c3799d9a396725b1b13fe21dd7c07b761dd16 100644 (file)
@@ -91,7 +91,7 @@ class OC_API {
                // Loop through registered actions
                foreach(self::$actions[$name] as $action){
                        $app = $action['app'];
-                       // Authorsie this call
+                       // Authorise this call
                        if(self::isAuthorised($action)){
                                if(is_callable($action['action'])){
                                        $responses[] = array('app' => $app, 'response' => call_user_func($action['action'], $parameters));