36
36
use ArtificialOwl \MySmallPhpTools \Exceptions \SignatureException ;
37
37
use OC \Core \Command \Base ;
38
38
use OCA \Backup \Db \PointRequest ;
39
+ use OCA \Backup \Exceptions \ExternalFolderNotFoundException ;
39
40
use OCA \Backup \Exceptions \RemoteInstanceException ;
40
41
use OCA \Backup \Exceptions \RemoteInstanceNotFoundException ;
41
42
use OCA \Backup \Exceptions \RemoteResourceNotFoundException ;
42
43
use OCA \Backup \Exceptions \RestoringChunkNotFoundException ;
44
+ use OCA \Backup \Exceptions \RestoringChunkPartNotFoundException ;
45
+ use OCA \Backup \Exceptions \RestoringPointException ;
43
46
use OCA \Backup \Exceptions \RestoringPointNotFoundException ;
47
+ use OCA \Backup \Exceptions \RestoringPointNotInitiatedException ;
48
+ use OCA \Backup \Exceptions \RestoringPointPackException ;
44
49
use OCA \Backup \Model \ChunkPartHealth ;
45
- use OCA \Backup \Model \RestoringHealth ;
46
50
use OCA \Backup \Model \RestoringPoint ;
47
51
use OCA \Backup \Service \ChunkService ;
52
+ use OCA \Backup \Service \ExternalFolderService ;
48
53
use OCA \Backup \Service \OutputService ;
54
+ use OCA \Backup \Service \PackService ;
49
55
use OCA \Backup \Service \PointService ;
50
56
use OCA \Backup \Service \RemoteService ;
51
57
use OCA \Backup \Service \RemoteStreamService ;
58
+ use OCP \Files \GenericFileException ;
52
59
use OCP \Files \NotFoundException ;
53
60
use OCP \Files \NotPermittedException ;
61
+ use OCP \Lock \LockedException ;
62
+ use Symfony \Component \Console \Exception \InvalidOptionException ;
54
63
use Symfony \Component \Console \Input \InputArgument ;
55
64
use Symfony \Component \Console \Input \InputInterface ;
56
65
use Symfony \Component \Console \Input \InputOption ;
@@ -74,12 +83,18 @@ class PointDownload extends Base {
74
83
/** @var ChunkService */
75
84
private $ chunkService ;
76
85
86
+ /** @var PackService */
87
+ private $ packService ;
88
+
77
89
/** @var RemoteStreamService */
78
90
private $ remoteStreamService ;
79
91
80
92
/** @var RemoteService */
81
93
private $ remoteService ;
82
94
95
+ /** @var ExternalFolderService */
96
+ private $ externalFolderService ;
97
+
83
98
/** @var OutputService */
84
99
private $ outputService ;
85
100
@@ -90,25 +105,31 @@ class PointDownload extends Base {
90
105
* @param PointRequest $pointRequest
91
106
* @param PointService $pointService
92
107
* @param ChunkService $chunkService
108
+ * @param PackService $packService
93
109
* @param RemoteStreamService $remoteStreamService
94
110
* @param RemoteService $remoteService
111
+ * @param ExternalFolderService $externalFolderService
95
112
* @param OutputService $outputService
96
113
*/
97
114
public function __construct (
98
115
PointRequest $ pointRequest ,
99
116
PointService $ pointService ,
100
117
ChunkService $ chunkService ,
118
+ PackService $ packService ,
101
119
RemoteStreamService $ remoteStreamService ,
102
120
RemoteService $ remoteService ,
121
+ ExternalFolderService $ externalFolderService ,
103
122
OutputService $ outputService
104
123
) {
105
124
parent ::__construct ();
106
125
107
126
$ this ->pointRequest = $ pointRequest ;
108
127
$ this ->pointService = $ pointService ;
109
128
$ this ->chunkService = $ chunkService ;
129
+ $ this ->packService = $ packService ;
110
130
$ this ->remoteStreamService = $ remoteStreamService ;
111
131
$ this ->remoteService = $ remoteService ;
132
+ $ this ->externalFolderService = $ externalFolderService ;
112
133
$ this ->outputService = $ outputService ;
113
134
}
114
135
@@ -119,37 +140,48 @@ public function __construct(
119
140
protected function configure () {
120
141
$ this ->setName ('backup:point:download ' )
121
142
->setDescription ('Download restoring point from remote instance ' )
122
- ->addArgument ('instance ' , InputArgument::REQUIRED , 'address of the remote instance ' )
123
143
->addArgument ('pointId ' , InputArgument::REQUIRED , 'Id of the restoring point ' )
124
- ->addOption ('no-check ' , '' , InputOption::VALUE_NONE , 'do not check integrity of restoring point ' );
144
+ ->addOption ('remote ' , '' , InputOption::VALUE_REQUIRED , 'address of the remote instance ' )
145
+ ->addOption ('external ' , '' , InputOption::VALUE_REQUIRED , 'storageId of the external storage ' )
146
+ ->addOption (
147
+ 'no-check ' , '' , InputOption::VALUE_NONE , 'do not check integrity of restoring point '
148
+ );
125
149
}
126
150
127
151
128
152
/**
129
153
* @param InputInterface $input
130
154
* @param OutputInterface $output
131
155
*
156
+ * @return int
157
+ * @throws ExternalFolderNotFoundException
158
+ * @throws GenericFileException
132
159
* @throws NotFoundException
133
160
* @throws NotPermittedException
134
161
* @throws RemoteInstanceException
135
162
* @throws RemoteInstanceNotFoundException
136
163
* @throws RemoteResourceNotFoundException
137
164
* @throws RestoringChunkNotFoundException
165
+ * @throws RestoringChunkPartNotFoundException
166
+ * @throws RestoringPointException
138
167
* @throws RestoringPointNotFoundException
139
- * @throws SignatureException
168
+ * @throws RestoringPointPackException
140
169
* @throws SignatoryException
170
+ * @throws SignatureException
141
171
*/
142
- protected function execute (InputInterface $ input , OutputInterface $ output ) {
143
- $ instance = $ input ->getArgument ('instance ' );
172
+ protected function execute (InputInterface $ input , OutputInterface $ output ): int {
144
173
$ pointId = $ input ->getArgument ('pointId ' );
174
+ $ remote = $ input ->getOption ('remote ' );
175
+ $ external = (int )$ input ->getOption ('external ' );
145
176
146
177
try {
147
178
$ point = $ this ->pointService ->getRestoringPoint ($ pointId );
148
179
$ output ->writeln ('> found a local restoring point ' );
149
180
} catch (RestoringPointNotFoundException $ e ) {
150
181
$ output ->writeln ('> downloading metadata ' );
151
182
152
- $ point = $ this ->remoteService ->getRestoringPoint ($ instance , $ pointId );
183
+ $ point = $ this ->getRestoringPoint ($ remote , $ external , $ pointId );
184
+ // $point = $this->remoteService->getRestoringPoint($instance, $pointId);
153
185
if (!$ input ->getOption ('no-check ' )) {
154
186
try {
155
187
$ this ->remoteStreamService ->verifyPoint ($ point );
@@ -161,8 +193,8 @@ protected function execute(InputInterface $input, OutputInterface $output) {
161
193
}
162
194
}
163
195
164
- $ point ->unsetHealth ()
165
- ->setInstance ('' );
196
+ $ point ->unsetHealth ();
197
+ // ->setInstance('');
166
198
167
199
$ this ->pointRequest ->save ($ point );
168
200
$ this ->pointService ->saveMetadata ($ point );
@@ -172,7 +204,12 @@ protected function execute(InputInterface $input, OutputInterface $output) {
172
204
$ output ->write ('check health status: ' );
173
205
$ this ->pointService ->generateHealth ($ point );
174
206
$ output ->writeln ($ this ->outputService ->displayHealth ($ point ));
175
- $ this ->downloadMissingFiles ($ instance , $ point , $ point ->getHealth (), $ output );
207
+ $ this ->downloadMissingFiles ($ output , $ remote , $ external , $ point );
208
+
209
+ return 0 ;
210
+ // $this->downloadMissingFiles($instance, $point, $point->getHealth(), $output);
211
+
212
+ // $point = $this->getRestoringPoint($remote, $external, $pointId);
176
213
177
214
178
215
// echo json_encode($point->getHealth());
@@ -211,40 +248,106 @@ protected function execute(InputInterface $input, OutputInterface $output) {
211
248
212
249
213
250
/**
214
- * @param string $instance
215
- * @param RestoringPoint $point
216
- * @param RestoringHealth $health
217
251
* @param OutputInterface $output
252
+ * @param string|null $remote
253
+ * @param int|null $external
254
+ * @param RestoringPoint $point
218
255
*
219
- * @throws NotFoundException
256
+ * @throws ExternalFolderNotFoundException
257
+ * @throws GenericFileException
220
258
* @throws NotPermittedException
221
259
* @throws RemoteInstanceException
222
260
* @throws RemoteInstanceNotFoundException
223
261
* @throws RemoteResourceNotFoundException
224
262
* @throws RestoringChunkNotFoundException
263
+ * @throws RestoringChunkPartNotFoundException
264
+ * @throws RestoringPointException
265
+ * @throws RestoringPointNotFoundException
266
+ * @throws RestoringPointNotInitiatedException
267
+ * @throws LockedException
225
268
*/
226
269
private function downloadMissingFiles (
227
- string $ instance ,
228
- RestoringPoint $ point ,
229
- RestoringHealth $ health ,
230
- OutputInterface $ output
270
+ OutputInterface $ output ,
271
+ ? string $ remote ,
272
+ ? int $ external ,
273
+ RestoringPoint $ point
231
274
): void {
232
- foreach ($ health ->getParts () as $ chunk ) {
233
- if ($ chunk ->getStatus () === ChunkPartHealth::STATUS_OK ) {
275
+ $ health = $ point ->getHealth ();
276
+ foreach ($ health ->getParts () as $ partHealth ) {
277
+ if ($ partHealth ->getStatus () === ChunkPartHealth::STATUS_OK ) {
234
278
continue ;
235
279
}
236
280
237
- $ output ->write (' * Downloading ' . $ chunk ->getDataName () . '/ ' . $ chunk ->getPartName () . ': ' );
238
- $ restoringChunk = $ this ->pointService ->getChunkContent (
281
+ $ output ->write (
282
+ ' * Downloading ' . $ partHealth ->getDataName () .
283
+ '/ ' . $ partHealth ->getChunkName () . '/ ' . $ partHealth ->getPartName () . ': '
284
+ );
285
+
286
+ $ chunk = $ this ->chunkService ->getChunkFromRP (
239
287
$ point ,
240
- $ chunk -> getDataName (),
241
- $ chunk -> getPartName ()
288
+ $ partHealth -> getChunkName (),
289
+ $ partHealth -> getDataName ()
242
290
);
243
291
244
- $ chunk = $ this ->remoteService ->downloadChunk ($ instance , $ point , $ restoringChunk );
245
- $ this ->chunkService ->saveChunkContent ($ point , $ chunk );
292
+ $ part = clone $ this ->packService ->getPartFromChunk ($ chunk , $ partHealth ->getPartName ());
293
+ // $this->packService->getChunkPartContent($point, $chunk, $part);
294
+ //
295
+ // $this->remoteService->uploadPart($instance, $point, $chunk, $part);
296
+
297
+ if (!is_null ($ remote )) {
298
+ $ this ->remoteService ->downloadPart ($ remote , $ point , $ chunk , $ part );
299
+ } else if ($ external > 0 ) {
300
+ $ externalFolder = $ this ->externalFolderService ->getByStorageId ($ external );
301
+ $ this ->externalFolderService ->downloadPart (
302
+ $ externalFolder ,
303
+ $ point ,
304
+ $ chunk ,
305
+ $ part
306
+ );
307
+ } else {
308
+ throw new InvalidOptionException ('use --remote or --external ' );
309
+ }
310
+
311
+ // $chunk = $this->remoteService->downloadChunk($instance, $point, $restoringChunk);
312
+ $ this ->packService ->saveChunkPartContent ($ point , $ chunk , $ part );
246
313
$ output ->writeln ('<info>ok</info> ' );
247
314
}
248
315
}
249
- }
250
316
317
+
318
+ /**
319
+ * @param string|null $remote
320
+ * @param int|null $external
321
+ * @param string $pointId
322
+ *
323
+ * @return RestoringPoint
324
+ * @throws NotPermittedException
325
+ * @throws RemoteInstanceException
326
+ * @throws RemoteInstanceNotFoundException
327
+ * @throws RemoteResourceNotFoundException
328
+ * @throws RestoringPointNotFoundException
329
+ * @throws ExternalFolderNotFoundException
330
+ * @throws RestoringChunkPartNotFoundException
331
+ * @throws RestoringPointException
332
+ * @throws RestoringPointPackException
333
+ * @throws GenericFileException
334
+ */
335
+ private function getRestoringPoint (
336
+ ?string $ remote ,
337
+ ?int $ external ,
338
+ string $ pointId
339
+ ): RestoringPoint {
340
+ if (!is_null ($ remote )) {
341
+ return $ this ->remoteService ->getRestoringPoint ($ remote , $ pointId );
342
+ }
343
+
344
+ if ($ external > 0 ) {
345
+ $ externalFolder = $ this ->externalFolderService ->getByStorageId ($ external );
346
+
347
+ return $ this ->externalFolderService ->getRestoringPoint ($ externalFolder , $ pointId );
348
+ }
349
+
350
+ throw new InvalidOptionException ('use --remote or --external ' );
351
+ }
352
+
353
+ }
0 commit comments