Отображение Google Drive в elFinder с помощью flysystem - PullRequest
0 голосов
/ 15 ноября 2018

Я хочу создать флайсистему Google Drive. Я создаю это приложение на основе Yii2, но я не против, если вы дадите мне другое решение в php.

Хорошо, пока это то, что я сделал.

  1. composer require creocoder/yii2-flysystem для Flysystem
  2. composer require nao-pon/flysystem-google-drive:~1.1 для адаптера

Я создаю конфигурацию в config/web.php

'components' => [
    'googleDrive' => [
        'class' => 'app\modules\superAdmin\components\GoogleDriveFilesystem',
        'clientId' => '{myIDHere}',
        'clientSecret' => '{secretHere}',
        'refreshToken' => '{tokenHere}',
        'rootFolderId' => ''
    ],
]

тогда это GoogleDriveFilesystem, который я расширяю от creocoder\flysystem\Filesystem;

namespace app\modules\superAdmin\components;


use creocoder\flysystem\Filesystem;
use elFinder;
use elFinderConnector;
use Google_Client;
use Google_Service_Drive;
use Hypweb\Flysystem\GoogleDrive\GoogleDriveAdapter;
use yii\base\InvalidConfigException;

class GoogleDriveFilesystem extends Filesystem
{
    public $clientId;
    public $clientSecret;
    public $refreshToken;
    public $rootFolderId;
    public $adapter;

    /**
     * @inheritdoc
     */
    public function init()
    {
        if ($this->clientId === null) {
            throw new InvalidConfigException('The "clientId" property must be set.');
        }
        if ($this->clientSecret === null) {
            throw new InvalidConfigException('The "clientSecret" property must be set.');
        }
        if ($this->refreshToken === null) {
            throw new InvalidConfigException('The "refreshToken" property must be set.');
        }
        parent::init();
    }

    /**
     * @return GoogleDriveAdapter
     */
    protected function prepareAdapter()
    {
        $client = new Google_Client();
        $client->setClientId($this->clientId);
        $client->setClientSecret($this->clientSecret);
        $client->refreshToken($this->refreshToken);
        $service = new Google_Service_Drive($client);

        $this->adapter = new GoogleDriveAdapter($service, $this->rootFolderId, [
            'useHasDir' => true
        ]);
        return $this->adapter;
    }


    public function displayInElFinder()
    {
        $googleDrive = $this->adapter;
        // Make Flysystem adapter and cache object
        $useCache = true;
        if ($useCache) {
            // Example to Flysystem cacheing
            $cache = new \League\Flysystem\Cached\Storage\Adapter(
                new \League\Flysystem\Adapter\Local('flycache'),
                'gdcache',
                300
            );

            // Flysystem cached adapter
            $adapter = new \League\Flysystem\Cached\CachedAdapter(
                $googleDrive,
                $cache
            );
        } else {
            // Not use cached adapter
            $cache = null;
            $adapter = $googleDrive;
        }

        // Google Drive elFinder Volume driver
        $gdrive = [
            // require
            'driver' => 'FlysystemExt',
            'filesystem' => new \League\Flysystem\Filesystem($adapter),
            'fscache' => $cache,
            'separator' => '/',
            'alias' => 'GoogleDrive',
            'rootCssClass' => 'elfinder-navbar-root-googledrive'
        ];

        // elFinder volume roots options
        $elFinderOpts = [
            'roots' => []
        ];

        $elFinderOpts['roots'][] = $gdrive;

        // run elFinder
        $connector = new elFinderConnector(new elFinder($elFinderOpts));
        $connector->run();;
    }
}

Для тестирования я использую

<code><?php $obj = Yii::$app->googleDrive;?>

<pre>
   <?= \yii\helper\VarDumper::dumpAsString($obj->listContents()) ?>

У меня один успех.

[
0 => [
    'name' => 'Contoh BL'
    'type' => 'dir'
    'path' => '1oF2A4SVTGL3zq9XDRF7o2hIzZ66IFeFS'
    'filename' => 'Contoh BL'
    'extension' => ''
    'timestamp' => 1542228118
    'size' => 0
    'hasdir' => false
    'dirname' => ''
    'basename' => '1oF2A4SVTGL3zq9XDRF7o2hIzZ66IFeFS'
]
]  

Но как отобразить их в elFinder, Я пытаюсь так,

<div class="row">
    <div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
        <?php $obj = Yii::$app->googleDrive; ?>

        <?php $obj->displayInElFinder() ?>
    </div>
</div>

Я получил такую ​​ошибку:

{
  "error": [
     "errUnknownCmd"
  ]
}
...