Skip to content

Commit

Permalink
.Net Core 3.1 removal, dependency updates, move from WebClient to Htt…
Browse files Browse the repository at this point in the history
…pClient (#100)

- Removal of .Net Core 3.1 as a target
- Update of some dependencies
- Update of the obsolete WebClient to HttpClient
  • Loading branch information
Niphyr authored Sep 22, 2023
2 parents 1a67695 + 068a9b8 commit 771bbeb
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 34 deletions.
1 change: 0 additions & 1 deletion .github/workflows/dotnet-core-build-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ jobs:
uses: actions/setup-dotnet@v2
with:
dotnet-version: |
3.1.x
6.0.x
- name: Install dependencies
run: dotnet restore
Expand Down
1 change: 0 additions & 1 deletion .github/workflows/publish-nuget-package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ jobs:
uses: actions/setup-dotnet@v2
with:
dotnet-version: |
3.1.x
6.0.x
# Build
- name: Install dependencies
Expand Down
57 changes: 30 additions & 27 deletions SimcProfileParser/DataSync/CacheService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -227,16 +227,15 @@ async Task<T> ICacheService.GetParsedFileContentsAsync<T>(SimcParsedFileType fil
}
}

var configuration = _registeredFiles.Where(f => f.ParsedFileType == fileType).FirstOrDefault();
var localPath = new Uri(Path.Combine(BaseFileDirectory, configuration.LocalParsedFile)).LocalPath;
var configuration = _registeredFiles.Where(f => f.ParsedFileType == fileType).FirstOrDefault()
?? throw new ArgumentOutOfRangeException(nameof(fileType), "Supplied fileType has not been registered.");

if (configuration == null)
throw new ArgumentOutOfRangeException("Supplied fileType has not been registered.");
var localPath = new Uri(Path.Combine(BaseFileDirectory, configuration.LocalParsedFile)).LocalPath;

// If the file doesn't exist, generate it.
if (!File.Exists(localPath))
{
_logger?.LogTrace($"File [{localPath}] does not exist, generating it...");
_logger?.LogTrace("File [{localPath}] does not exist, generating it...", localPath);
await ((ICacheService)this).GenerateParsedFileAsync(fileType);
}

