Skip to content

Commit 6222b76

Browse files
Refactor uploads
1 parent 5287084 commit 6222b76

File tree

13 files changed

+41
-268
lines changed

13 files changed

+41
-268
lines changed

client/src/components/Actions.vue

Lines changed: 4 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<template>
22
<div class="flex flex-col items-end w-full">
33
<div class="flex justify-end">
4-
<upload :files="files" />
4+
<upload :files="files" v-on:uploaded="handleFileUploaded" />
55

66
<button
77
v-if="canCreateBackups.isPermitted"
@@ -23,7 +23,6 @@
2323
:status="file.status"
2424
:percent="file.progress * 100"
2525
:file="file"
26-
@restore="restore(file)"
2726
/>
2827
</ul>
2928
</div>
@@ -58,6 +57,9 @@ export default {
5857
},
5958
},
6059
methods: {
60+
handleFileUploaded(file) {
61+
this.files = this.files.filter((item) => item.file.uniqueIdentifier !== file.uniqueIdentifier)
62+
},
6163
backup() {
6264
this.loading = true;
6365
this.confirming = false;
@@ -80,33 +82,6 @@ export default {
8082
this.loading = false;
8183
});
8284
},
83-
restore(file) {
84-
this.loading = true;
85-
this.confirming = false;
86-
file.status = "restoring";
87-
88-
this.$store.dispatch('backup-provider/setStatus','restore_in_progress');
89-
90-
this.$axios
91-
.post(cp_url("api/backups/restore-from-path"), {
92-
path: file.path,
93-
})
94-
.then(({ data }) => {
95-
this.$toast.info(__(data.message));
96-
})
97-
.catch((error) => {
98-
let message = __("statamic-backup::backup.restore.failed");
99-
100-
if (error.response.data.message) {
101-
message = error.response.data.message;
102-
}
103-
this.$toast.error(__(message));
104-
})
105-
.finally(() => {
106-
this.loading = false;
107-
file.status = "restored";
108-
});
109-
},
11085
},
11186
};
11287
</script>

client/src/components/Upload.vue

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,9 @@ export default {
8787
});
8888
8989
this.resumable.on("fileSuccess", (file, event) => {
90-
const data = JSON.parse(event);
91-
this.findFile(file).status = "success";
92-
this.findFile(file).path = data.file;
90+
const data = JSON.parse(event);;
9391
this.$toast.success(data.message);
94-
this.$emit("uploaded", data.file);
92+
this.$emit("uploaded", file);
9593
});
9694
9795
this.resumable.on("fileError", (file, event) => {

client/src/components/UploadStatus.vue

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -52,26 +52,6 @@
5252
>
5353
<svg-icon name="micro/circle-with-cross" class="h-4 w-4" />
5454
</button>
55-
56-
<button
57-
v-if="status === 'success'"
58-
@click.prevent="restore"
59-
class="btn-primary"
60-
>
61-
<svg-icon name="folder-home" class="h-4 w-4 mr-2 text-current" />
62-
<span>{{ __("statamic-backup::backup.restore.label") }}</span>
63-
</button>
64-
65-
<loading-graphic v-if="status === 'restoring'" :inline="true" text="" />
66-
67-
<span v-if="status === 'restored'" class="text-green-500 filename">
68-
{{ __("statamic-backup::backup.restore.success") }}
69-
<svg-icon
70-
v-if="status === 'restored'"
71-
name="light/check"
72-
class="h-4 w-4 ml-2"
73-
/>
74-
</span>
7555
</div>
7656
</div>
7757
</template>
@@ -92,9 +72,6 @@ export default {
9272
pause() {
9373
this.$emit("pause", this.file);
9474
},
95-
restore() {
96-
this.$emit("restore", this.file);
97-
},
9875
},
9976
};
10077
</script>

routes/cp.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
use Itiden\Backup\Http\Controllers\Api\BackupController;
55
use Itiden\Backup\Http\Controllers\Api\DestroyBackupController;
66
use Itiden\Backup\Http\Controllers\Api\RestoreController;
7-
use Itiden\Backup\Http\Controllers\Api\RestoreFromPathController;
87
use Itiden\Backup\Http\Controllers\Api\StateController;
98
use Itiden\Backup\Http\Controllers\Api\StoreBackupController;
109
use Itiden\Backup\Http\Controllers\DownloadBackupController;
@@ -54,8 +53,4 @@
5453
Route::post('/restore/{timestamp}', RestoreController::class)
5554
->middleware('can:restore backups')
5655
->name('restore');
57-
58-
Route::post('/restore-from-path', RestoreFromPathController::class)
59-
->middleware('can:restore backups')
60-
->name('restore-from-path');
6156
});

