using System.Diagnostics; using System.Runtime.InteropServices; using Duempelkas.App.Services; namespace Duempelkas.Infrastructure.Services; public class FileSaveService : IFileSaveService { public async Task SaveFileAsync(byte[] content, string suggestedFileName) { var filePath = await ShowSaveDialogAsync(suggestedFileName); if (filePath == null) return null; await File.WriteAllBytesAsync(filePath, content); return filePath; } public Task OpenFileAsync(string filePath) { if (string.IsNullOrWhiteSpace(filePath) || !File.Exists(filePath)) { return Task.CompletedTask; } return Task.Run(() => { if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { Process.Start(new ProcessStartInfo { FileName = filePath, UseShellExecute = true }); return; } if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) { Process.Start("open", filePath); return; } Process.Start("xdg-open", filePath); }); } private static Task ShowSaveDialogAsync(string suggestedFileName) { return Task.Run(() => { if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { // Fallback: save to Documents folder var documentsPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); return Path.Combine(documentsPath, suggestedFileName); } var extension = Path.GetExtension(suggestedFileName); var filterName = extension == ".pdf" ? "PDF-Dateien" : "Dateien"; var filter = $"{filterName} (*{extension})|*{extension}|Alle Dateien (*.*)|*.*"; var script = $@" Add-Type -AssemblyName System.Windows.Forms $dialog = New-Object System.Windows.Forms.SaveFileDialog $dialog.Filter = '{filter}' $dialog.FileName = '{suggestedFileName.Replace("'", "''")}' $dialog.Title = 'Datei speichern' if ($dialog.ShowDialog() -eq 'OK') {{ $dialog.FileName }} else {{ '' }} "; var psi = new ProcessStartInfo { FileName = "powershell", Arguments = $"-NoProfile -Command \"{script.Replace("\"", "\\\"\"")}\"", RedirectStandardOutput = true, UseShellExecute = false, CreateNoWindow = true }; using var process = Process.Start(psi); if (process == null) return null; var result = process.StandardOutput.ReadToEnd().Trim(); process.WaitForExit(); return string.IsNullOrWhiteSpace(result) ? null : result; }); } }