|
1
|
|
|
<?php |
|
2
|
|
|
namespace phpbu\App\Backup\Collector; |
|
3
|
|
|
|
|
4
|
|
|
use Google_Client; |
|
5
|
|
|
use Google_Service_Drive; |
|
6
|
|
|
use phpbu\App\Backup\Collector; |
|
7
|
|
|
use phpbu\App\Backup\File; |
|
8
|
|
|
use phpbu\App\Backup\Path; |
|
9
|
|
|
use phpbu\App\Backup\Target; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* GoogleDrive class. |
|
13
|
|
|
* |
|
14
|
|
|
* @package phpbu |
|
15
|
|
|
* @subpackage Backup |
|
16
|
|
|
* @author Sebastian Feldmann <[email protected]> |
|
17
|
|
|
* @copyright Sebastian Feldmann <[email protected]> |
|
18
|
|
|
* @license https://opensource.org/licenses/MIT The MIT License (MIT) |
|
19
|
|
|
* @link http://phpbu.de/ |
|
20
|
|
|
* @since Class available since Release 5.1.0 |
|
21
|
|
|
*/ |
|
22
|
|
|
class GoogleDrive extends Remote implements Collector |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* Google drive api service. |
|
26
|
|
|
* |
|
27
|
|
|
* @var \Google_Service_Drive |
|
28
|
|
|
*/ |
|
29
|
|
|
private $service; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Parent folder id. |
|
33
|
|
|
* |
|
34
|
|
|
* @var string |
|
35
|
|
|
*/ |
|
36
|
|
|
private $parent; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Constructor. |
|
40
|
|
|
* |
|
41
|
|
|
* @param \phpbu\App\Backup\Target $target |
|
42
|
|
|
* @param \phpbu\App\Backup\Path $path |
|
43
|
|
|
* @param \Google_Service_Drive $service |
|
44
|
|
|
*/ |
|
45
|
2 |
|
public function __construct(Target $target, Path $path, Google_Service_Drive $service) |
|
46
|
|
|
{ |
|
47
|
2 |
|
$this->setUp($target, $path); |
|
48
|
2 |
|
$this->service = $service; |
|
49
|
2 |
|
$this->parent = $path->getPath(); |
|
50
|
2 |
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Collect all created backups. |
|
54
|
|
|
* |
|
55
|
|
|
* @return void |
|
56
|
|
|
*/ |
|
57
|
2 |
|
protected function collectBackups() |
|
58
|
|
|
{ |
|
59
|
2 |
|
$results = $this->service->files->listFiles($this->getParams()); |
|
60
|
|
|
|
|
61
|
|
|
/** @var \Google_Service_Drive_DriveFile $googleFile */ |
|
62
|
2 |
|
foreach ($results->getFiles() as $googleFile) { |
|
63
|
1 |
|
if ($this->isFileMatch($this->path->getPath() . '/' . $googleFile->getName())) { |
|
64
|
1 |
|
$file = new File\GoogleDrive($this->service, $googleFile); |
|
65
|
1 |
|
$index = $this->getFileIndex($file); |
|
66
|
1 |
|
$this->files[$index] = $file; |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
2 |
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Return google api params list to find all backups. |
|
73
|
|
|
* |
|
74
|
|
|
* @return array |
|
75
|
|
|
*/ |
|
76
|
2 |
|
private function getParams() : array |
|
77
|
|
|
{ |
|
78
|
|
|
return [ |
|
79
|
2 |
|
'includeTeamDriveItems' => false, |
|
80
|
2 |
|
'pageSize' => 1000, |
|
81
|
2 |
|
'fields' => 'nextPageToken, files(id, name, createdTime, size)', |
|
82
|
2 |
|
'spaces' => 'drive', |
|
83
|
2 |
|
'q' => 'trashed = false AND visibility = \'limited\'' . $this->getParentsFilter(), |
|
84
|
|
|
]; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Return parent filter query. |
|
89
|
|
|
* |
|
90
|
|
|
* @return string |
|
91
|
|
|
*/ |
|
92
|
2 |
|
private function getParentsFilter() : string |
|
93
|
|
|
{ |
|
94
|
2 |
|
return empty($this->parent) ? '' : ' AND \'' . $this->parent . '\' in parents'; |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|