Filesystem

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():

<?php
namespace OCA\MyApp\AppInfo;

use \OCP\AppFramework\App;
use \OCA\MyApp\Storage\AuthorStorage;

class Application extends App {

    public function __construct(array $urlParams=[]){
        parent::__construct('myapp', $urlParams);

        $container = $this->getContainer();

        /**
         * Storage Layer
         */
        $container->registerService('AuthorStorage', function($c) {
            return new AuthorStorage($c->query('RootStorage'));
        });

        $container->registerService('RootStorage', function($c) {
            return $c->query('ServerContainer')->getRootFolder();
        });

    }
}

Writing to a File

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 owncloud/data/myfile.txt you should get the root folder and use:

<?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) {
                $file = $this->storage->newFile('/myfile.txt');
            }

            // the id can be accessed by $file->getId();
            $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.

<?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');
        }
    }
}

How to Get the Storage Owner Using a File Id

A storage’s owner can be retrieved using a file id, as in the following example.

<?php

$mountCache = \OC::$server->getMountProviderCollection()->getMountCache();

$mounts = $mountCache->getMountsForFileId($fileId);
$userWithAccessToFile = array_map(function(ICachedMountInfo $mount) {
    return $mount->getUser();
}, $mounts);

$mounts = $mountCache->getMountsForFileId($fileId);
if (count($mounts) > 0) {
    $node = $mounts[0]->getMountPointNode();
    $owner = $node->getOwner();
}