33
33
34
34
35
35
use OC \Core \Command \Base ;
36
- use OCA \Backup \Db \ExternalFolderRequest ;
37
- use OCA \Backup \Model \ExternalFolder ;
38
- use Symfony \Component \Console \Input \InputArgument ;
36
+ use OCA \Backup \Exceptions \ExternalFolderNotFoundException ;
37
+ use OCA \Backup \Service \ExternalFolderService ;
38
+ use OCA \Files_External \Lib \InsufficientDataForMeaningfulAnswerException ;
39
+ use OCP \Files \StorageNotAvailableException ;
39
40
use Symfony \Component \Console \Input \InputInterface ;
40
41
use Symfony \Component \Console \Output \OutputInterface ;
42
+ use Symfony \Component \Console \Question \ChoiceQuestion ;
43
+ use Symfony \Component \Console \Question \ConfirmationQuestion ;
44
+ use Symfony \Component \Console \Question \Question ;
41
45
42
46
43
47
/**
48
52
class ExternalAdd extends Base {
49
53
50
54
51
- /** @var ExternalFolderRequest */
52
- private $ externalFolderRequest ;
55
+ /** @var ExternalFolderService */
56
+ private $ externalFolderService ;
53
57
54
58
55
59
/**
56
60
* ExternalAdd constructor.
57
61
*
58
- * @param ExternalFolderRequest $externalFolderRequest
62
+ * @param ExternalFolderService $externalFolderService
59
63
*/
60
- public function __construct (ExternalFolderRequest $ externalFolderRequest ) {
64
+ public function __construct (ExternalFolderService $ externalFolderService ) {
61
65
parent ::__construct ();
62
66
63
- $ this ->externalFolderRequest = $ externalFolderRequest ;
67
+ $ this ->externalFolderService = $ externalFolderService ;
64
68
}
65
69
66
70
@@ -69,9 +73,7 @@ public function __construct(ExternalFolderRequest $externalFolderRequest) {
69
73
*/
70
74
protected function configure () {
71
75
$ this ->setName ('backup:external:add ' )
72
- ->setDescription ('Add external filesystem to store your backups ' )
73
- ->addArgument ('storage_id ' , InputArgument::REQUIRED , 'storage_id from oc_storage ' )
74
- ->addArgument ('root ' , InputArgument::REQUIRED , 'folder ' );
76
+ ->setDescription ('Add external filesystem to store your backups ' );
75
77
}
76
78
77
79
@@ -80,19 +82,137 @@ protected function configure() {
80
82
* @param OutputInterface $output
81
83
*
82
84
* @return int
85
+ * @throws InsufficientDataForMeaningfulAnswerException
86
+ * @throws StorageNotAvailableException
87
+ * @throws ExternalFolderNotFoundException
83
88
*/
84
89
protected function execute (InputInterface $ input , OutputInterface $ output ): int {
85
- $ storageId = (int )$ input ->getArgument ('storage_id ' );
86
- $ root = $ input ->getArgument ('root ' );
87
90
88
- $ folder = new ExternalFolder ();
89
- $ folder ->setStorageId ($ storageId );
90
- $ folder ->setRoot ($ root );
91
+ $ storageId = $ this ->selectStorage ($ input , $ output );
92
+ $ output ->writeln ('' );
93
+ if ($ storageId === 0 ) {
94
+ $ output ->writeln ('Operation cancelled ' );
91
95
92
- $ this ->externalFolderRequest ->save ($ folder );
96
+ return 0 ;
97
+ }
98
+
99
+ $ root = $ this ->requestingRoot ($ input , $ output );
100
+ echo '>> ' . json_encode ($ root );
101
+ $ output ->writeln ('' );
102
+ if ($ root === '' ) {
103
+ $ output ->writeln ('Operation cancelled ' );
104
+
105
+ return 0 ;
106
+ }
107
+
108
+ $ external = $ this ->externalFolderService ->getStorageById ($ storageId );
109
+ $ output ->writeln ('' );
110
+ if ($ external ->getRoot () !== '' ) {
111
+ $ output ->writeln ('This external filesystem is already used by the Backup App ' );
112
+
113
+ return 0 ;
114
+ }
115
+
116
+ $ external ->setRoot ($ root );
117
+
118
+ $ output ->writeln ('Please confirm the creation of a new External Folder, based on this setup: ' );
119
+ $ output ->writeln ('' );
120
+ $ output ->writeln ('Storage Id: <info> ' . $ external ->getStorageId () . '</info> ' );
121
+ $ output ->writeln ('Storage Path: <info> ' . $ external ->getStorage () . '</info> ' );
122
+ $ output ->writeln ('Localisation of backup files: <info> ' . $ external ->getRoot () . '</info> ' );
123
+ $ output ->writeln ('' );
124
+
125
+ $ question = new ConfirmationQuestion (
126
+ '<comment>Do you really want to create and use this External Folder to store your backup ?</comment> (y/N) ' ,
127
+ false ,
128
+ '/^(y|Y)/i '
129
+ );
130
+
131
+ $ helper = $ this ->getHelper ('question ' );
132
+ if (!$ helper ->ask ($ input , $ output , $ question )) {
133
+ $ output ->writeln ('Operation cancelled ' );
134
+
135
+ return 0 ;
136
+ }
137
+
138
+ $ this ->externalFolderService ->save ($ external );
139
+
140
+ $ output ->writeln (
141
+ '<info>The generated External Folder will now be used to store your restoring points</info> '
142
+ );
143
+
144
+ return 0 ;
145
+ }
146
+
147
+
148
+ /**
149
+ * @param InputInterface $input
150
+ * @param OutputInterface $output
151
+ *
152
+ * @return int|mixed|string|null
153
+ * @throws InsufficientDataForMeaningfulAnswerException
154
+ * @throws StorageNotAvailableException
155
+ */
156
+ private function selectStorage (InputInterface $ input , OutputInterface $ output ): int {
157
+ $ availableStorage = [];
158
+ foreach ($ this ->externalFolderService ->getStorages () as $ storage ) {
159
+ if ($ storage ->getRoot () !== '' ) {
160
+ continue ;
161
+ }
162
+ $ availableStorage [$ storage ->getStorageId ()] =
163
+ $ storage ->getStorage () . ' (id: ' . $ storage ->getStorageId () . ') ' ;
164
+ }
165
+
166
+ if (empty ($ availableStorage )) {
167
+ $ output ->writeln ('There is no available external filesystem. ' );
168
+ $ output ->writeln (
169
+ 'You can use <info>./occ backup:external:list</info> to see already configured external folders '
170
+ );
171
+ $ output ->writeln ('You can use the <info>Files External</info> to add a new external filesystem ' );
172
+ $ output ->writeln ('' );
173
+
174
+ return 0 ;
175
+ }
176
+
177
+ $ availableStorage [0 ] = 'exit ' ;
178
+
179
+ $ output ->writeln ('' );
180
+ $ helper = $ this ->getHelper ('question ' );
181
+ $ question = new ChoiceQuestion (
182
+ 'Which external storage you want to use to store your backups ? ' ,
183
+ $ availableStorage ,
184
+ 0
185
+ );
186
+ $ question ->setErrorMessage ('Select a valid filesystem ' );
187
+
188
+ $ result = $ helper ->ask ($ input , $ output , $ question );
189
+ foreach ($ availableStorage as $ k => $ v ) {
190
+ if ($ v === $ result ) {
191
+ return $ k ;
192
+ }
193
+ }
93
194
94
195
return 0 ;
95
196
}
96
197
198
+
199
+ /**
200
+ * @param InputInterface $input
201
+ * @param OutputInterface $output
202
+ *
203
+ * @return string
204
+ */
205
+ private function requestingRoot (InputInterface $ input , OutputInterface $ output ): string {
206
+ $ helper = $ this ->getHelper ('question ' );
207
+ $ default = 'backup/points/ ' ;
208
+
209
+ $ question = new Question (
210
+ 'Path to the right folder to store your backups (default="<info> ' . $ default . '</info>"): ' ,
211
+ $ default
212
+ );
213
+
214
+ return trim ($ helper ->ask ($ input , $ output , $ question ));
215
+ }
216
+
97
217
}
98
218
0 commit comments