Skip to content

Commit

Permalink
✨ Add Google Drive
Browse files Browse the repository at this point in the history
  • Loading branch information
sugeng-sulistiyawan committed Oct 18, 2024
1 parent c98abea commit 4f15224
Show file tree
Hide file tree
Showing 3 changed files with 167 additions and 1 deletion.
42 changes: 41 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ This extension provides [Flysystem 3](https://flysystem.thephpleague.com) integr
[![Latest Stable Version](https://img.shields.io/packagist/v/diecoding/yii2-flysystem?label=stable)](https://packagist.org/packages/diecoding/yii2-flysystem)
[![Total Downloads](https://img.shields.io/packagist/dt/diecoding/yii2-flysystem)](https://packagist.org/packages/diecoding/yii2-flysystem)
[![Latest Stable Release Date](https://img.shields.io/github/release-date/sugeng-sulistiyawan/yii2-flysystem)](https://github.com/sugeng-sulistiyawan/yii2-flysystem)
[![Quality Score](https://img.shields.io/scrutinizer/quality/g/sugeng-sulistiyawan/yii2-flysystem)](https://scrutinizer-ci.com/g/sugeng-sulistiyawan/yii2-flysystem)
<!-- [![Quality Score](https://img.shields.io/scrutinizer/quality/g/sugeng-sulistiyawan/yii2-flysystem)](https://scrutinizer-ci.com/g/sugeng-sulistiyawan/yii2-flysystem) -->
[![Build Status](https://img.shields.io/travis/com/sugeng-sulistiyawan/yii2-flysystem)](https://app.travis-ci.com/sugeng-sulistiyawan/yii2-flysystem)
[![License](https://img.shields.io/github/license/sugeng-sulistiyawan/yii2-flysystem)](https://github.com/sugeng-sulistiyawan/yii2-flysystem)
[![PHP Version Require](https://img.shields.io/packagist/dependency-v/diecoding/yii2-flysystem/php?color=6f73a6)](https://packagist.org/packages/diecoding/yii2-flysystem)
Expand All @@ -31,6 +31,7 @@ This extension provides [Flysystem 3](https://flysystem.thephpleague.com) integr
- [SFTP Filesystem](#sftp-filesystem)
- [WebDAV Filesystem](#webdav-filesystem)
- [ZipArchive Filesystem](#ziparchive-filesystem)
- [Google Drive Filesystem](#google-drive-filesystem)
- [Additional Configuration](#additional-configuration)
- [URL File Action Settings](#url-file-action-settings)
- [Global Visibility Settings](#global-visibility-settings)
Expand Down Expand Up @@ -86,10 +87,12 @@ or add to the require section of your `composer.json` file.

- [league/flysystem-async-aws-s3](https://github.com/thephpleague/flysystem-async-aws-s3)
- [league/flysystem-aws-s3-v3](https://github.com/thephpleague/flysystem-aws-s3-v3)
- [league/flysystem-google-cloud-storage](https://github.com/thephpleague/flysystem-google-cloud-storage)
- [league/flysystem-ftp](https://github.com/thephpleague/flysystem-ftp)
- [league/flysystem-sftp-v3](https://github.com/thephpleague/flysystem-sftp-v3)
- [league/flysystem-webdav](https://github.com/thephpleague/flysystem-webdav)
- [league/flysystem-ziparchive](https://github.com/thephpleague/flysystem-ziparchive)
- [masbug/flysystem-google-drive-ext](https://github.com/masbug/flysystem-google-drive-ext)

## Configuration

Expand Down Expand Up @@ -393,6 +396,43 @@ return [
];
```

### Google Drive Filesystem

Either run

```shell
composer require masbug/flysystem-google-drive-ext
```

or add

```shell
"masbug/flysystem-google-drive-ext": "^2.0"
```

to the `require` section of your `composer.json` file and configure application `components` as follows

```php
return [
// ...
'components' => [
// ...
'fs' => [
'class' => \diecoding\flysystem\GoogleDriveComponent::class,
'applicationName' => 'My Google Drive App',
'clientId' => '',
'clientSecret' => '',
'refreshToken' => '',
// 'teamDriveId' => '',
// 'sharedFolderId' => '',
// 'options' => [],
// 'debug' => false,
// 'prefix' => '',
],
],
];
```

## Additional Configuration

### URL File Action Settings
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"league/flysystem-sftp-v3": "^3.0",
"league/flysystem-webdav": "^3.0",
"league/flysystem-ziparchive": "^3.0",
"masbug/flysystem-google-drive-ext": "^2.0",
"phpunit/phpunit": "~9.5.0"
},
"autoload": {
Expand Down
125 changes: 125 additions & 0 deletions src/GoogleDriveComponent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
<?php

namespace diecoding\flysystem;

use Google\Client;
use Google\Service\Drive;
use Masbug\Flysystem\GoogleDriveAdapter;
use yii\base\InvalidConfigException;

/**
* Interacting with Google Drive filesystem
* @see https://github.com/masbug/flysystem-google-drive-ext
*
* ```php
* 'components' => [
* 'fs' => [
* 'class' => \diecoding\flysystem\GoogleDriveComponent::class,
* 'applicationName' => 'My Google Drive App',
* 'clientId' => '',
* 'clientSecret' => '',
* 'refreshToken' => '',
* // 'teamDriveId' => '',
* // 'sharedFolderId' => '',
* // 'options' => [],
* // 'debug' => false,
* // 'prefix' => '',
* ],
* ],
* ```
*
* @link https://sugengsulistiyawan.my.id/
* @author Sugeng Sulistiyawan <sugeng.sulistiyawan@gmail.com>
* @copyright Copyright (c) 2024
*/
class GoogleDriveComponent extends AbstractComponent
{
/**
* @var string
*/
public $applicationName;

/**
* @var string
*/
public $clientId;

/**
* @var string
*/
public $clientSecret;

/**
* @var string
*/
public $refreshToken;

/**
* @var string
*/
public $teamDriveId;

/**
* @var string
*/
public $sharedFolderId;

/**
* @var array
*/
public $options = [];

/**
* @var Client
*/
protected $client;

/**
* @inheritdoc
*/
public function init()
{
if (empty($this->applicationName)) {
throw new InvalidConfigException('The "applicationName" property must be set.');
}

if (empty($this->clientId)) {
throw new InvalidConfigException('The "clientId" property must be set.');
}

if (empty($this->clientSecret)) {
throw new InvalidConfigException('The "clientSecret" property must be set.');
}

if (empty($this->refreshToken)) {
throw new InvalidConfigException('The "refreshToken" property must be set.');
}

if (!empty($this->teamDriveId)) {
$this->options['teamDriveId'] = $this->teamDriveId;
}

if (!empty($this->sharedFolderId)) {
$this->options['sharedFolderId'] = $this->sharedFolderId;
}

parent::init();
}

/**
* @return GoogleDriveAdapter
*/
protected function initAdapter()
{
$client = new Client();
$client->setClientId($this->clientId);
$client->setClientSecret($this->clientSecret);
$client->refreshToken(refreshToken: $this->refreshToken);
$client->setApplicationName($this->applicationName);

$this->client = $client;
$service = new Drive($this->client);

return new GoogleDriveAdapter($service, $this->prefix, $this->options);
}
}

0 comments on commit 4f15224

Please sign in to comment.