You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
From nextcloud/server#26982
Adds some documentation for the filesystem layer, both a high level overview of how the various pieces interact and a high level guide for apps interacting with the filesystem.
Hopefully this will be useful to anyone trying to either use the filesystem or work on the filesystem.
Signed-off-by: Louis Chemineau <[email protected]>
Because users can choose their storage backend, the filesystem should be accessed by using the appropriate filesystem classes.
8
+
High level guide to using the Nextcloud filesystem API.
9
+
10
+
Because users can choose their storage backend, the filesystem should be accessed by using the appropriate filesystem classes. For a simplified filesystem for app specific data see `IAppData <appdata.html>`_
11
+
12
+
Node API
13
+
^^^^^^^^
14
+
15
+
The "Node API" is the primary api for apps to access the Nextcloud filesystem, each item in the filesystem is
16
+
represented as either a File or Folder node with each node providing access to the relevant filesystem information
17
+
and actions for the node.
18
+
19
+
20
+
Getting access
21
+
--------------
22
+
23
+
Access to the filesystem is provided by the ``IRootFolder`` which can be injected into your class.
24
+
From the root folder you can either access a user's home folder or access a file or folder by its absolute path.
25
+
26
+
.. code-block:: php
27
+
28
+
use OCP\Files\IRootFolder;
29
+
use OCP\IUserSession;
30
+
31
+
class FileSystemAccessExample {
32
+
private IUserSession $userSession;
33
+
private IRootFolder $rootFolder;
34
+
35
+
public function __constructor(IUserSession $userSession, IRootFolder $rootFolder) {
36
+
$this->userSession = $userSession;
37
+
$this->rootFolder = $rootFolder;
38
+
}
39
+
40
+
/**
41
+
* Create a new file with specified content in the home folder of the current user
42
+
* returning the size of the resulting file.
43
+
*/
44
+
public function getCurrentUserFolder(string $path, string $content): int {
45
+
$user = $this->userSession->getUser();
46
+
47
+
if ($user === null) {
48
+
return null;
49
+
}
50
+
51
+
// the "user folder" corresponds to the root of the user visible files
For more details on the specific methods provided by file and folder nodes see the method documentation from the ``OCP\Files\File`` and ``OCP\Files\Folder`` interfaces.
8
57
9
-
Filesystem classes can be injected automatically with dependency injection. This is the user filesystem.
10
-
For a simplified filestystem for app specific data see `IAppData <appdata.html>`_
11
58
12
59
Writing to a file
13
60
-----------------
14
61
15
-
16
62
All methods return a Folder object on which files and folders can be accessed, or filesystem operations can be performed relatively to their root. For instance for writing to file:`nextcloud/data/myfile.txt` you should get the root folder and use:
17
63
18
64
.. code-block:: php
19
65
20
-
<?php
21
-
namespace OCA\MyApp\Storage;
22
-
23
66
use OCP\Files\IRootFolder;
24
67
25
-
class AuthorStorage {
68
+
class FileWritingExample {
26
69
27
-
/** @var IRootStorage */
28
-
private $storage;
70
+
private IRootStorage $storage;
29
71
30
72
public function __construct(IRootFolder $storage){
0 commit comments