diff --git a/Wino.Core.Domain/Constants.cs b/Wino.Core.Domain/Constants.cs index 36aaac94..1a274c07 100644 --- a/Wino.Core.Domain/Constants.cs +++ b/Wino.Core.Domain/Constants.cs @@ -13,5 +13,6 @@ public const string ClientLogFile = "Client_.log"; public const string ServerLogFile = "Server_.log"; + public const string LogArchiveFileName = "WinoLogs.zip"; } } diff --git a/Wino.Core.Domain/Interfaces/IFileService.cs b/Wino.Core.Domain/Interfaces/IFileService.cs index 5500e2f9..6fbe5d96 100644 --- a/Wino.Core.Domain/Interfaces/IFileService.cs +++ b/Wino.Core.Domain/Interfaces/IFileService.cs @@ -8,5 +8,13 @@ namespace Wino.Core.Domain.Interfaces Task CopyFileAsync(string sourceFilePath, string destinationFolderPath); Task GetFileStreamAsync(string folderPath, string fileName); Task GetFileContentByApplicationUriAsync(string resourcePath); + + /// + /// Zips all existing logs and saves to picked destination folder. + /// + /// Folder path where logs are stored. + /// Target path to save the archive file. + /// True if zip is created with at least one item, false if logs are not found. + Task SaveLogsToFolderAsync(string logsFolder, string destinationFolder); } } diff --git a/Wino.Core.UWP/Services/FileService.cs b/Wino.Core.UWP/Services/FileService.cs index fdfbf5ab..4e92dfb1 100644 --- a/Wino.Core.UWP/Services/FileService.cs +++ b/Wino.Core.UWP/Services/FileService.cs @@ -1,7 +1,9 @@ using System; using System.IO; +using System.IO.Compression; using System.Threading.Tasks; using Windows.Storage; +using Wino.Core.Domain; using Wino.Core.Domain.Interfaces; namespace Wino.Core.UWP.Services @@ -34,5 +36,27 @@ namespace Wino.Core.UWP.Services return await createdFile.OpenStreamForWriteAsync(); } + + public async Task SaveLogsToFolderAsync(string logsFolder, string destinationFolder) + { + var logFiles = Directory.GetFiles(logsFolder, "*.log"); + + if (logFiles.Length == 0) return false; + + using var fileStream = await GetFileStreamAsync(destinationFolder, Constants.LogArchiveFileName); + using var archive = new ZipArchive(fileStream, ZipArchiveMode.Create, true); + + foreach (var logFile in logFiles) + { + using FileStream logFileStream = File.Open(logFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); + + var zipArchiveEntry = archive.CreateEntry(Path.GetFileName(logFile), CompressionLevel.Fastest); + using var zipStream = zipArchiveEntry.Open(); + + await logFileStream.CopyToAsync(zipStream); + } + + return true; + } } } diff --git a/Wino.Mail.ViewModels/AboutPageViewModel.cs b/Wino.Mail.ViewModels/AboutPageViewModel.cs index 36987a56..a767c199 100644 --- a/Wino.Mail.ViewModels/AboutPageViewModel.cs +++ b/Wino.Mail.ViewModels/AboutPageViewModel.cs @@ -1,14 +1,13 @@ using System; -using System.IO; using System.Threading.Tasks; using CommunityToolkit.Mvvm.Input; using Wino.Core.Domain; +using Wino.Core.Domain.Enums; using Wino.Core.Domain.Interfaces; -using Wino.Core.Services; namespace Wino.Mail.ViewModels { - public class AboutPageViewModel : BaseViewModel + public partial class AboutPageViewModel : BaseViewModel { private readonly IStoreRatingService _storeRatingService; private readonly INativeAppService _nativeAppService; @@ -22,9 +21,6 @@ namespace Wino.Mail.ViewModels public string PrivacyPolicyUrl => "https://www.winomail.app/privacy_policy.html"; public string PaypalUrl => "https://paypal.me/bkaankose?country.x=PL&locale.x=en_US"; - public AsyncRelayCommand NavigateCommand { get; set; } - public AsyncRelayCommand ShareWinoLogCommand { get; set; } - public AsyncRelayCommand ShareProtocolLogCommand { get; set; } public IPreferencesService PreferencesService { get; } public AboutPageViewModel(IStoreRatingService storeRatingService, @@ -42,9 +38,6 @@ namespace Wino.Mail.ViewModels _fileService = fileService; PreferencesService = preferencesService; - NavigateCommand = new AsyncRelayCommand(Navigate); - ShareWinoLogCommand = new AsyncRelayCommand(ShareWinoLogAsync); - ShareProtocolLogCommand = new AsyncRelayCommand(ShareProtocolLogAsync); } protected override void OnActivated() @@ -70,35 +63,28 @@ namespace Wino.Mail.ViewModels } } - private Task ShareProtocolLogAsync() - => SaveLogInternalAsync(ImapTestService.ProtocolLogFileName); - - private Task ShareWinoLogAsync() - => SaveLogInternalAsync(Constants.ClientLogFile); - - private async Task SaveLogInternalAsync(string sourceFileName) + [RelayCommand] + private async Task ShareWinoLogAsync() { var appDataFolder = _appInitializerService.ApplicationDataFolderPath; - var logFile = Path.Combine(appDataFolder, sourceFileName); - - if (!File.Exists(logFile)) - { - DialogService.InfoBarMessage(Translator.Info_LogsNotFoundTitle, Translator.Info_LogsNotFoundMessage, Core.Domain.Enums.InfoBarMessageType.Warning); - return; - } - var selectedFolderPath = await DialogService.PickWindowsFolderAsync(); if (string.IsNullOrEmpty(selectedFolderPath)) return; - var copiedFilePath = await _fileService.CopyFileAsync(logFile, selectedFolderPath); + var areLogsSaved = await _fileService.SaveLogsToFolderAsync(appDataFolder, selectedFolderPath).ConfigureAwait(false); - var copiedFileName = Path.GetFileName(copiedFilePath); - - DialogService.InfoBarMessage(Translator.Info_LogsSavedTitle, string.Format(Translator.Info_LogsSavedMessage, copiedFileName), Core.Domain.Enums.InfoBarMessageType.Success); + if (areLogsSaved) + { + DialogService.InfoBarMessage(Translator.Info_LogsSavedTitle, string.Format(Translator.Info_LogsSavedMessage, Constants.LogArchiveFileName), InfoBarMessageType.Success); + } + else + { + DialogService.InfoBarMessage(Translator.Info_LogsNotFoundTitle, Translator.Info_LogsNotFoundMessage, InfoBarMessageType.Error); + } } + [RelayCommand] private async Task Navigate(object url) { if (url is string stringUrl) diff --git a/Wino.Mail/Views/Settings/AboutPage.xaml b/Wino.Mail/Views/Settings/AboutPage.xaml index 163241ce..65280613 100644 --- a/Wino.Mail/Views/Settings/AboutPage.xaml +++ b/Wino.Mail/Views/Settings/AboutPage.xaml @@ -61,9 +61,9 @@ + HorizontalAlignment="Center" + VerticalAlignment="Center" + Data="{StaticResource OpenInNewWindowPathIcon}" /> @@ -81,9 +81,9 @@ + HorizontalAlignment="Center" + VerticalAlignment="Center" + Data="{StaticResource OpenInNewWindowPathIcon}" /> @@ -98,9 +98,9 @@ + HorizontalAlignment="Center" + VerticalAlignment="Center" + Data="{StaticResource OpenInNewWindowPathIcon}" /> @@ -118,9 +118,9 @@ + HorizontalAlignment="Center" + VerticalAlignment="Center" + Data="{StaticResource OpenInNewWindowPathIcon}" /> @@ -135,17 +135,10 @@ -