|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace diecoding\flysystem; |
|
4
|
|
|
|
|
5
|
|
|
use diecoding\flysystem\adapter\GoogleDriveAdapter; |
|
6
|
|
|
use diecoding\flysystem\traits\UrlGeneratorComponentTrait; |
|
7
|
|
|
use Google\Client; |
|
8
|
|
|
use Google\Service\Drive; |
|
9
|
|
|
use yii\base\InvalidConfigException; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Interacting with Google Drive filesystem |
|
13
|
|
|
* @see https://github.com/masbug/flysystem-google-drive-ext |
|
14
|
|
|
* |
|
15
|
|
|
* ```php |
|
16
|
|
|
* 'components' => [ |
|
17
|
|
|
* 'fs' => [ |
|
18
|
|
|
* 'class' => \diecoding\flysystem\GoogleDriveComponent::class, |
|
19
|
|
|
* 'applicationName' => 'My Google Drive App', |
|
20
|
|
|
* 'clientId' => '', |
|
21
|
|
|
* 'clientSecret' => '', |
|
22
|
|
|
* 'refreshToken' => '', |
|
23
|
|
|
* // 'teamDriveId' => '', |
|
24
|
|
|
* // 'sharedFolderId' => '', |
|
25
|
|
|
* // 'options' => [], |
|
26
|
|
|
* 'secret' => 'my-secret', // for secure route url |
|
27
|
|
|
* // 'action' => '/site/file', // action route |
|
28
|
|
|
* // 'prefix' => '', |
|
29
|
|
|
* ], |
|
30
|
|
|
* ], |
|
31
|
|
|
* ``` |
|
32
|
|
|
* |
|
33
|
|
|
* @link https://sugengsulistiyawan.my.id/ |
|
34
|
|
|
* @author Sugeng Sulistiyawan <[email protected]> |
|
35
|
|
|
* @copyright Copyright (c) 2024 |
|
36
|
|
|
*/ |
|
37
|
|
|
class GoogleDriveComponent extends AbstractComponent |
|
38
|
|
|
{ |
|
39
|
|
|
use UrlGeneratorComponentTrait; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @var string |
|
43
|
|
|
*/ |
|
44
|
|
|
public $applicationName; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @var string |
|
48
|
|
|
*/ |
|
49
|
|
|
public $clientId; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @var string |
|
53
|
|
|
*/ |
|
54
|
|
|
public $clientSecret; |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @var string |
|
58
|
|
|
*/ |
|
59
|
|
|
public $refreshToken; |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @var string |
|
63
|
|
|
*/ |
|
64
|
|
|
public $teamDriveId; |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @var string |
|
68
|
|
|
*/ |
|
69
|
|
|
public $sharedFolderId; |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @var string |
|
73
|
|
|
*/ |
|
74
|
|
|
public $secret; |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @var array |
|
78
|
|
|
*/ |
|
79
|
|
|
public $options = []; |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @var Client |
|
83
|
|
|
*/ |
|
84
|
|
|
protected $client; |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @var string[] |
|
88
|
|
|
*/ |
|
89
|
|
|
protected $_availableOptions = [ |
|
90
|
|
|
'teamDriveId', |
|
91
|
|
|
'sharedFolderId', |
|
92
|
|
|
]; |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @inheritdoc |
|
96
|
|
|
* @throws InvalidConfigException |
|
97
|
|
|
*/ |
|
98
|
|
|
public function init() |
|
99
|
|
|
{ |
|
100
|
|
|
$this->validateProperties([ |
|
101
|
|
|
'clientId', |
|
102
|
|
|
'clientSecret', |
|
103
|
|
|
'refreshToken', |
|
104
|
|
|
'secret', |
|
105
|
|
|
]); |
|
106
|
|
|
|
|
107
|
|
|
$this->initEncrypter($this->secret); |
|
108
|
|
|
|
|
109
|
|
|
parent::init(); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* @return GoogleDriveAdapter |
|
114
|
|
|
*/ |
|
115
|
|
|
protected function initAdapter() |
|
116
|
|
|
{ |
|
117
|
|
|
$client = new Client(); |
|
118
|
|
|
$client->setClientId($this->clientId); |
|
119
|
|
|
$client->setClientSecret($this->clientSecret); |
|
120
|
|
|
$client->refreshToken(refreshToken: $this->refreshToken); |
|
121
|
|
|
|
|
122
|
|
|
if (!empty($this->applicationName)) { |
|
123
|
|
|
$client->setApplicationName($this->applicationName); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
foreach ($this->_availableOptions as $property) { |
|
127
|
|
|
if (!empty($this->$property)) { |
|
128
|
|
|
$this->options[$property] = $this->$property; |
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
$this->client = $client; |
|
133
|
|
|
$service = new Drive($this->client); |
|
134
|
|
|
|
|
135
|
|
|
$adapter = new GoogleDriveAdapter($service, $this->prefix, $this->options); |
|
136
|
|
|
// for UrlGeneratorAdapterTrait |
|
137
|
|
|
$adapter->component = $this; |
|
138
|
|
|
|
|
139
|
|
|
return $adapter; |
|
140
|
|
|
} |
|
141
|
|
|
} |
|
142
|
|
|
|