Skip to content

Commit 015ce68

Browse files
committed
use external index when searching if available
1 parent 20467dd commit 015ce68

File tree

3 files changed

+75
-12
lines changed

3 files changed

+75
-12
lines changed

lib/Command/FileSearch.php

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,18 @@
3131

3232
namespace OCA\Backup\Command;
3333

34+
use ArtificialOwl\MySmallPhpTools\Traits\Nextcloud\nc23\TNC23Deserialize;
3435
use ArtificialOwl\MySmallPhpTools\Traits\TArrayTools;
3536
use ArtificialOwl\MySmallPhpTools\Traits\TStringTools;
3637
use OC\Core\Command\Base;
3738
use OCA\Backup\Exceptions\ArchiveCreateException;
3839
use OCA\Backup\Exceptions\ArchiveNotFoundException;
3940
use OCA\Backup\Exceptions\RestoringPointNotFoundException;
41+
use OCA\Backup\Exceptions\RestoringPointNotInitiatedException;
42+
use OCA\Backup\Model\ArchiveFile;
43+
use OCA\Backup\Model\RestoringChunk;
44+
use OCA\Backup\Model\RestoringData;
45+
use OCA\Backup\Model\RestoringPoint;
4046
use OCA\Backup\Service\ChunkService;
4147
use OCA\Backup\Service\PointService;
4248
use OCP\Files\NotFoundException;
@@ -55,6 +61,7 @@
5561
class FileSearch extends Base {
5662
use TArrayTools;
5763
use TStringTools;
64+
use TNC23Deserialize;
5865

5966

6067
/** @var PointService */
@@ -103,6 +110,7 @@ protected function configure() {
103110
* @throws NotFoundException
104111
* @throws NotPermittedException
105112
* @throws RestoringPointNotFoundException
113+
* @throws RestoringPointNotInitiatedException
106114
*/
107115
protected function execute(InputInterface $input, OutputInterface $output): int {
108116
$search = $input->getArgument('search');
@@ -126,14 +134,24 @@ protected function execute(InputInterface $input, OutputInterface $output): int
126134
$empty = true;
127135

128136
foreach ($point->getRestoringData() as $data) {
137+
if ($data->getType() === RestoringData::INTERNAL_DATA
138+
|| $data->getType() === RestoringData::SQL_DUMP) {
139+
continue;
140+
}
141+
129142
$chunks = $data->getChunks();
130143
$progressBar = new ProgressBar($output, sizeof($chunks));
131144
$progressBar->start();
132145

133146
foreach ($chunks as $chunk) {
134147
$progressBar->advance();
135148
try {
136-
$files = $this->chunkService->searchFileInChunk($point, $chunk, $search);
149+
try {
150+
$files = $this->searchFilesInChunkFolder($point, $chunk, $search);
151+
} catch (NotFoundException | NotPermittedException $e) {
152+
$files = $this->chunkService->searchFilesInChunk($point, $chunk, $search);
153+
}
154+
137155
if (empty($files)) {
138156
continue;
139157
}
@@ -171,4 +189,45 @@ protected function execute(InputInterface $input, OutputInterface $output): int
171189

172190
return 0;
173191
}
192+
193+
194+
/**
195+
* @param RestoringPoint $point
196+
* @param RestoringChunk $chunk
197+
* @param string $search
198+
*
199+
* @return array
200+
* @throws NotFoundException
201+
* @throws NotPermittedException
202+
* @throws RestoringPointNotInitiatedException
203+
*/
204+
private function searchFilesInChunkFolder(
205+
RestoringPoint $point,
206+
RestoringChunk $chunk,
207+
string $search
208+
): array {
209+
$folder = $this->chunkService->getChunkFolder($point, $chunk);
210+
211+
$file = $folder->getFile(ChunkService::PREFIX . $chunk->getName());
212+
$data = json_decode($file->getContent(), true);
213+
if (!is_array($data)) {
214+
$data = [];
215+
}
216+
217+
/** @var ArchiveFile[] $files */
218+
$files = $this->deserializeArray($this->getArray('files', $data), ArchiveFile::class);
219+
$chunk->setFiles($files);
220+
221+
return array_filter(
222+
array_map(
223+
function (ArchiveFile $file) use ($search): ?ArchiveFile {
224+
if (strpos(strtolower($file->getName()), $search) !== false) {
225+
return $file;
226+
}
227+
228+
return null;
229+
}, $chunk->getFiles()
230+
)
231+
);
232+
}
174233
}

lib/Model/ArchiveFile.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131

3232
namespace OCA\Backup\Model;
3333

34+
use ArtificialOwl\MySmallPhpTools\IDeserializable;
3435
use ArtificialOwl\MySmallPhpTools\Traits\TArrayTools;
3536
use JsonSerializable;
3637

@@ -39,7 +40,7 @@
3940
*
4041
* @package OCA\Backup\Model
4142
*/
42-
class ArchiveFile implements JsonSerializable {
43+
class ArchiveFile implements JsonSerializable, IDeserializable {
4344
use TArrayTools;
4445

4546

@@ -126,7 +127,7 @@ public function getRestoringChunk(): RestoringChunk {
126127
*
127128
* @return ArchiveFile
128129
*/
129-
public function import(array $data): ArchiveFile {
130+
public function import(array $data): IDeserializable {
130131
$this->setName($this->get('name', $data));
131132
$this->setFilesize($this->getInt('filesize', $data));
132133

lib/Service/ChunkService.php

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131

3232
namespace OCA\Backup\Service;
3333

34+
use ArtificialOwl\MySmallPhpTools\Traits\Nextcloud\nc23\TNC23Deserialize;
3435
use ArtificialOwl\MySmallPhpTools\Traits\TArrayTools;
3536
use ArtificialOwl\MySmallPhpTools\Traits\TFileTools;
3637
use ArtificialOwl\MySmallPhpTools\Traits\TStringTools;
@@ -65,6 +66,7 @@ class ChunkService {
6566
use TArrayTools;
6667
use TStringTools;
6768
use TFileTools;
69+
use TNC23Deserialize;
6870

6971

7072
public const BACKUP_SCRIPT = 'restore.php';
@@ -181,7 +183,7 @@ public function restoreChunk(
181183
$zip->extractTo($root, ($filename === '') ? null : $filename);
182184
$this->closeZipArchive($zip);
183185

184-
unlink($root . self::PREFIX . $chunk->getName() . '.json');
186+
unlink($root . self::PREFIX . $chunk->getName());
185187
// $this->restoreFromArchive($archive, $root);
186188
// $this->deleteArchive($backup, $archive, 'zip');
187189
}
@@ -259,19 +261,20 @@ public function listFilesFromChunk(RestoringPoint $point, RestoringChunk $chunk)
259261

260262

261263
/**
262-
* @param RestoringChunk $archive
264+
* @param RestoringChunk $chunk
263265
* @param ZipArchive $zip
264266
*/
265-
public function listFilesFromZip(RestoringChunk $archive, ZipArchive $zip): void {
266-
$json = $zip->getFromName(self::PREFIX . $archive->getName() . '.json');
267+
public function listFilesFromZip(RestoringChunk $chunk, ZipArchive $zip): void {
268+
$json = $zip->getFromName(self::PREFIX . $chunk->getName());
267269
if (!$json) {
268270
return;
269271
}
270272

271273
$data = json_decode($json, true);
272-
$files = $this->getList('files', $data, [ArchiveFile::class, 'import'], []);
274+
/** @var ArchiveFile[] $files */
275+
$files = $this->deserializeArray($this->getArray('files', $data), ArchiveFile::class);
273276

274-
$archive->setFiles($files);
277+
$chunk->setFiles($files);
275278
}
276279

277280

@@ -494,7 +497,7 @@ public function generateZip(RestoringPoint $point, RestoringChunk $chunk): ZipSt
494497
public function finalizeZip(ZipStreamer $zip, RestoringChunk $archive): void {
495498
$str = json_encode($archive->getResume(), JSON_PRETTY_PRINT);
496499
$read = fopen('data://text/plain,' . $str, 'rb');
497-
$zip->addFileFromStream($read, self::PREFIX . $archive->getName() . '.json');
500+
$zip->addFileFromStream($read, self::PREFIX . $archive->getName());
498501

499502
$zip->finalize();
500503
}
@@ -509,7 +512,7 @@ private function finalizeChunk(RestoringPoint $point, RestoringChunk $chunk) {
509512
$folder = $this->getChunkFolder($point, $chunk);
510513
if ($this->configService->getAppValueBool(ConfigService::PACK_INDEX)) {
511514
$folder->newFile(
512-
self::PREFIX . $chunk->getName() . '.json',
515+
self::PREFIX . $chunk->getName(),
513516
json_encode($chunk->getResume(), JSON_PRETTY_PRINT)
514517
);
515518
}
@@ -838,7 +841,7 @@ public function getChunkResource(RestoringPoint $point, RestoringChunk $chunk):
838841
* @throws NotFoundException
839842
* @throws NotPermittedException
840843
*/
841-
public function searchFileInChunk(RestoringPoint $point, RestoringChunk $chunk, string $search): array {
844+
public function searchFilesInChunk(RestoringPoint $point, RestoringChunk $chunk, string $search): array {
842845
if (empty($chunk->getFiles())) {
843846
$this->listFilesFromChunk($point, $chunk);
844847
}

0 commit comments

Comments
 (0)