final stage

This commit is contained in:
2026-06-01 16:11:29 +02:00
parent 5557650824
commit 3424c1c9f7
21 changed files with 5010 additions and 47 deletions
@@ -0,0 +1,118 @@
<?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]);
}
}
@@ -242,23 +242,43 @@ class StorageRepositoryController extends Controller
$normalizedPath = $this->repoService->normalizePath($path);
try {
$stream = $filesystem->readStream($normalizedPath);
} catch (\Exception $e) {
Log::error("StorageRepository importToLocal: readStream failed for repo #{$storageRepository->id} path '{$path}': " . $e->getMessage());
return response()->json(['success' => false, 'message' => 'File non trovato nel repository remoto o impossibile da leggere.'], 404);
if ($storageRepository->tipo === 'google_drive') {
try {
$fileData = $this->repoService->readGoogleDriveFile($storageRepository, $normalizedPath, $basename);
} catch (\Exception $e) {
Log::error("StorageRepository importToLocal: readGoogleDriveFile failed for repo #{$storageRepository->id} path '{$path}': " . $e->getMessage());
return response()->json(['success' => false, 'message' => 'Impossibile leggere il file da Google Drive: ' . $e->getMessage()], 500);
}
} else {
try {
$fileData = [];
$fileData['stream'] = $filesystem->readStream($normalizedPath);
$fileData['mimeType'] = null;
try { $fileData['mimeType'] = $filesystem->mimeType($normalizedPath); } catch (\Exception) {}
$fileData['fileSize'] = null;
try { $fileData['fileSize'] = $filesystem->fileSize($normalizedPath); } catch (\Exception) {}
$fileData['basename'] = $basename;
} catch (\Exception $e) {
Log::error("StorageRepository importToLocal: readStream failed for repo #{$storageRepository->id} path '{$path}': " . $e->getMessage());
return response()->json(['success' => false, 'message' => 'File non trovato nel repository remoto o impossibile da leggere.'], 404);
}
}
$stream = $fileData['stream'];
if (!$stream) {
return response()->json(['success' => false, 'message' => 'Impossibile leggere il file remoto.'], 500);
}
$basename = $fileData['basename'];
$mimeType = $fileData['mimeType'] ?? null;
$fileSize = $fileData['fileSize'] ?? null;
$disk = \App\Models\AppSetting::getDocumentiStorageDisk();
$storagePath = \App\Models\AppSetting::getDocumentiStoragePath();
$extension = strtolower(pathinfo($basename, PATHINFO_EXTENSION));
$nomeSenzaEstensione = pathinfo($basename, PATHINFO_FILENAME);
$uniqueName = preg_replace('/[^a-zA-Z0-9_\-\x{80}-\x{FF}]/u', '_', $nomeSenzaEstensione) . '_' . time() . ($extension ? '.' . $extension : '');
$uniqueName = preg_replace('/[^a-zA-Z0-9_\-\x80-\x{FF}]/u', '_', $nomeSenzaEstensione) . '_' . time() . ($extension ? '.' . $extension : '');
$filePath = $storagePath . '/' . date('Y/m/d') . '/' . $uniqueName;
$stored = \Illuminate\Support\Facades\Storage::disk($disk)->writeStream($filePath, $stream);
@@ -268,11 +288,6 @@ class StorageRepositoryController extends Controller
return response()->json(['success' => false, 'message' => 'Errore durante il salvataggio locale del file.'], 500);
}
$mimeType = null;
try { $mimeType = $filesystem->mimeType($path); } catch (\Exception) {}
$fileSize = null;
try { $fileSize = $filesystem->fileSize($path); } catch (\Exception) {}
$visibilita = $validated['visibilita'] ?? 'pubblico';
$targetType = null;
$targetId = null;