Expand All @@ -255,10 +254,8 @@ async Task<T> ICacheService.GetParsedFileContentsAsync<T>(SimcParsedFileType fil
/// <param name="fileType">Type of file to generate data for</param>
async Task ICacheService.GenerateParsedFileAsync(SimcParsedFileType fileType)
{
var configuration = _registeredFiles.Where(f => f.ParsedFileType == fileType).FirstOrDefault();

if (configuration == null)
throw new ArgumentOutOfRangeException("Supplied fileType has not been registered.");
var configuration = _registeredFiles.Where(f => f.ParsedFileType == fileType).FirstOrDefault()
?? throw new ArgumentOutOfRangeException(nameof(fileType), "Supplied fileType has not been registered.");

// Gather together all the raw data the extraction service needs to run its process
var rawData = new Dictionary<string, string>();
Expand All @@ -272,7 +269,7 @@ async Task ICacheService.GenerateParsedFileAsync(SimcParsedFileType fileType)
var parsedData = _rawDataExtractionService.GenerateData(configuration.ParsedFileType, rawData);
var localPath = Path.Combine(BaseFileDirectory, configuration.LocalParsedFile);

_logger?.LogTrace($"Saving parsed json data for [{configuration.ParsedFileType}] to [{localPath}]");
_logger?.LogTrace("Saving parsed json data for [{configuration.ParsedFileType}] to [{localPath}]", configuration.ParsedFileType, localPath);
await File.WriteAllTextAsync(localPath, JsonConvert.SerializeObject(parsedData));
}

Expand All @@ -297,25 +294,25 @@ internal async Task<string> GetRawFileContentsAsync(CacheFileConfiguration confi
{
var destinationRawFile = configuration.RawFiles.Where(r => r.Key == localRawFile).FirstOrDefault();

_logger?.LogTrace($"Path does not exist: [{localPath}] - attempting to download file from [{destinationRawFile}].");
_logger?.LogTrace("Path does not exist: [{localPath}] - attempting to download file from [{destinationRawFile}].", localPath, destinationRawFile);

var downloaded = await DownloadFileIfChangedAsync(new Uri(destinationRawFile.Value),
new Uri(Path.Combine(BaseFileDirectory, destinationRawFile.Key)));

if (!downloaded)
{
_logger?.LogError($"Unable to download [{destinationRawFile}] to [{localPath}]");
_logger?.LogError("Unable to download [{destinationRawFile}] to [{localPath}]", destinationRawFile, localPath);
if(Directory.Exists(BaseFileDirectory))
{
_logger?.LogTrace($"Listing directory contents for [{BaseFileDirectory}]");
_logger?.LogTrace("Listing directory contents for [{BaseFileDirectory}]", BaseFileDirectory);
foreach(var file in Directory.GetFiles(BaseFileDirectory))
{
_logger?.LogTrace($"File: {file}");
_logger?.LogTrace("File: {file}", file);
}
}
else
{
_logger?.LogError($"Directory does not exist: [{BaseFileDirectory}]");
_logger?.LogError("Directory does not exist: [{BaseFileDirectory}]", BaseFileDirectory);
}
}
}
Expand All @@ -333,10 +330,10 @@ internal async Task<string> GetRawFileContentsAsync(CacheFileConfiguration confi
/// <returns></returns>
internal async Task<bool> DownloadFileIfChangedAsync(Uri sourceUri, Uri destinationUri)
{
HttpClient httpClient = new HttpClient();
using HttpClient httpClient = new();

HttpRequestMessage request =
new HttpRequestMessage(HttpMethod.Head,
new(HttpMethod.Head,
sourceUri);

HttpResponseMessage response;
Expand All @@ -347,7 +344,7 @@ internal async Task<bool> DownloadFileIfChangedAsync(Uri sourceUri, Uri destinat
}
catch (Exception ex)
{
_logger?.LogError(ex, $"Error downloading {sourceUri} to {destinationUri}");
_logger?.LogError(ex, "Error downloading {sourceUri} to {destinationUri}", sourceUri, destinationUri);
return false;
}

Expand All @@ -358,9 +355,9 @@ internal async Task<bool> DownloadFileIfChangedAsync(Uri sourceUri, Uri destinat
.FirstOrDefault();

if (eTag != null)
_logger?.LogTrace($"etag for this file is {eTag}");
_logger?.LogTrace("etag for this file is {eTag}", eTag);
else
_logger?.LogTrace($"No etag found for {destinationUri.LocalPath}");
_logger?.LogTrace("No etag found for {destinationUri.LocalPath}", destinationUri.LocalPath);

DateTime lastModified = DateTime.UtcNow;

Expand All @@ -370,41 +367,47 @@ internal async Task<bool> DownloadFileIfChangedAsync(Uri sourceUri, Uri destinat
// Check if we need to download it or not.
if (eTag != null && // If there is an etag
response.Headers.ETag.Tag == eTag.ETag && // and they match
eTag.LastModified == lastModified) // and the last modified's match
eTag.LastModified == lastModified) // and the last modified match
return true; // Then we don't need to download it.

var downloadResponse = await DownloadFileAsync(sourceUri, destinationUri);

// If the download was successful, save the etag.
if (downloadResponse)
{
_logger?.LogTrace($"Successfully downloaded {sourceUri} to {destinationUri.LocalPath}");
_logger?.LogTrace("Successfully downloaded {sourceUri} to {destinationUri.LocalPath}", sourceUri, destinationUri.LocalPath);
lastModified = File.GetLastWriteTimeUtc(destinationUri.LocalPath);
await UpdateCacheDataAsync(destinationUri.LocalPath, response.Headers.ETag.Tag, lastModified);
}
else
{
_logger?.LogError($"Failure downloading file.");
_logger?.LogError("Failure downloading file.");
}

return downloadResponse;
}

internal async Task<bool> DownloadFileAsync(Uri sourceUri, Uri destinationUri)
{
WebClient client = new WebClient();
using HttpClient client = new();

try
{
var baseDirectory = new Uri(destinationUri, ".");
if (!Directory.Exists(baseDirectory.OriginalString))
Directory.CreateDirectory(baseDirectory.OriginalString);

await client.DownloadFileTaskAsync(sourceUri, destinationUri.LocalPath);
using var s = await client.GetStreamAsync(sourceUri);

if (File.Exists(destinationUri.LocalPath))
File.Delete(destinationUri.LocalPath);

using var fs = new FileStream(destinationUri.LocalPath, FileMode.CreateNew);
await s.CopyToAsync(fs);
}
catch (Exception ex)
{
_logger?.LogError(ex, $"Unable to DownloadFileAsync [{sourceUri}] to [{destinationUri}]");
_logger?.LogError(ex, "Unable to DownloadFileAsync [{sourceUri}] to [{destinationUri}]", sourceUri, destinationUri);
return false;
}
return true;
Expand All @@ -413,7 +416,7 @@ internal async Task<bool> DownloadFileAsync(Uri sourceUri, Uri destinationUri)
#region eTag Cache

private readonly string _etagCacheDataFile = "FileDownloadCache.json";
protected List<FileETag> _eTagCacheData = new List<FileETag>();
protected List<FileETag> _eTagCacheData = new();

/// <summary>
/// Update the cache with an entry
Expand Down
10 changes: 5 additions & 5 deletions SimcProfileParser/SimcProfileParser.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<TargetFrameworks>net6.0</TargetFrameworks>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Version>1.4.1</Version>
<Version>1.5.0</Version>
<Authors>Mechanical Priest</Authors>
<Company>Mechanical Priest</Company>
<PackageLicenseExpression>GPL-3.0-only</PackageLicenseExpression>
Expand All @@ -18,8 +18,8 @@
<Title>Simc Profile Parser</Title>
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
<PackageIcon>packageIcon.png</PackageIcon>
<AssemblyVersion>1.4.1</AssemblyVersion>
<FileVersion>1.4.1</FileVersion>
<AssemblyVersion>1.5.0</AssemblyVersion>
<FileVersion>1.5.0</FileVersion>
</PropertyGroup>

<ItemGroup>
Expand All @@ -31,7 +31,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="6.0.1" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="6.0.4" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup>
</Project>

0 comments on commit 771bbeb

Please sign in to comment.