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 / pCloud / Client / DirIterator.php
backup / src / JetBackup / Destination / Vendors / pCloud / Client Last commit date
.htaccess 1 year ago Client.php 1 year ago ClientException.php 1 year ago DirIterator.php 1 year ago File.php 1 year ago index.html 1 year ago web.config 1 year ago
DirIterator.php
124 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\pCloud\Client;
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\Exception\IOException;
18 use JetBackup\Exception\JBException;
19
20 defined( '__JETBACKUP__' ) or die( 'Restricted access' );
21
22 class DirIterator implements DestinationDirIterator {
23
24 const CHUNK_LIMIT = 1000;
25
26 private Box $_destination;
27 private string $_directory;
28 private ?ListFiles $_list=null;
29 private ?string $_parent_id=null;
30 /** @var File[] */
31 private array $_files=[];
32
33 /**
34 * @param Box $destination
35 * @param string $directory
36 * @param string|null $parent_id
37 *
38 * @throws IOException
39 * @throws JBException
40 */
41 public function __construct(Box $destination, string $directory, ?string $parent_id=null) {
42 $this->_destination = $destination;
43 $this->_directory = $directory;
44
45 if(!$this->_destination->getClient()) throw new IOException("Unable to retrieve google drive service");
46 $path = $this->_destination->getRealPath($this->_directory);
47
48 if(!$parent_id) $parent_id = $this->_destination->getFileId($path == '/' ? '' : $path);
49 $this->_parent_id = $parent_id;
50
51 if(!$this->_destination->dirExists($this->_directory, $parent_id)) return;
52
53 $this->rewind();
54 }
55
56 /**
57 * @param bool $rewind
58 *
59 * @return void
60 * @throws IOException
61 */
62 private function _loadChunk(bool $rewind=false):void {
63 try {
64
65 $marker = '';
66
67 if(!$rewind && $this->_list) {
68 if(!$this->_list->getNextPageToken()) {
69 $this->_list = null;
70 return;
71 }
72 $marker = $this->_list->getNextPageToken();
73 }
74
75 $this->_list = $this->_destination->_retries(function() use ($rewind, $marker) {
76 return $this->_destination->getClient()->listFolder($this->_parent_id, self::CHUNK_LIMIT, $marker);
77 }, "Failed fetching list of files");
78
79 $this->_files = $this->_list ? $this->_list->getFiles() : [];
80
81 } catch(Exception $e) {
82 if($e->getCode() == 404) $this->_files = [];
83 else throw new IOException($e->getMessage(), $e->getCode(), $e);
84 }
85 }
86
87 /**
88 * @return void
89 * @throws IOException
90 */
91 public function rewind():void { $this->_loadChunk(true); }
92
93 /**
94 * @return bool
95 * @throws IOException
96 */
97 public function hasNext():bool {
98 if(!($this->_files && count($this->_files))) $this->_loadChunk();
99 return ($this->_files && count($this->_files));
100 }
101
102 /**
103 * @return ?iDestinationFile
104 * @throws IOException
105 */
106 public function getNext():?iDestinationFile {
107 if(!$this->hasNext()) return null;
108
109 $nextfile = array_shift($this->_files);
110
111 $path = $this->_directory . '/' . $nextfile->getName();
112 $basename = basename($path);
113
114 $file = new DestinationFile();
115 $file->setType($nextfile->getMimeType() == Client::MIMITYPE_DIR ? iDestinationFile::TYPE_DIRECTORY : iDestinationFile::TYPE_FILE);
116 $file->setName($basename);
117 $file->setPath($basename == $path ? '' : dirname($path));
118 $file->setSize($nextfile->getMimeType() == Client::MIMITYPE_DIR ? 4096 : $nextfile->getSize());
119 $file->setModifyTime($nextfile->getModificationTime());
120 $file->setFileData(json_encode([ 'id' => $nextfile->getId() ]));
121
122 return $file;
123 }
124 }