This commit is contained in:
Bozhidar 2024-05-02 18:38:42 +03:00
parent 98efa1b42c
commit 13b5e89f81
4 changed files with 25 additions and 11 deletions

View file

@ -50,20 +50,26 @@ class BackupStorage
return $rootPath;
}
public static function getInstance()
public static function getInstance($path = false)
{
$rootPath = self::getPath();
if ($path) {
$rootPath = $path;
}
$storageBuild = Storage::build([
'driver' => 'local',
'throw' => false,
'root' => $rootPath,
]);
$storageBuild->buildTemporaryUrlsUsing(function ($path, $expiration, $options) {
$storageBuild->buildTemporaryUrlsUsing(function ($path, $expiration, $options) use($rootPath) {
return URL::temporarySignedRoute(
'backup.download',
$expiration,
array_merge($options, ['path' => $path])
array_merge($options, [
'path' => $path,
'root_path' => $rootPath,
])
);
});

View file

@ -2,6 +2,7 @@
namespace App\Filament\Resources;
use App\BackupStorage;
use App\Filament\Enums\BackupStatus;
use App\Filament\Enums\BackupType;
use App\Filament\Resources\BackupResource\Pages;
@ -102,9 +103,11 @@ class BackupResource extends Resource
Tables\Actions\Action::make('download')
->icon('heroicon-o-arrow-down-tray')
->action(function (Backup $backup) {
$url = Storage::disk('backups')
->temporaryUrl($backup->filepath, Carbon::now()->addMinutes(5));
return redirect($url);
$backupStorage = BackupStorage::getInstance($backup->root_path);
$tempUrl = $backupStorage->temporaryUrl($backup->file_name, Carbon::now()->addMinutes(5));
return redirect($tempUrl);
}),
Tables\Actions\ViewAction::make(),
])

View file

@ -3,6 +3,7 @@ declare(strict_types=1);
namespace App\Http\Controllers;
use App\BackupStorage;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\URL;
@ -19,7 +20,9 @@ class BackupDownloadController extends Controller
return response('Invalid URL provided');
}
return Storage::disk('backups')->download($request->get('path'));
$backupStorage = BackupStorage::getInstance($request->get('root_path'));
return $backupStorage->download($request->get('path'));
}
}

View file

@ -89,7 +89,6 @@ class Backup extends Model
public function checkBackup()
{
if ($this->status == BackupStatus::Processing) {
$backupDoneFile = $this->path.'/backup.done';
if (file_exists($backupDoneFile)) {
@ -98,7 +97,7 @@ class Backup extends Model
mkdir($tempValidatePath);
}
shell_exec('cd '.$tempValidatePath.' && unzip -o '.Storage::disk('backups')->path($this->filepath));
shell_exec('cd '.$tempValidatePath.' && unzip -o '.$this->file_path);
$validateDatabaseFile = $tempValidatePath.'/database.sql';
$validateEnvFile = $tempValidatePath.'/.env';
@ -136,10 +135,13 @@ class Backup extends Model
}
ShellApi::safeDelete($this->path,[
Storage::path('backups')
$this->root_path
]);
ShellApi::safeDelete($this->temp_path,[
$this->root_path
]);
$this->size = filesize(Storage::disk('backups')->path($this->filepath));
$this->size = filesize($this->file_path);
$this->status = 'completed';
$this->completed = true;
$this->completed_at = now();