This commit is contained in:
STAM 2024-08-05 02:43:59 +03:00
parent b4e881a1d7
commit 6063467e2d
No known key found for this signature in database
GPG Key ID: 711526C6938897F1
5 changed files with 58 additions and 33 deletions

View File

@ -1,6 +1,9 @@
# Atlassian Downloader - Changelog
## 2.x
* `2.0.0.2` - - minor update:
* Added `maxRetries (default: 5)` and `delayBetweenRetries (default: 2500, milliseconds)` args, to redownload file if connection will be reset.
* Updated dependencies.
* `2.0.0.1` - - minor update:
* Fix default output dir, enable nullables, fix compiler warnings #43
* Remove redundant parameters from publish profiles #42

View File

@ -213,31 +213,43 @@ internal class DownloaderService : IHostedService
private async Task DownloadFile(ResponseItem file, string outputFile, CancellationToken cancellationToken)
{
if (!string.IsNullOrEmpty(file.Md5))
for (int attempt = 1; attempt <= options.MaxRetries; attempt++)
{
File.WriteAllText(outputFile + ".md5", file.Md5);
}
if (!string.IsNullOrEmpty(file.Md5))
{
File.WriteAllText(outputFile + ".md5", file.Md5);
}
try
{
using var outputStream = File.OpenWrite(outputFile);
using var request = await this.client.GetStreamAsync(file.ZipUrl, cancellationToken).ConfigureAwait(false);
await request.CopyToAsync(outputStream, cancellationToken).ConfigureAwait(false);
}
catch (Exception downloadEx)
{
this.logger.LogError(downloadEx, "Failed to download file \"{uri}\" to \"{outputFile}\".", file.ZipUrl, outputFile);
try
{
File.Delete(outputFile);
using var outputStream = File.OpenWrite(outputFile);
using var request = await this.client.GetStreamAsync(file.ZipUrl, cancellationToken).ConfigureAwait(false);
await request.CopyToAsync(outputStream, cancellationToken).ConfigureAwait(false);
}
catch (Exception removeEx)
catch (Exception downloadEx)
{
this.logger.LogError(removeEx, "Failed to remove incomplete file \"{outputFile}\".", outputFile);
}
}
this.logger.LogError(downloadEx, "Attempt {attempt} failed to download file \"{uri}\" to \"{outputFile}\".", attempt, file.ZipUrl, outputFile);
this.logger.LogInformation("File \"{uri}\" successfully downloaded to \"{outputFile}\".", file.ZipUrl, outputFile);
if (attempt == options.MaxRetries)
{
this.logger.LogError(downloadEx, "Failed to download file \"{uri}\" to \"{outputFile}\".", file.ZipUrl, outputFile);
try
{
File.Delete(outputFile);
}
catch (Exception removeEx)
{
this.logger.LogError(removeEx, "Failed to remove incomplete file \"{outputFile}\".", outputFile);
}
throw;
}
else
{
await Task.Delay(options.DelayBetweenRetries, cancellationToken).ConfigureAwait(false);
}
}
this.logger.LogInformation("File \"{uri}\" successfully downloaded to \"{outputFile}\".", file.ZipUrl, outputFile);
}
}
#pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously

View File

@ -8,4 +8,7 @@ public record DownloaderOptions(
bool Version,
string? ProductVersion,
bool SkipFileCheck,
string UserAgent) { }
string UserAgent,
int MaxRetries,
int DelayBetweenRetries
) { }

View File

@ -24,6 +24,8 @@ public class Program
/// <param name="productVersion">Override target version to download some product. Advice: Use it with "customFeed".</param>
/// <param name="skipFileCheck">Skip compare of file sizes if a local file already exists. Existing file will be skipped to check and redownload.</param>
/// <param name="userAgent">Set custom user agent via this feature flag.</param>
/// <param name="maxRetries">Set custom count of download retries.</param>
/// <param name="delayBetweenRetries">Set custom delay between retries (in milliseconds).</param>
static async Task Main(
string? outputDir = default,
Uri[]? customFeed = null,
@ -31,6 +33,8 @@ public class Program
bool about = false,
string? productVersion = null,
bool skipFileCheck = false,
int maxRetries = 5,
int delayBetweenRetries = 2500,
string userAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:101.0) Gecko/20100101 Firefox/101.0") => await
Host
.CreateDefaultBuilder()
@ -60,7 +64,9 @@ public class Program
about,
productVersion,
skipFileCheck,
userAgent))
userAgent,
maxRetries,
delayBetweenRetries))
.AddHttpClient())
.RunConsoleAsync()
.ConfigureAwait(false);

View File

@ -12,6 +12,7 @@
<!--build props-->
<Nullable>enable</Nullable>
<OutputType>Exe</OutputType>
<GenerateAssemblyInfo>true</GenerateAssemblyInfo>
<TargetFramework>net8.0</TargetFramework>
<IsPackable>false</IsPackable>
<ApplicationIcon>favicon.ico</ApplicationIcon>
@ -26,25 +27,25 @@
<RepositoryType>git</RepositoryType>
<RepositoryUrl>https://github.com/EpicMorg/atlassian-downloader</RepositoryUrl>
<PackageTags>atlassian, donwloader, epicmorg</PackageTags>
<AssemblyVersion>2.0.0.0</AssemblyVersion>
<FileVersion>2.0.0.0</FileVersion>
<Version>2.0.0.0</Version>
<Copyright>EpicMorg 2023</Copyright>
<AssemblyVersion>2.0.0.2</AssemblyVersion>
<FileVersion>2.0.0.2</FileVersion>
<Version>2.0.0.2</Version>
<Copyright>EpicMorg 2024</Copyright>
<Product>Atlassian Downloader</Product>
<Company>EpicMorg</Company>
<PackageReadmeFile>README.md</PackageReadmeFile>
<RootNamespace>EpicMorg.Atlassian.Downloader</RootNamespace>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0-rc.2.23479.6" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0-rc.2.23479.6" />
<PackageReference Include="Microsoft.Extensions.Http" Version="8.0.0-rc.2.23479.6" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.0-rc.2.23479.6" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="8.0.0-rc.2.23479.6" />
<PackageReference Include="Serilog.Extensions.Logging" Version="7.0.1-dev-10354" />
<PackageReference Include="Serilog.Settings.Configuration" Version="7.0.2-dev-00546" />
<PackageReference Include="Serilog.Sinks.Console" Version="4.2.0-dev-00918" />
<PackageReference Include="Serilog" Version="3.1.0-dev-02078" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Http" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="8.0.0" />
<PackageReference Include="Serilog.Extensions.Logging" Version="8.0.0" />
<PackageReference Include="Serilog.Settings.Configuration" Version="8.0.2" />
<PackageReference Include="Serilog.Sinks.Console" Version="6.0.0" />
<PackageReference Include="Serilog" Version="4.0.1" />
<PackageReference Include="System.CommandLine.DragonFruit" Version="0.4.0-alpha.22272.1" />
<None Include="..\README.md">
<Pack>True</Pack>