[Pkg-owncloud-commits] [owncloud-doc] 82/270: add filesystem docs

David Prévot taffit at moszumanska.debian.org
Thu Jul 31 03:53:03 UTC 2014


This is an automated email from the git hooks/post-receive script.

taffit pushed a commit to branch master
in repository owncloud-doc.

commit 6c10d21d44687498ea777ee2044286813efda101
Author: Bernhard Posselt <dev at bernhard-posselt.com>
Date:   Mon May 12 16:40:31 2014 +0200

    add filesystem docs
---
 developer_manual/app/filesystem.rst | 102 +++++++++++++++++++++++++++++++++++-
 1 file changed, 101 insertions(+), 1 deletion(-)

diff --git a/developer_manual/app/filesystem.rst b/developer_manual/app/filesystem.rst
index 39b97e7..3cc0aad 100644
--- a/developer_manual/app/filesystem.rst
+++ b/developer_manual/app/filesystem.rst
@@ -2,4 +2,104 @@
 Filesystem
 ==========
 
-.. sectionauthor:: Bernhard Posselt <dev at bernhard-posselt.com>
\ No newline at end of file
+.. sectionauthor:: Bernhard Posselt <dev at bernhard-posselt.com>
+
+Because users can choose their storage backend, the filesystem should be accessed by using the appropriate filesystem classes.
+
+Filesystem classes can be injected from the ServerContainer by calling the method **getRootFolder()**, **getUserFolder()** or **getAppFolder()**:
+
+.. code-block:: php
+
+    <?php
+    namespace OCA\MyApp\AppInfo;
+
+    use \OCP\AppFramework\App;
+
+    use \OCA\MyApp\Storage\AuthorStorage;
+
+
+    class Application extends App {
+
+        public function __construct(array $urlParams=array()){
+            parent::__construct('myapp', $urlParams);
+
+            $container = $this->getContainer();
+
+            /**
+             * Database Layer
+             */
+            $container->registerService('AuthorStorage', function($c) {
+                return new AuthorStorage($c->query('ServerContainer')->getRootFolder());
+            });
+        }
+    }
+
+Writing to a file
+=================
+
+All methods return a Folder object on which files and folders can be accessed, or filesystem operatoins can be performed relatively to their root. For instance for writing to file:`owncloud/data/myfile.txt` you should get the root folder and use:
+
+.. code-block:: php
+
+    <?php
+    namespace OCA\MyApp\Storage;
+
+    class AuthorStorage {
+
+        private $storage;
+
+        public function __construct($storage){
+            $this->storage = $storage;
+        }
+
+        public function writeTxt($content) {
+            // check if file exists and write to it if possible
+            try {
+                try {
+                    $file = $this->storage->get('/myfile.txt');
+                } catch(\OCP\Files\NotFoundException $e) {
+                    $this->storage->touch('/myfile.txt');
+                    $file = $this->storage->get('/myfile.txt');
+                }
+
+                $file->putContent($content);
+        
+            } catch(\OCP\Files\NotPermittedException $e) {
+                // you have to create this exception by yourself ;)
+                throw new StorageException('Cant write to file');
+            }
+        }
+    }
+
+Reading from a file
+===================
+
+Files and folders can also be accessed by id, by calling the **getById** method on the folder.
+
+.. code-block:: php
+
+    <?php
+    namespace OCA\MyApp\Storage;
+
+    class AuthorStorage {
+
+        private $storage;
+
+        public function __construct($storage){
+            $this->storage = $storage;
+        }
+
+        public function getContent($id) {
+            // check if file exists and write to it if possible
+            try {
+                $file = $this->storage->getById($id);
+                if($file instanceof \OCP\Files\File) {
+                    return $file->getContent();
+                } else {
+                    throw new StorageException('Can not read from folder');
+                }
+            } catch(\OCP\Files\NotFoundException $e) {
+                throw new StorageException('File does not exist');
+            }
+        }
+    }
\ No newline at end of file

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-owncloud/owncloud-doc.git



More information about the Pkg-owncloud-commits mailing list