Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1.1.0.0 #32

Merged
merged 1 commit into from
Oct 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 46 additions & 4 deletions src/DonloaderService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
{
class DonloaderService : IHostedService
{
private readonly string UserAgentString = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:101.0) Gecko/20100101 Firefox/101.0";

private readonly ILogger<DonloaderService> logger;
private readonly DownloaderOptions options;
private readonly HttpClient client;
Expand All @@ -40,7 +42,7 @@
{
this.logger = logger;
this.client = client;
client.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:101.0) Gecko/20100101 Firefox/101.0");
client.DefaultRequestHeaders.Add("User-Agent", UserAgentString);
this.options = options;
this.hostApplicationLifetime = hostApplicationLifetime;
}
Expand Down Expand Up @@ -185,12 +187,12 @@
Console.ResetColor();
}

private async Task<(string json, IDictionary<string, ResponseItem[]> versions)> GetJson(string feedUrl, string? productVersion = null, CancellationToken cancellationToken = default)

Check warning on line 190 in src/DonloaderService.cs

View workflow job for this annotation

GitHub Actions / build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 190 in src/DonloaderService.cs

View workflow job for this annotation

GitHub Actions / build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 190 in src/DonloaderService.cs

View workflow job for this annotation

GitHub Actions / build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 190 in src/DonloaderService.cs

View workflow job for this annotation

GitHub Actions / build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.
{
var atlassianJson = await client.GetStringAsync(feedUrl, cancellationToken).ConfigureAwait(false);
var json = atlassianJson.Trim()["downloads(".Length..^1];
logger.LogTrace("Downloaded json: {0}", json);
var parsed = JsonSerializer.Deserialize<ResponseItem[]>(json, new JsonSerializerOptions

Check warning on line 195 in src/DonloaderService.cs

View workflow job for this annotation

GitHub Actions / build

Using member 'System.Text.Json.JsonSerializer.Deserialize<EpicMorg.Atlassian.Downloader.ResponseItem[]>(string, System.Text.Json.JsonSerializerOptions?)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.

Check warning on line 195 in src/DonloaderService.cs

View workflow job for this annotation

GitHub Actions / build

Using member 'System.Text.Json.JsonSerializer.Deserialize<EpicMorg.Atlassian.Downloader.ResponseItem[]>(string, System.Text.Json.JsonSerializerOptions?)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.

Check warning on line 195 in src/DonloaderService.cs

View workflow job for this annotation

GitHub Actions / build

Using member 'System.Text.Json.JsonSerializer.Deserialize<EpicMorg.Atlassian.Downloader.ResponseItem[]>(string, System.Text.Json.JsonSerializerOptions?)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.

Check warning on line 195 in src/DonloaderService.cs

View workflow job for this annotation

GitHub Actions / build

Using member 'System.Text.Json.JsonSerializer.Deserialize<EpicMorg.Atlassian.Downloader.ResponseItem[]>(string, System.Text.Json.JsonSerializerOptions?)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.
{
PropertyNameCaseInsensitive = true
});
Expand Down Expand Up @@ -326,7 +328,47 @@
}
else
{
logger.LogWarning($"File \"{outputFile}\" already exists. Download from \"{file.ZipUrl}\" skipped.");
logger.LogWarning($"File \"{outputFile}\" already exists. File sizes will be compared.");
long localFileSize = new System.IO.FileInfo(outputFile).Length;
logger.LogInformation($"Size of local file is {localFileSize} bytes.");
try
{
var httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Add("User-Agent", UserAgentString);
HttpResponseMessage response = await httpClient.SendAsync(new HttpRequestMessage(HttpMethod.Head, file.ZipUrl));
if (response.IsSuccessStatusCode)
{
if (response.Content.Headers.ContentLength.HasValue)
{
long remoteFileSize = response.Content.Headers.ContentLength.Value;
logger.LogInformation($"Size of remote file is \"{remoteFileSize}\" bytes.");

if (remoteFileSize == localFileSize) {
logger.LogInformation($"Size of remote and local files and are same ({remoteFileSize} bytes and {localFileSize} bytes). Nothing to download. Operation skipped.");
}
else
{
logger.LogWarning($"Size of remote and local files and are not same ({remoteFileSize} bytes and {localFileSize} bytes). Download started.");
File.Delete(outputFile);
await DownloadFile(file, outputFile, cancellationToken).ConfigureAwait(false);
}

}
else
{
logger.LogWarning($"Cant get size of remote file \"{file.ZipUrl}\". May be server not support it feature. Sorry.");
continue;
}
}
else
{
logger.LogCritical($"Request execution error: \"{response.StatusCode}\". Sorry.");
}
}
catch (HttpRequestException ex)
{
logger.LogCritical($"HTTP request error: \"{ex.Message}\", \"{ex.StackTrace}\", \"{ex.StatusCode}\". Sorry.");
}
}
}
}
Expand All @@ -348,14 +390,14 @@
}
catch (Exception downloadEx)
{
logger.LogError(downloadEx, $"Failed to download file {file.ZipUrl} to {outputFile}.");
logger.LogError(downloadEx, $"Failed to download file \"{file.ZipUrl}\" to \"{outputFile}\".");
try
{
File.Delete(outputFile);
}
catch (Exception removeEx)
{
logger.LogError(removeEx, $"Failed to remove incomplete file {outputFile}.");
logger.LogError(removeEx, $"Failed to remove incomplete file \"{outputFile}\".");
}
}
logger.LogInformation($"File \"{file.ZipUrl}\" successfully downloaded to \"{outputFile}\".");
Expand Down
7 changes: 4 additions & 3 deletions src/atlassian-downloader.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<PublishSingleFile>true</PublishSingleFile>
<PublishAot>false</PublishAot>
<SelfContained>true</SelfContained>
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
<PublishTrimmed>true</PublishTrimmed>
Expand All @@ -17,9 +18,9 @@
<RepositoryType>git</RepositoryType>
<RepositoryUrl>https://github.com/EpicMorg/atlassian-downloader</RepositoryUrl>
<PackageTags>atlassian, donwloader, epicmorg</PackageTags>
<AssemblyVersion>1.0.1.1</AssemblyVersion>
<FileVersion>1.0.1.1</FileVersion>
<Version>1.0.1.1</Version>
<AssemblyVersion>1.1.0.0</AssemblyVersion>
<FileVersion>1.1.0.0</FileVersion>
<Version>1.1.0.0</Version>
<Copyright>EpicMorg 2023</Copyright>
<Product>Atlassian Downloader</Product>
<IsPackable>true</IsPackable>
Expand Down
Loading