Owncloud 8.1“myapp”示例,添加一个函数来写入文件

时间:2016-01-01 18:56:16

标签: php owncloud

在Owncloud 8.1上使用owncloud命令行,我创建了一个新的测试应用程序:

ocdev startapp MyApp --email mail@example.com --author "Your Name" --description "My first app" --owncloud 8

该应用正常运行,我可以将其添加到owncloud控制面板中。

现在我想写一个文件,所以我使用了owncloud文档中的一个例子: https://doc.owncloud.org/server/8.1/developer_manual/app/filesystem.html

[编辑]我重新开始,现在,我不知道是否省略了什么,但“myapp”没有“application.php”文件。 所以我在/var/www/core/apps/myapp/appinfo/application.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();

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

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

    }
}

然后我创建一个名为/var/www/core/apps/myapp/storage/AuthorStorage.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');
            }

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

示例应用程序已经为我提供了一个指向pagecontroller.php

中索引函数的路径
['name' => 'page#index', 'url' => '/', 'verb' => 'GET']

如何从那里调用函数“writeTxt”? 基于http://php.net/manual/en/language.oop5.basic.php 我试过了:

use \OCA\MyApp\Storage\AuthorStorage;

public function index() {
//added part
$a = new AuthorStorage();
$a->writeTxt('test');    
//original part
$params = ['user' => $this->userId];
return new TemplateResponse('myapp', 'main', $params);  //templates/main.php

}

运行后,我在/var/www/core/apps/myapp/controller/pagecontroller.php#44找不到“Class'OCA \ MyApp \ Storage \ AuthorStorage'”

即使在use \OCA\MyApp\Storage\AuthorStorage;ClassNotFoundException: Attempted to load class... Symfony)的帮助下,它似乎也无济于事。

由于

1 个答案:

答案 0 :(得分:0)

时间已经过去了,所以我发布了对于我的问题的答案,对于owncloud 9。

以下是具有读取,写入,复制文件功能的基本脚本的步骤。

“application.php”已明确从示例中删除。这不是一个错误。

下列的程序: https://doc.owncloud.org/server/9.0/developer_manual/app/startapp.html 生成演示“MyApp”应用程序:

ocdev startapp MyApp --email mail@example.com --author "Your Name" --description "My first app" --owncloud 9

编辑文件myapp / controller / pagecontroller.php

<?php
/**
 * ownCloud - myapp
 *
 * This file is licensed under the Affero General Public License version 3 or
 * later. See the COPYING file.
 *
 * @author Your Name <mail@example.com>
 * @copyright Your Name 2016
 */

namespace OCA\MyApp\Controller;

use OCP\IRequest;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\Controller;
use OCP\Files\IRootFolder;
use OC\Files\Storage\Temporary;

class PageController extends Controller {

    private $userId;
    private $storage;
    private $userstorage;

    public function __construct($AppName, IRequest $request, IRootFolder $storage, $UserId){
    parent::__construct($AppName, $request);
    $this->storage = $storage;
    $this->userId = $UserId;

    $this->userstorage = $this->storage->get($this->userId.'/files/');

    }



    /**
     * CAUTION: the @Stuff turns off security checks; for this page no admin is
     *          required and no CSRF check. If you don't know what CSRF is, read
     *          it up in the docs or you might create a security hole. This is
     *          basically the only required method to add this exemption, don't
     *          add it to any other method if you don't exactly know what it does
     *
     * @NoAdminRequired
     * @NoCSRFRequired
     */
    public function index() {

    $listedudossier = $this->userstorage->getDirectoryListing();

    //list all items in the root directory
    //and copies the files in an directory called old
    //the directory "old" is not script created
    foreach ($listedudossier as $value) {

        if ( $value->getType() == 'file' ){
            $value->copy($this->userId.'/files/old/'.$value->getName());
            //also works
            //$value->copy($this->userstorage->getPath().'/old/'.$value->getName());    
        }


    }

    $params = ['listedudossier' => $listedudossier ];
    return new TemplateResponse('myapp', 'main', $params);  // templates/main.php

    }

    /**
     * Simply method that posts back the payload of the request
     * @NoAdminRequired
     */
    public function doEcho($echo) {


    //creates a file    
    $this->userstorage->newFile('myfile2.txt');

    //opens a file, adds inputbox content and saves the file
    $file = $this->userstorage->get('myfile.txt');
    $contenu = $file->getContent(); 
    $file->putContent($contenu.$echo);

    return new DataResponse(['echo' => $echo]);
    }



}

您还需要编辑myapp / templates / part.content.php

<p>Hello World <?php p($_['user']) ?></p>

<p><button id="hello">click me</button></p>

<p><textarea id="echo-content">
    Send this as ajax
</textarea></p>
<p><button id="echo">Send ajax request</button></p>

Ajax response: <div id="echo-result"></div>

<p>listedudossier:<p> <?php 
foreach ($_['listedudossier'] as $file) {

   echo "type:".$file->getType(); 
   echo "<p>";

   echo "nom:".$file->getName(); 
   echo "<p>";

   echo "modif:".$file->getMTime(); 
   echo "<p>";

}

从这里,您可以在开发环境中测试代码: https://github.com/owncloud/ocdev/blob/master/README.rst#installation