PluginProbe ʕ •ᴥ•ʔ
JetBackup – Backup, Restore & Migrate / 3.1.22.4
JetBackup – Backup, Restore & Migrate v3.1.22.4
3.1.22.4 3.1.22.3 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.4.8 1.4.8.1 1.4.9 1.5.0 1.5.1 1.5.1.1 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.6.0 1.6.10 1.6.11 1.6.12 1.6.13 1.6.15 1.6.5.1 1.6.8.8 1.6.9 1.6.9.1 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7.5 2.0.8.7 2.0.9.11 2.0.9.14 2.0.9.15 2.0.9.6 2.0.9.7 2.0.9.9 3.1.10.7 3.1.11.1 3.1.12.3 3.1.13.4 3.1.14.17 3.1.15.4 3.1.16.1 3.1.17.5 3.1.18.10 3.1.18.8 3.1.18.9 3.1.19.8 3.1.20.3 3.1.21.3 3.1.7.9 3.1.9.2 trunk 1.1.90 1.1.91 1.2.0 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.6 1.3.7 1.3.8 1.3.9 1.4.0 1.4.1 1.4.2
backup / src / JetBackup / Destination / Vendors / GoogleDrive / DirIterator.php
backup / src / JetBackup / Destination / Vendors / GoogleDrive Last commit date
Client 1 year ago .htaccess 1 year ago Cache.php 1 year ago ChunkedDownload.php 1 year ago ChunkedUpload.php 1 year ago DirIterator.php 1 year ago GoogleDrive.php 1 year ago index.html 1 year ago web.config 1 year ago
DirIterator.php
139 lines
1 <?php
2 /*
3 *
4 * JetBackup @ package
5 * Created By Idan Ben-Ezra
6 *
7 * Copyrights @ JetApps
8 * https://www.jetapps.com
9 *
10 **/
11 namespace JetBackup\Destination\Vendors\GoogleDrive;
12
13 use Exception;
14 use JetBackup\Destination\DestinationFile;
15 use JetBackup\Destination\Integration\DestinationDirIterator;
16 use JetBackup\Destination\Integration\DestinationFile as iDestinationFile;
17 use JetBackup\Destination\Vendors\GoogleDrive\Client\Client;
18 use JetBackup\Destination\Vendors\GoogleDrive\Client\File;
19 use JetBackup\Destination\Vendors\GoogleDrive\Client\ListFiles;
20 use JetBackup\Exception\IOException;
21 use JetBackup\Exception\JBException;
22
23 defined( '__JETBACKUP__' ) or die( 'Restricted access' );
24
25 class DirIterator implements DestinationDirIterator {
26
27 const CHUNK_LIMIT = 1000;
28
29 private GoogleDrive $_destination;
30 private string $_directory;
31 private ?ListFiles $_list=null;
32 private ?string $_parent_id=null;
33 /** @var File[] */
34 private array $_files=[];
35
36 /**
37 * @param GoogleDrive $destination
38 * @param string $directory
39 * @param string|null $parent_id
40 *
41 * @throws IOException
42 * @throws JBException
43 */
44 public function __construct(GoogleDrive $destination, string $directory, ?string $parent_id=null) {
45 $this->_destination = $destination;
46 $this->_directory = $directory;
47
48 if(!$this->_destination->getClient()) throw new IOException("Unable to retrieve google drive service");
49 $path = $this->_destination->getRealPath($this->_directory);
50
51 if(!$parent_id) $parent_id = $this->_destination->getFileId($path == '/' ? '' : $path);
52 if(!$parent_id) return;
53
54 $this->_parent_id = $parent_id;
55 if(!$this->_destination->dirExists($this->_directory)) return;
56 $this->rewind();
57
58 }
59
60 /**
61 * @param bool $rewind
62 *
63 * @return void
64 * @throws IOException
65 */
66 private function _loadChunk(bool $rewind=false):void {
67 try {
68
69 $marker = '';
70
71 if(!$rewind && $this->_list) {
72 if(!$this->_list->getNextPageToken()) {
73 $this->_list = null;
74 return;
75 }
76 $marker = $this->_list->getNextPageToken();
77 }
78
79 $this->_list = $this->_destination->_retries(function() use ($rewind, $marker) {
80
81 $params = [
82 "fields" => 'files,nextPageToken',
83 "q" => "'$this->_parent_id' in parents and trashed = false",
84 "orderBy" => "name asc,modifiedTime desc",
85 "pageSize" => self::CHUNK_LIMIT
86 ];
87
88 if($marker) $params['pageToken'] = $marker;
89
90 return $this->_destination->getClient()->listFiles($params);
91
92 }, "Failed fetching list of files");
93
94 $this->_files = $this->_list ? $this->_list->getFiles() : [];
95
96 } catch(Exception $e) {
97 if($e->getCode() == 404) $this->_files = [];
98 else throw new IOException($e->getMessage(), $e->getCode(), $e);
99 }
100 }
101
102 /**
103 * @return void
104 * @throws IOException
105 */
106 public function rewind():void { $this->_loadChunk(true); }
107
108 /**
109 * @return bool
110 * @throws IOException
111 */
112 public function hasNext():bool {
113 if(!($this->_files && count($this->_files))) $this->_loadChunk();
114 return ($this->_files && count($this->_files));
115 }
116
117 /**
118 * @return ?iDestinationFile
119 * @throws IOException
120 */
121 public function getNext():?iDestinationFile {
122 if(!$this->hasNext()) return null;
123
124 $nextfile = array_shift($this->_files);
125
126 $path = $this->_directory . '/' . $nextfile->getName();
127 $basename = basename($path);
128
129 $file = new DestinationFile();
130 $file->setType($nextfile->getMimeType() == Client::MIMITYPE_DIR ? iDestinationFile::TYPE_DIRECTORY : iDestinationFile::TYPE_FILE);
131 $file->setName($basename);
132 $file->setPath($basename == $path ? '' : dirname($path));
133 $file->setSize($nextfile->getMimeType() == Client::MIMITYPE_DIR ? 4096 : $nextfile->getSize());
134 $file->setModifyTime($nextfile->getModificationTime());
135 $file->setFileData(json_encode([ 'id' => $nextfile->getId() ]));
136
137 return $file;
138 }
139 }