| | | 1 | | using LifeChart.Application.Interfaces; |
| | | 2 | | |
| | | 3 | | namespace LifeChart.Infrastructure.Backup; |
| | | 4 | | |
| | | 5 | | public class LocalExportProvider : IBackupProvider |
| | | 6 | | { |
| | | 7 | | private readonly string _backupDir; |
| | | 8 | | |
| | 0 | 9 | | public LocalExportProvider() |
| | | 10 | | { |
| | 0 | 11 | | _backupDir = Path.Combine( |
| | 0 | 12 | | Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), |
| | 0 | 13 | | "LifeChart", "Backups"); |
| | | 14 | | |
| | 0 | 15 | | Directory.CreateDirectory(_backupDir); |
| | 0 | 16 | | } |
| | | 17 | | |
| | | 18 | | public async Task BackupAsync(string dbPath) |
| | | 19 | | { |
| | 0 | 20 | | var fileName = $"lifechart_backup_{DateTime.Today:yyyy-MM-dd}.sqlite"; |
| | 0 | 21 | | var destPath = Path.Combine(_backupDir, fileName); |
| | 0 | 22 | | await CopyFileAsync(dbPath, destPath); |
| | 0 | 23 | | } |
| | | 24 | | |
| | | 25 | | public Task<IEnumerable<BackupInfo>> ListBackupsAsync() |
| | | 26 | | { |
| | 0 | 27 | | if (!Directory.Exists(_backupDir)) |
| | 0 | 28 | | return Task.FromResult(Enumerable.Empty<BackupInfo>()); |
| | | 29 | | |
| | 0 | 30 | | var files = Directory |
| | 0 | 31 | | .GetFiles(_backupDir, "lifechart_backup_*.sqlite") |
| | 0 | 32 | | .Select(path => |
| | 0 | 33 | | { |
| | 0 | 34 | | var info = new FileInfo(path); |
| | 0 | 35 | | return new BackupInfo(info.Name, info.LastWriteTimeUtc, info.Length); |
| | 0 | 36 | | }) |
| | 0 | 37 | | .OrderByDescending(b => b.CreatedAt); |
| | | 38 | | |
| | 0 | 39 | | return Task.FromResult<IEnumerable<BackupInfo>>(files); |
| | | 40 | | } |
| | | 41 | | |
| | | 42 | | public async Task RestoreAsync(BackupInfo backup, string targetPath) |
| | | 43 | | { |
| | 0 | 44 | | var sourcePath = Path.Combine(_backupDir, backup.FileName); |
| | 0 | 45 | | if (!File.Exists(sourcePath)) |
| | 0 | 46 | | throw new FileNotFoundException( |
| | 0 | 47 | | $"Backup-Datei nicht gefunden: {backup.FileName}"); |
| | | 48 | | |
| | 0 | 49 | | await CopyFileAsync(sourcePath, targetPath); |
| | 0 | 50 | | } |
| | | 51 | | |
| | 0 | 52 | | public string BackupDirectory => _backupDir; |
| | | 53 | | |
| | | 54 | | private static async Task CopyFileAsync(string source, string destination) |
| | | 55 | | { |
| | 0 | 56 | | await using var src = File.OpenRead(source); |
| | 0 | 57 | | await using var dst = File.Create(destination); |
| | 0 | 58 | | await src.CopyToAsync(dst); |
| | 0 | 59 | | } |
| | | 60 | | } |