Files
glastree/app/Http/Controllers/Admin/BackupController.php
T
2026-06-01 16:11:29 +02:00

119 lines
3.9 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use App\Models\AppSetting;
use App\Services\BackupService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\View\View;
class BackupController extends Controller
{
private BackupService $backupService;
public function __construct(BackupService $backupService)
{
$this->backupService = $backupService;
}
public function index(): View
{
$backups = $this->backupService->list();
$config = $this->backupService->getConfig();
return view('admin.backup.index', compact('backups', 'config'));
}
public function run(): RedirectResponse
{
$result = $this->backupService->run();
if ($result['success']) {
return redirect()->route('admin.backup.index')
->with('success', 'Backup completato: ' . ($result['filename'] ?? '') . ' (' . ($result['size_formatted'] ?? '') . ')');
}
return redirect()->route('admin.backup.index')
->with('error', $result['message'] ?? 'Errore durante il backup.');
}
public function download(string $filename)
{
$filepath = $this->backupService->download($filename);
if (!$filepath) {
return redirect()->route('admin.backup.index')
->with('error', 'File di backup non trovato.');
}
return response()->download($filepath)->deleteFileAfterSend(false);
}
public function destroy(string $filename): RedirectResponse
{
if ($this->backupService->delete($filename)) {
return redirect()->route('admin.backup.index')
->with('success', 'Backup "' . $filename . '" eliminato.');
}
return redirect()->route('admin.backup.index')
->with('error', 'Impossibile eliminare il backup.');
}
public function updateConfig(Request $request): RedirectResponse
{
$validated = $request->validate([
'backup_path' => 'nullable|string|max:255',
'backup_retention_days' => 'nullable|integer|min:1|max:365',
'backup_include_files' => 'nullable|boolean',
'backup_include_env' => 'nullable|boolean',
]);
$this->backupService->saveConfig($validated);
return redirect()->route('admin.backup.index')
->with('success', 'Configurazione backup salvata.');
}
public function toggleAuto(): RedirectResponse
{
$enabled = !AppSetting::getSetting('backup_auto_enabled', false);
$setting = AppSetting::first() ?? new AppSetting();
$setting->backup_auto_enabled = $enabled;
$setting->save();
$msg = $enabled ? 'Backup automatico abilitato.' : 'Backup automatico disabilitato.';
return redirect()->route('admin.backup.index')->with('success', $msg);
}
public function saveAutoConfig(Request $request): RedirectResponse
{
$validated = $request->validate([
'backup_auto_enabled' => 'nullable|boolean',
'backup_auto_frequency' => 'nullable|in:daily,weekly,monthly',
'backup_auto_hour' => 'nullable|integer|min:0|max:23',
]);
$setting = AppSetting::first() ?? new AppSetting();
$setting->backup_auto_enabled = !empty($validated['backup_auto_enabled']);
$setting->backup_auto_frequency = $validated['backup_auto_frequency'] ?? 'daily';
$setting->backup_auto_hour = $validated['backup_auto_hour'] ?? 3;
$setting->save();
return redirect()->route('admin.backup.index')
->with('success', 'Configurazione backup automatico salvata.');
}
public function downloadMigrationScript(): JsonResponse
{
$script = view('admin.backup.migration-script')->render();
return response()->json(['script' => $script]);
}
}