diff --git a/src/GIMI-ModManager.WinUI/Services/ActivationService.cs b/src/GIMI-ModManager.WinUI/Services/ActivationService.cs index d2b55bfd..e8f22c49 100644 --- a/src/GIMI-ModManager.WinUI/Services/ActivationService.cs +++ b/src/GIMI-ModManager.WinUI/Services/ActivationService.cs @@ -5,6 +5,7 @@ using CommunityToolkit.WinUI; using GIMI_ModManager.Core.Contracts.Services; using GIMI_ModManager.Core.GamesService; +using GIMI_ModManager.Core.Helpers; using GIMI_ModManager.WinUI.Activation; using GIMI_ModManager.WinUI.Contracts.Services; using GIMI_ModManager.WinUI.Helpers; @@ -148,6 +149,7 @@ private async Task StartupAsync() } const int MinimizedPosition = -32000; + private async Task SetWindowSettings() { var screenSize = await _localSettingsService.ReadSettingAsync(ScreenSizeSettings.Key); @@ -195,23 +197,34 @@ private async void OnApplicationExit(object sender, WindowEventArgs args) args.Handled = true; var notificationCleanup = Task.Run(_modNotificationManager.CleanupAsync).ConfigureAwait(false); - var saveWindowSettingsTask = SaveWindowSettingsAsync().ConfigureAwait(false); + var saveWindowSettingsTask = SaveWindowSettingsAsync(); _logger.Debug("JASM shutting down..."); _modUpdateAvailableChecker.CancelAndStop(); _updateChecker.CancelAndStop(); - + await saveWindowSettingsTask; await _windowManagerService.CloseWindowsAsync().ConfigureAwait(false); var tmpDir = new DirectoryInfo(App.TMP_DIR); if (tmpDir.Exists) { + tmpDir.Refresh(); _logger.Debug("Deleting temporary directory: {Path}", tmpDir.FullName); - tmpDir.Delete(true); + try + { + tmpDir.EnumerateFiles("*", SearchOption.AllDirectories) + .ForEach(f => f.Attributes = FileAttributes.Normal); + tmpDir.EnumerateDirectories("*", SearchOption.AllDirectories) + .ForEach(d => d.Attributes = FileAttributes.Normal); + tmpDir.Delete(true); + } + catch (Exception e) + { + _logger.Warning(e, "Failed to delete temporary directory: {Path}", tmpDir.FullName); + } } - await saveWindowSettingsTask; await notificationCleanup; _logger.Debug("JASM shutdown complete."); } diff --git a/src/GIMI-ModManager.WinUI/Services/ImageHandlerService.cs b/src/GIMI-ModManager.WinUI/Services/ImageHandlerService.cs index 632b0e61..c294a592 100644 --- a/src/GIMI-ModManager.WinUI/Services/ImageHandlerService.cs +++ b/src/GIMI-ModManager.WinUI/Services/ImageHandlerService.cs @@ -6,7 +6,7 @@ namespace GIMI_ModManager.WinUI.Services; public class ImageHandlerService { - private readonly DirectoryInfo _tmpFolder = new(Path.Combine(App.TMP_DIR, "Images")); + private readonly string _tmpFolder = Path.Combine(App.TMP_DIR, "Images"); public readonly Uri PlaceholderImageUri = new(Path.Combine(App.ASSET_DIR, "ModPanePlaceholder.webp")); @@ -60,13 +60,14 @@ public async Task DownloadImageAsync(Uri url, CancellationToken can throw new ArgumentException($"Url must be a valid image url. Invalid extension: {invalidExtension}"); } + var tmpFolder = new DirectoryInfo(_tmpFolder); - var tmpFile = Path.Combine(_tmpFolder.FullName, + var tmpFile = Path.Combine(tmpFolder.FullName, $"WEB_DOWNLOAD_{Guid.NewGuid():N}{Path.GetExtension(url.ToString())}"); - if (!_tmpFolder.Exists) - _tmpFolder.Create(); + if (!tmpFolder.Exists) + tmpFolder.Create(); var client = _httpClientFactory.CreateClient(); @@ -81,12 +82,14 @@ public async Task DownloadImageAsync(Uri url, CancellationToken can private async Task CopyImageToTmpFolder(StorageFile file) { - if (!_tmpFolder.Exists) _tmpFolder.Create(); + var tmpFolder = new DirectoryInfo(_tmpFolder); - var tmpFile = new FileInfo(Path.Combine(_tmpFolder.FullName, file.Name)); + if (!tmpFolder.Exists) tmpFolder.Create(); + + var tmpFile = new FileInfo(Path.Combine(tmpFolder.FullName, file.Name)); if (tmpFile.Exists) tmpFile.Delete(); - var tmpImage = await file.CopyAsync(await StorageFolder.GetFolderFromPathAsync(_tmpFolder.FullName)); + var tmpImage = await file.CopyAsync(await StorageFolder.GetFolderFromPathAsync(tmpFolder.FullName)); var extension = tmpImage.FileType; var newFileName = $"{Path.GetFileNameWithoutExtension(tmpImage.Name)}_{Guid.NewGuid()}{extension}";