database backups

This commit is contained in:
Saeed Vaziry
2023-08-25 21:05:18 +02:00
parent f9ac454a7c
commit ea3f011f34
55 changed files with 1111 additions and 172 deletions

View File

@ -2,21 +2,25 @@
namespace App\StorageProviders;
use App\Exceptions\BackupFileException;
use App\Models\Server;
use App\SSHCommands\Storage\DownloadFromDropboxCommand;
use App\SSHCommands\Storage\UploadToDropboxCommand;
use Illuminate\Support\Facades\Http;
use Laravel\Socialite\Facades\Socialite;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Throwable;
class Dropbox extends AbstractStorageProvider
{
public function connect(): RedirectResponse
{
session()->put('storage_provider_id', $this->storageProvider->id);
protected string $apiUrl = 'https://api.dropboxapi.com/2';
return Socialite::driver('dropbox')->redirect();
public function connect(): bool
{
$res = Http::withToken($this->storageProvider->credentials['token'])
->post($this->apiUrl.'/check/user', [
'query' => ''
]);
return $res->successful();
}
/**
@ -28,15 +32,19 @@ public function upload(Server $server, string $src, string $dest): array
new UploadToDropboxCommand(
$src,
$dest,
$this->storageProvider->token
$this->storageProvider->credentials['token']
),
'upload-to-dropbox'
);
$data = json_decode($upload);
$data = json_decode($upload, true);
if (isset($data['error'])) {
throw new BackupFileException("Failed to upload to Dropbox ".$data['error_summary'] ?? '');
}
return [
'size' => $data?->size,
'size' => $data['size'] ?? null,
];
}
@ -49,7 +57,7 @@ public function download(Server $server, string $src, string $dest): void
new DownloadFromDropboxCommand(
$src,
$dest,
$this->storageProvider->token
$this->storageProvider->credentials['token']
),
'download-from-dropbox'
);
@ -61,11 +69,11 @@ public function delete(array $paths): void
foreach ($paths as $path) {
$data[] = ['path' => $path];
}
Http::withToken($this->storageProvider->token)
Http::withToken($this->storageProvider->credentials['token'])
->withHeaders([
'Content-Type:application/json',
])
->post('https://api.dropboxapi.com/2/files/delete_batch', [
->post($this->apiUrl.'/files/delete_batch', [
'entries' => $data,
]);
}