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
|
|
|
* @inheritdoc |
88
|
|
|
*/ |
89
|
|
|
public function init() |
90
|
|
|
{ |
91
|
|
|
if (empty($this->clientId)) { |
92
|
|
|
throw new InvalidConfigException('The "clientId" property must be set.'); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
if (empty($this->clientSecret)) { |
96
|
|
|
throw new InvalidConfigException('The "clientSecret" property must be set.'); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
if (empty($this->refreshToken)) { |
100
|
|
|
throw new InvalidConfigException('The "refreshToken" property must be set.'); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
if (!empty($this->teamDriveId)) { |
104
|
|
|
$this->options['teamDriveId'] = $this->teamDriveId; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
if (!empty($this->sharedFolderId)) { |
108
|
|
|
$this->options['sharedFolderId'] = $this->sharedFolderId; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
if (empty($this->secret)) { |
112
|
|
|
throw new InvalidConfigException('The "secret" property must be set.'); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
$this->initEncrypter($this->secret); |
116
|
|
|
|
117
|
|
|
parent::init(); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @return GoogleDriveAdapter |
122
|
|
|
*/ |
123
|
|
|
protected function initAdapter() |
124
|
|
|
{ |
125
|
|
|
$client = new Client(); |
126
|
|
|
$client->setClientId($this->clientId); |
127
|
|
|
$client->setClientSecret($this->clientSecret); |
128
|
|
|
$client->refreshToken(refreshToken: $this->refreshToken); |
129
|
|
|
|
130
|
|
|
if (!empty($this->applicationName)) { |
131
|
|
|
$client->setApplicationName($this->applicationName); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
$this->client = $client; |
135
|
|
|
$service = new Drive($this->client); |
136
|
|
|
|
137
|
|
|
return new GoogleDriveAdapter($service, $this->prefix, $this->options); |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|