Skip to content

Commit 99e155b

Browse files
committed
backup:external:appdata --unset
Signed-off-by: Maxence Lange <maxence@artificial-owl.com>
1 parent 65d6fc5 commit 99e155b

File tree

7 files changed

+64
-12
lines changed

7 files changed

+64
-12
lines changed

lib/Command/ExternalAppData.php

Lines changed: 49 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,15 @@
3333

3434
use ArtificialOwl\MySmallPhpTools\Traits\Nextcloud\nc23\TNC23Deserialize;
3535
use OC\Core\Command\Base;
36+
use OCA\Backup\Exceptions\ExternalAppdataException;
3637
use OCA\Backup\Exceptions\ExternalFolderNotFoundException;
3738
use OCA\Backup\Service\ConfigService;
3839
use OCA\Backup\Service\ExternalFolderService;
3940
use OCA\Backup\Service\PointService;
4041
use OCA\Files_External\Lib\InsufficientDataForMeaningfulAnswerException;
4142
use OCP\Files\StorageNotAvailableException;
4243
use Symfony\Component\Console\Input\InputInterface;
44+
use Symfony\Component\Console\Input\InputOption;
4345
use Symfony\Component\Console\Output\OutputInterface;
4446
use Symfony\Component\Console\Question\ChoiceQuestion;
4547
use Symfony\Component\Console\Question\ConfirmationQuestion;
@@ -89,7 +91,8 @@ public function __construct(
8991
*/
9092
protected function configure() {
9193
$this->setName('backup:external:appdata')
92-
->setDescription('Add external filesystem to store the app\'s data');
94+
->setDescription('Add external filesystem to store the app\'s data')
95+
->addOption('unset', '', InputOption::VALUE_NONE, 'Unset the current external appdata');
9396
}
9497

9598

@@ -103,15 +106,56 @@ protected function configure() {
103106
* @throws ExternalFolderNotFoundException
104107
*/
105108
protected function execute(InputInterface $input, OutputInterface $output): int {
106-
$output->writeln(
107-
'This configuration tool will help you set the <info>Appdata</info> folder of the Backup App on an <info>external storage</info>'
108-
);
109-
$output->writeln('...');
109+
$unset = $input->getOption('unset');
110+
111+
try {
112+
$external = $this->pointService->getExternalAppData();
113+
$output->writeln('Your <info>appdata</info> is currently on an external storage:');
114+
$output->writeln('Storage Id: <info>' . $external->getStorageId() . '</info>');
115+
$output->writeln('Storage: <info>' . $external->getStorage() . '</info>');
116+
$output->writeln('Root: <info>' . $external->getRoot() . '</info>');
117+
$output->writeln('');
118+
119+
if (!$unset) {
120+
return 0;
121+
}
122+
} catch (ExternalAppdataException $e) {
123+
$unset = false;
124+
}
125+
126+
if (!$unset) {
127+
$output->writeln(
128+
'This configuration tool will help you set the <info>Appdata</info> folder '
129+
. ' of the Backup App on an <info>external storage</info>'
130+
);
131+
}
132+
133+
$output->writeln('');
110134
$output->writeln('');
111135
$output->writeln('<error>All previous Restoring Point will be lost during this process</error>');
112136
$output->writeln('');
113137
$output->writeln('');
138+
139+
if ($unset) {
140+
$question = new ConfirmationQuestion(
141+
'<comment>Do you really want to not use this External Folder appdata anymore ?</comment> (y/N) ',
142+
false,
143+
'/^(y|Y)/i'
144+
);
145+
146+
$helper = $this->getHelper('question');
147+
if (!$helper->ask($input, $output, $question)) {
148+
$output->writeln('Operation cancelled');
149+
}
150+
151+
$this->pointService->setExternalAppData(0);
152+
153+
return 0;
154+
}
155+
156+
114157
$storageId = $this->selectStorage($input, $output);
158+
115159
$output->writeln('');
116160
if ($storageId === 0) {
117161
$output->writeln('Operation cancelled');

lib/Controller/LocalController.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
use OCA\Backup\Exceptions\RestoringPointException;
4545
use OCA\Backup\Exceptions\RestoringPointNotFoundException;
4646
use OCA\Backup\Model\BackupEvent;
47+
use OCA\Backup\Model\ExternalFolder;
4748
use OCA\Backup\Model\RestoringPoint;
4849
use OCA\Backup\Service\ConfigService;
4950
use OCA\Backup\Service\CronService;
@@ -247,7 +248,7 @@ public function getAppData(): DataResponse {
247248
try {
248249
return new DataResponse($this->pointService->getExternalAppData());
249250
} catch (ExternalAppdataException $e) {
250-
return new DataResponse([]);
251+
return new DataResponse(new ExternalFolder());
251252
} catch (Exception $e) {
252253
throw new OcsException($e->getMessage(), Http::STATUS_BAD_REQUEST);
253254
}

lib/Db/PointRequest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,5 +216,4 @@ public function getByInstance(string $instance): array {
216216

217217
return $this->getItemsFromRequest($qb);
218218
}
219-
220219
}

lib/Model/RestoringChunk.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -433,5 +433,4 @@ public function jsonSerialize(): array {
433433

434434
return $arr;
435435
}
436-
437436
}

lib/Service/ConfigService.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,13 @@ public function getAppValueArray(string $key): array {
188188
}
189189

190190

191+
/**
192+
* @param string $key
193+
*/
194+
public function unsetAppValue(string $key): void {
195+
$this->config->deleteAppValue(Application::APP_ID, $key);
196+
}
197+
191198
/**
192199
* Set a value by key
193200
*

lib/Service/PointService.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -705,19 +705,23 @@ public function getExternalAppData(): ExternalFolder {
705705
* @throws InsufficientDataForMeaningfulAnswerException
706706
* @throws StorageNotAvailableException
707707
*/
708-
public function setExternalAppData(int $storageId, string $root): void {
708+
public function setExternalAppData(int $storageId, string $root = ''): void {
709709
if ($storageId === 0) {
710710
try {
711711
$this->destroyBackupFS();
712712
} catch (ExternalFolderNotFoundException | NotFoundException | NotPermittedException $e) {
713713
$this->deleteAllPoints();
714714
}
715715

716-
$this->configService->unsetAppConfig(ConfigService::EXTERNAL_APPDATA);
716+
$this->configService->unsetAppValue(ConfigService::EXTERNAL_APPDATA);
717717

718718
return;
719719
}
720720

721+
if ($root === '') {
722+
throw new ExternalFolderNotFoundException('empty root');
723+
}
724+
721725
$external = $this->externalFolderService->getStorageById($storageId);
722726
$external->setRoot($root);
723727

lib/Service/RestoreService.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,6 @@ private function generateChunkPartsFromFolder(
316316

317317
// TODO: generate Checksum, files count ? check if zip is valid ?
318318
} catch (NotFoundException $e) {
319-
320319
try {
321320
/** @var File $chunkFile */
322321
$chunkFile = $node->get($chunkName . '.zip.gz');
@@ -327,7 +326,6 @@ private function generateChunkPartsFromFolder(
327326
$chunk->setChecksum($this->getChecksumFromStream($read));
328327
fclose($read);
329328
} catch (NotFoundException $e) {
330-
331329
$isPacked = true;
332330
foreach ($node->getDirectoryListing() as $item) {
333331
/** @var File $item */

0 commit comments

Comments
 (0)