|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Quantum PHP Framework |
|
5
|
|
|
* |
|
6
|
|
|
* An open source software development framework for PHP |
|
7
|
|
|
* |
|
8
|
|
|
* @package Quantum |
|
9
|
|
|
* @author Arman Ag. <[email protected]> |
|
10
|
|
|
* @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org) |
|
11
|
|
|
* @link http://quantum.softberg.org/ |
|
12
|
|
|
* @since 2.9.5 |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
namespace Quantum\Libraries\Storage\Adapters\GoogleDrive; |
|
16
|
|
|
|
|
17
|
|
|
use Quantum\Libraries\Storage\Contracts\FilesystemAdapterInterface; |
|
18
|
|
|
use Exception; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Class GoogleDriveFileSystemAdapter |
|
22
|
|
|
* @package Quantum\Libraries\Storage |
|
23
|
|
|
*/ |
|
24
|
|
|
class GoogleDriveFileSystemAdapter implements FilesystemAdapterInterface |
|
25
|
|
|
{ |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var GoogleDriveApp |
|
29
|
|
|
*/ |
|
30
|
|
|
private $googleDriveApp; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @param GoogleDriveApp $googleDriveApp |
|
34
|
|
|
*/ |
|
35
|
|
|
public function __construct(GoogleDriveApp $googleDriveApp) |
|
36
|
|
|
{ |
|
37
|
|
|
$this->googleDriveApp = $googleDriveApp; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @inheritDoc |
|
42
|
|
|
*/ |
|
43
|
|
|
public function makeDirectory(string $dirname, ?string $parentId = null): bool |
|
44
|
|
|
{ |
|
45
|
|
|
try{ |
|
46
|
|
|
$data = [ |
|
47
|
|
|
'name' => $dirname, |
|
48
|
|
|
'mimeType' => GoogleDriveApp::FOLDER_MIMETYPE, |
|
49
|
|
|
'parents' => $parentId ? [$parentId] : ['root'] |
|
50
|
|
|
]; |
|
51
|
|
|
|
|
52
|
|
|
$this->googleDriveApp->rpcRequest(GoogleDriveApp::FILE_METADATA_URL, $data); |
|
53
|
|
|
return true; |
|
54
|
|
|
} catch (Exception $e) { |
|
55
|
|
|
return false; |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @inheritDoc |
|
61
|
|
|
*/ |
|
62
|
|
|
public function removeDirectory(string $dirname): bool |
|
63
|
|
|
{ |
|
64
|
|
|
return $this->remove($dirname); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @inheritDoc |
|
69
|
|
|
*/ |
|
70
|
|
|
public function get(string $filename) |
|
71
|
|
|
{ |
|
72
|
|
|
try { |
|
73
|
|
|
return (string)$this->googleDriveApp->getFileInfo($filename, true); |
|
74
|
|
|
} catch (Exception $e) { |
|
75
|
|
|
return false; |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @inheritDoc |
|
81
|
|
|
*/ |
|
82
|
|
|
public function put(string $filename, string $content, ?string $parentId = null) |
|
83
|
|
|
{ |
|
84
|
|
|
try { |
|
85
|
|
|
if($this->isFile($filename)){ |
|
86
|
|
|
return $this->googleDriveApp->rpcRequest(GoogleDriveApp::FILE_MEDIA_URL . '/' . $filename . '?uploadType=media', $content, 'PATCH', 'application/octet-stream'); |
|
87
|
|
|
}else{ |
|
88
|
|
|
$data = [ |
|
89
|
|
|
'name' => $filename, |
|
90
|
|
|
'parents' => $parentId ? [$parentId] : ['root'] |
|
91
|
|
|
]; |
|
92
|
|
|
|
|
93
|
|
|
$newFile = $this->googleDriveApp->rpcRequest(GoogleDriveApp::FILE_METADATA_URL, $data); |
|
94
|
|
|
|
|
95
|
|
|
return $this->put($newFile->id, $content); |
|
96
|
|
|
} |
|
97
|
|
|
} catch (Exception $e) { |
|
98
|
|
|
return false; |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @inheritDoc |
|
104
|
|
|
*/ |
|
105
|
|
|
public function append(string $filename, string $content) |
|
106
|
|
|
{ |
|
107
|
|
|
$fileContent = $this->get($filename); |
|
108
|
|
|
|
|
109
|
|
|
return $this->put($filename, $fileContent . $content); |
|
|
|
|
|
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* @inheritDoc |
|
114
|
|
|
*/ |
|
115
|
|
|
public function rename(string $oldName, string $newName): bool |
|
116
|
|
|
{ |
|
117
|
|
|
try { |
|
118
|
|
|
$data = [ |
|
119
|
|
|
'name' => $newName |
|
120
|
|
|
]; |
|
121
|
|
|
|
|
122
|
|
|
$this->googleDriveApp->rpcRequest(GoogleDriveApp::FILE_METADATA_URL . '/' . $oldName, $data, 'PATCH'); |
|
123
|
|
|
return true; |
|
124
|
|
|
} catch (Exception $e) { |
|
125
|
|
|
return false; |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* @inheritDoc |
|
131
|
|
|
*/ |
|
132
|
|
|
public function copy(string $source, string $dest = 'root'): bool |
|
133
|
|
|
{ |
|
134
|
|
|
try { |
|
135
|
|
|
$data = [ |
|
136
|
|
|
'parents' => [$dest] |
|
137
|
|
|
]; |
|
138
|
|
|
|
|
139
|
|
|
$this->googleDriveApp->rpcRequest(GoogleDriveApp::FILE_METADATA_URL . '/' . $source . '/copy', $data); |
|
140
|
|
|
return true; |
|
141
|
|
|
} catch (Exception $e) { |
|
142
|
|
|
return false; |
|
143
|
|
|
} |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* @inheritDoc |
|
148
|
|
|
*/ |
|
149
|
|
|
public function exists(string $filename): bool |
|
150
|
|
|
{ |
|
151
|
|
|
return $this->isFile($filename); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* @inheritDoc |
|
156
|
|
|
*/ |
|
157
|
|
|
public function size(string $filename) |
|
158
|
|
|
{ |
|
159
|
|
|
try { |
|
160
|
|
|
$meta = (array)$this->googleDriveApp->getFileInfo($filename); |
|
161
|
|
|
return $meta['size']; |
|
162
|
|
|
} catch (Exception $e) { |
|
163
|
|
|
return false; |
|
164
|
|
|
} |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
/** |
|
168
|
|
|
* @inheritDoc |
|
169
|
|
|
*/ |
|
170
|
|
|
public function lastModified(string $filename) |
|
171
|
|
|
{ |
|
172
|
|
|
try { |
|
173
|
|
|
$meta = (array)$this->googleDriveApp->getFileInfo($filename); |
|
174
|
|
|
return !empty($meta['modifiedTime']) ? strtotime($meta['modifiedTime']) : false; |
|
175
|
|
|
} catch (Exception $e) { |
|
176
|
|
|
return false; |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
/** |
|
182
|
|
|
* @inheritDoc |
|
183
|
|
|
*/ |
|
184
|
|
|
public function remove(string $filename): bool |
|
185
|
|
|
{ |
|
186
|
|
|
try { |
|
187
|
|
|
$this->googleDriveApp->rpcRequest(GoogleDriveApp::FILE_METADATA_URL . '/' . $filename, [], 'DELETE'); |
|
188
|
|
|
return true; |
|
189
|
|
|
} catch (Exception $e) { |
|
190
|
|
|
return false; |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
/** |
|
196
|
|
|
* @inheritDoc |
|
197
|
|
|
*/ |
|
198
|
|
|
public function isFile(string $filename): bool |
|
199
|
|
|
{ |
|
200
|
|
|
try { |
|
201
|
|
|
$meta = (array)$this->googleDriveApp->getFileInfo($filename); |
|
202
|
|
|
|
|
203
|
|
|
return $meta['kind'] === GoogleDriveApp::DRIVE_FILE_KIND && $meta['mimeType'] != GoogleDriveApp::FOLDER_MIMETYPE; |
|
204
|
|
|
} catch (Exception $e) { |
|
205
|
|
|
return false; |
|
206
|
|
|
} |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
/** |
|
210
|
|
|
* @inheritDoc |
|
211
|
|
|
*/ |
|
212
|
|
|
public function isDirectory(string $dirname): bool |
|
213
|
|
|
{ |
|
214
|
|
|
try { |
|
215
|
|
|
$meta = (array)$this->googleDriveApp->getFileInfo($dirname); |
|
216
|
|
|
|
|
217
|
|
|
return $meta['kind'] === GoogleDriveApp::DRIVE_FILE_KIND && $meta['mimeType'] === GoogleDriveApp::FOLDER_MIMETYPE; |
|
218
|
|
|
} catch (Exception $e) { |
|
219
|
|
|
return false; |
|
220
|
|
|
} |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
/** |
|
224
|
|
|
* @inheritDoc |
|
225
|
|
|
*/ |
|
226
|
|
|
public function listDirectory(string $dirname) |
|
227
|
|
|
{ |
|
228
|
|
|
try { |
|
229
|
|
|
$params = [ |
|
230
|
|
|
'q' => "'$dirname' in parents and trashed = false", |
|
231
|
|
|
'fields' => '*' |
|
232
|
|
|
]; |
|
233
|
|
|
$response = (array)$this->googleDriveApp->rpcRequest(GoogleDriveApp::FILE_METADATA_URL . '?' . http_build_query($params), [], 'GET'); |
|
234
|
|
|
return $response["files"]; |
|
235
|
|
|
} catch (Exception $e) { |
|
236
|
|
|
return false; |
|
237
|
|
|
} |
|
238
|
|
|
} |
|
239
|
|
|
} |