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