Skip to content

Commit

Permalink
changed to accept unsecure connection
Browse files Browse the repository at this point in the history
  • Loading branch information
netquick committed Jul 18, 2024
1 parent d445152 commit 7078517
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 10 deletions.
71 changes: 64 additions & 7 deletions DeFRaG_Helper/Helpers/CreateAndUpdateDB.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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))
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -698,6 +705,43 @@ public static List<string> 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
Expand All @@ -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}");
}
}

Expand Down
3 changes: 2 additions & 1 deletion DeFRaG_Helper/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
7 changes: 5 additions & 2 deletions DeFRaG_Helper/ViewModels/MapViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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));
Expand Down

0 comments on commit 7078517

Please sign in to comment.