src/Http/Controllers/Api/RestoreFromPathController.php

Lines changed: 0 additions & 22 deletions
This file was deleted.

src/Http/Controllers/UploadController.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,21 @@
66

77
use Illuminate\Http\JsonResponse;
88
use Illuminate\Http\Request;
9+
use Itiden\Backup\Contracts\Repositories\BackupRepository;
910
use Itiden\Backup\DataTransferObjects\ChunkyTestDto;
1011
use Itiden\Backup\DataTransferObjects\ChunkyUploadDto;
1112
use Itiden\Backup\Http\Requests\ChunkyUploadRequest;
1213
use Itiden\Backup\Support\Facades\Chunky;
1314

1415
final readonly class UploadController
1516
{
16-
public function __invoke(ChunkyUploadRequest $request): JsonResponse
17+
public function __invoke(ChunkyUploadRequest $request, BackupRepository $repo): JsonResponse
1718
{
18-
return Chunky::put(ChunkyUploadDto::fromRequest($request));
19+
return Chunky::put(ChunkyUploadDto::fromRequest($request), onCompleted: function (string $completeFile) use (
20+
$repo,
21+
): void {
22+
$repo->add($completeFile);
23+
});
1924
}
2025

2126
public function test(Request $request): JsonResponse

src/Http/Requests/RestoreFromPathRequest.php

Lines changed: 0 additions & 17 deletions
This file was deleted.

src/Jobs/RestoreFromPathJob.php

Lines changed: 0 additions & 43 deletions
This file was deleted.

src/Support/Chunky.php

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Itiden\Backup\Support;
66

7+
use Closure;
78
use Illuminate\Contracts\Filesystem\Filesystem;
89
use Illuminate\Http\JsonResponse;
910
use Illuminate\Http\Response;
@@ -12,6 +13,8 @@
1213
use Itiden\Backup\DataTransferObjects\ChunkyUploadDto;
1314
use SplFileInfo;
1415

16+
use function Illuminate\Filesystem\join_paths;
17+
1518
final class Chunky
1619
{
1720
private Filesystem $disk;
@@ -33,9 +36,10 @@ public function path(?string $path = ''): string
3336
}
3437

3538
/**
36-
* put a from≤ chunks.
39+
* Store a chunk of a file. If all chunks are uploaded, merge them into a single file.
40+
* @param ?Closure<string> $onCompleted Callback to run when the file is fully uploaded.
3741
*/
38-
public function put(ChunkyUploadDto $dto): JsonResponse
42+
public function put(ChunkyUploadDto $dto, ?Closure $onCompleted = null): JsonResponse
3943
{
4044
if (!$this->disk->putFileAs($dto->path, $dto->file, $dto->filename . '.part' . $dto->currentChunk)) {
4145
return response()->json(['message' => 'Error saving chunk'], Response::HTTP_INTERNAL_SERVER_ERROR);
@@ -54,6 +58,9 @@ public function put(ChunkyUploadDto $dto): JsonResponse
5458
}
5559

5660
$completeFile = $this->mergeChunksIntoFile($dto->path, $dto->filename, $dto->totalChunks);
61+
if ($onCompleted) {
62+
$onCompleted($completeFile);
63+
}
5764
if ($completeFile) {
5865
return response()->json(
5966
['message' => 'File successfully uploaded', 'file' => $completeFile],
@@ -83,13 +90,15 @@ public function mergeChunksIntoFile(string $path, string $filename, int $totalCh
8390
}
8491
fclose($file);
8592

93+
$targetPath = join_paths('backups', $filename);
94+
8695
// move the file to the backups folder
87-
$this->disk->move($path . '/' . $filename, 'backups/' . $filename);
96+
$this->disk->move(join_paths($path, $filename), $targetPath);
8897

8998
// delete the chunks
9099
$this->disk->deleteDirectory($path);
91100

92-
return $this->path('backups/' . $filename);
101+
return $this->path($targetPath);
93102
}
94103

95104
/**

src/Support/Facades/Chunky.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010
use Itiden\Backup\Support\Chunky as ChunkySupport;
1111

1212
/**
13-
* @method static \Illuminate\Http\JsonResponse put(ChunkyUploadDto $dto)
13+
* @method static \Illuminate\Http\JsonResponse put(ChunkyUploadDto $dto, ?Closure<string> $onCompleted = null)
1414
* @method static \Illuminate\Http\JsonResponse exists(ChunkyTestDto $dto)
15-
* @method static string path()
15+
* @method static string path(string $path = '')
1616
*
1717
* @see \Itiden\Backup\Support\Chunky
1818
*/

0 commit comments

Comments
 (0)