|
| 1 | +using System; |
| 2 | +using System.IO; |
| 3 | +using System.IO.Compression; |
| 4 | +using System.Linq; |
| 5 | +using System.Threading.Tasks; |
| 6 | + |
| 7 | +namespace CDriveMaster.Core.Services; |
| 8 | + |
| 9 | +public sealed class DiagnosticExporter |
| 10 | +{ |
| 11 | + private readonly Func<string> outputDirectoryProvider; |
| 12 | + private readonly Func<string> appBaseDirectoryProvider; |
| 13 | + private readonly Func<DateTime> nowProvider; |
| 14 | + |
| 15 | + public DiagnosticExporter( |
| 16 | + Func<string>? outputDirectoryProvider = null, |
| 17 | + Func<string>? appBaseDirectoryProvider = null, |
| 18 | + Func<DateTime>? nowProvider = null) |
| 19 | + { |
| 20 | + this.outputDirectoryProvider = outputDirectoryProvider |
| 21 | + ?? (() => Environment.GetFolderPath(Environment.SpecialFolder.Desktop)); |
| 22 | + this.appBaseDirectoryProvider = appBaseDirectoryProvider |
| 23 | + ?? (() => AppContext.BaseDirectory); |
| 24 | + this.nowProvider = nowProvider ?? (() => DateTime.Now); |
| 25 | + } |
| 26 | + |
| 27 | + public async Task<string> ExportAsync() |
| 28 | + { |
| 29 | + string outputDirectory = outputDirectoryProvider(); |
| 30 | + Directory.CreateDirectory(outputDirectory); |
| 31 | + |
| 32 | + string zipPath = Path.Combine(outputDirectory, $"CDriveMaster_Diag_{nowProvider():yyyyMMdd_HHmmss}.zip"); |
| 33 | + |
| 34 | + string tempPath = Path.GetTempFileName(); |
| 35 | + if (File.Exists(tempPath)) |
| 36 | + { |
| 37 | + File.Delete(tempPath); |
| 38 | + } |
| 39 | + |
| 40 | + Directory.CreateDirectory(tempPath); |
| 41 | + |
| 42 | + try |
| 43 | + { |
| 44 | + string sysInfoPath = Path.Combine(tempPath, "sysinfo.txt"); |
| 45 | + string sysInfo = string.Join(Environment.NewLine, new[] |
| 46 | + { |
| 47 | + $"Timestamp: {nowProvider():yyyy-MM-dd HH:mm:ss}", |
| 48 | + $"OS Version: {Environment.OSVersion.VersionString}", |
| 49 | + $"Is64BitOperatingSystem: {Environment.Is64BitOperatingSystem}", |
| 50 | + $"IsElevated: {PlatformProbe.IsElevated}" |
| 51 | + }); |
| 52 | + await File.WriteAllTextAsync(sysInfoPath, sysInfo); |
| 53 | + |
| 54 | + string appBase = appBaseDirectoryProvider(); |
| 55 | + string logsPath = Path.Combine(appBase, "Logs"); |
| 56 | + if (Directory.Exists(logsPath)) |
| 57 | + { |
| 58 | + string tempLogs = Path.Combine(tempPath, "Logs"); |
| 59 | + Directory.CreateDirectory(tempLogs); |
| 60 | + |
| 61 | + var jsonFiles = Directory |
| 62 | + .EnumerateFiles(logsPath, "*.json", SearchOption.AllDirectories) |
| 63 | + .ToList(); |
| 64 | + |
| 65 | + foreach (var file in jsonFiles) |
| 66 | + { |
| 67 | + try |
| 68 | + { |
| 69 | + string relativePath = Path.GetRelativePath(logsPath, file); |
| 70 | + string target = Path.Combine(tempLogs, relativePath); |
| 71 | + string? targetDir = Path.GetDirectoryName(target); |
| 72 | + if (!string.IsNullOrWhiteSpace(targetDir)) |
| 73 | + { |
| 74 | + Directory.CreateDirectory(targetDir); |
| 75 | + } |
| 76 | + |
| 77 | + File.Copy(file, target, overwrite: true); |
| 78 | + } |
| 79 | + catch (IOException) |
| 80 | + { |
| 81 | + // Ignore a single locked/corrupted file and continue collecting diagnostics. |
| 82 | + } |
| 83 | + catch (UnauthorizedAccessException) |
| 84 | + { |
| 85 | + // Ignore a single inaccessible file and continue collecting diagnostics. |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + string rulesPath = Path.Combine(appBase, "Rules"); |
| 91 | + if (Directory.Exists(rulesPath)) |
| 92 | + { |
| 93 | + string tempRules = Path.Combine(tempPath, "Rules"); |
| 94 | + CopyDirectory(rulesPath, tempRules); |
| 95 | + } |
| 96 | + |
| 97 | + if (File.Exists(zipPath)) |
| 98 | + { |
| 99 | + File.Delete(zipPath); |
| 100 | + } |
| 101 | + |
| 102 | + ZipFile.CreateFromDirectory(tempPath, zipPath); |
| 103 | + return zipPath; |
| 104 | + } |
| 105 | + finally |
| 106 | + { |
| 107 | + try |
| 108 | + { |
| 109 | + if (Directory.Exists(tempPath)) |
| 110 | + { |
| 111 | + Directory.Delete(tempPath, recursive: true); |
| 112 | + } |
| 113 | + } |
| 114 | + catch |
| 115 | + { |
| 116 | + // Best effort cleanup only. |
| 117 | + } |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + private static void CopyDirectory(string sourceDir, string targetDir) |
| 122 | + { |
| 123 | + Directory.CreateDirectory(targetDir); |
| 124 | + |
| 125 | + foreach (var file in Directory.EnumerateFiles(sourceDir)) |
| 126 | + { |
| 127 | + string target = Path.Combine(targetDir, Path.GetFileName(file)); |
| 128 | + File.Copy(file, target, overwrite: true); |
| 129 | + } |
| 130 | + |
| 131 | + foreach (var dir in Directory.EnumerateDirectories(sourceDir)) |
| 132 | + { |
| 133 | + string targetSubDir = Path.Combine(targetDir, Path.GetFileName(dir)); |
| 134 | + CopyDirectory(dir, targetSubDir); |
| 135 | + } |
| 136 | + } |
| 137 | +} |
0 commit comments