diff --git a/DeFRaG_Helper/Helpers/CreateAndUpdateDB.cs b/DeFRaG_Helper/Helpers/CreateAndUpdateDB.cs index fd899e3..fe53490 100644 --- a/DeFRaG_Helper/Helpers/CreateAndUpdateDB.cs +++ b/DeFRaG_Helper/Helpers/CreateAndUpdateDB.cs @@ -18,7 +18,7 @@ internal class CreateAndUpdateDB { // Define a static HttpClient instance at the class level - private static readonly HttpClient httpClient = new HttpClient(); + private static readonly HttpClient httpClient = CreateHttpClient(); //method to create the database in the Appdata folder. DO ONLY USE TO CREATE A NEW DB!!! private static readonly string AppDataPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData); private static readonly string AppDataFolder = Path.Combine(AppDataPath, "DeFRaG_Helper"); @@ -29,7 +29,14 @@ private static void ShowMessage(string message) App.Current.Dispatcher.Invoke(() => { MainWindow.Instance.ShowMessage(message); }); } + private static HttpClient CreateHttpClient() + { + var handler = new HttpClientHandler(); + handler.ServerCertificateCustomValidationCallback = + (sender, certificate, chain, sslPolicyErrors) => true; // Bypass SSL certificate validation + return new HttpClient(handler); + } private static void CreateDB() { if (!System.IO.Directory.Exists(AppDataFolder)) @@ -630,10 +637,10 @@ private async static Task GetMapDetails(MapData tempMap, int mapCount) //update the map in the database with method UpdateOrAddMap in MapViewModel var mapViewModel = await MapViewModel.GetInstanceAsync(); - if (map.Name == null || map.Name == "") - { - map.Name = Path.GetFileNameWithoutExtension(tempMap.Name); - } + //if (map.Name == null || map.Name == "") + // { + // map.Name = Path.GetFileNameWithoutExtension(tempMap.Name); + //} //Update or add map to the database await mapViewModel.UpdateOrAddMap(map); @@ -698,6 +705,43 @@ public static List ExtractImageUrls(string html) } + //public static async Task DownloadImageAsync(string imageUrl, string baseUrl) + //{ + // // Construct the full URL for the image + // string fullUrl = new Uri(new Uri(baseUrl), imageUrl).ToString(); + // // Extract the original file name from the image URL + // string fileName = Path.GetFileName(new Uri(fullUrl).AbsolutePath); + // // Determine the local path for saving the image + // string folderPath = GetImagePath(imageUrl); + // string filePath = Path.Combine(folderPath, fileName); + + // using (var client = httpClient) + // { + // var response = await client.GetAsync(fullUrl); + // if (response.IsSuccessStatusCode) + // { + // var imageBytes = await response.Content.ReadAsByteArrayAsync(); + // await File.WriteAllBytesAsync(filePath, imageBytes); + // } + // } + //} + //public static async Task DownloadImageAsync(string imageUrl, string baseUrl) + //{ + // // Construct the full URL for the image + // string fullUrl = new Uri(new Uri(baseUrl), imageUrl).ToString(); + // // Extract the original file name from the image URL + // string fileName = Path.GetFileName(new Uri(fullUrl).AbsolutePath); + // // Determine the local path for saving the image + // string folderPath = GetImagePath(imageUrl); + // string filePath = Path.Combine(folderPath, fileName); + + // var response = await httpClient.GetAsync(fullUrl); + // if (response.IsSuccessStatusCode) + // { + // var imageBytes = await response.Content.ReadAsByteArrayAsync(); + // await File.WriteAllBytesAsync(filePath, imageBytes); + // } + //} public static async Task DownloadImageAsync(string imageUrl, string baseUrl) { // Construct the full URL for the image @@ -708,14 +752,27 @@ public static async Task DownloadImageAsync(string imageUrl, string baseUrl) string folderPath = GetImagePath(imageUrl); string filePath = Path.Combine(folderPath, fileName); - using (var client = new HttpClient()) + // Ensure the directory exists + Directory.CreateDirectory(folderPath); + + try { - var response = await client.GetAsync(fullUrl); + var response = await httpClient.GetAsync(fullUrl); if (response.IsSuccessStatusCode) { var imageBytes = await response.Content.ReadAsByteArrayAsync(); await File.WriteAllBytesAsync(filePath, imageBytes); } + else + { + // Log or handle the unsuccessful response + SimpleLogger.Log($"Failed to download {fullUrl}. Status code: {response.StatusCode}"); + } + } + catch (Exception ex) + { + // Log or handle the exception + SimpleLogger.Log($"Exception occurred while downloading {fullUrl}: {ex.Message}"); } } diff --git a/DeFRaG_Helper/MainWindow.xaml.cs b/DeFRaG_Helper/MainWindow.xaml.cs index 755cd52..3cdbc74 100644 --- a/DeFRaG_Helper/MainWindow.xaml.cs +++ b/DeFRaG_Helper/MainWindow.xaml.cs @@ -47,7 +47,8 @@ public static MainWindow Instance { if (_instance == null) _instance = Application.Current.MainWindow as MainWindow; - MessageHelper.Log("MainWindow Instance"); + if (_instance == null) + throw new Exception("Main window not found"); return _instance; } diff --git a/DeFRaG_Helper/ViewModels/MapViewModel.cs b/DeFRaG_Helper/ViewModels/MapViewModel.cs index cef2427..b388be2 100644 --- a/DeFRaG_Helper/ViewModels/MapViewModel.cs +++ b/DeFRaG_Helper/ViewModels/MapViewModel.cs @@ -428,7 +428,10 @@ public async Task UpdateOrAddMap(Map map) private void UpdateMapProperties(Map existingMap, Map newMap) { // Update the properties of existingMap with those from newMap - existingMap.Name = newMap.Name; + if (newMap.Name != null && newMap.Name != "") + { + existingMap.Name = newMap.Name; + } existingMap.Mapname = newMap.Mapname; existingMap.Filename = newMap.Filename; existingMap.Releasedate = newMap.Releasedate; @@ -450,7 +453,7 @@ private void UpdateMapProperties(Map existingMap, Map newMap) existingMap.Items = newMap.Items; existingMap.Functions = newMap.Functions; - + // Notify UI of property changes OnPropertyChanged(nameof(Maps));