diff --git a/README.md b/README.md index df18427..c742f7b 100644 --- a/README.md +++ b/README.md @@ -59,6 +59,8 @@ Options: --action Action to perform [default: Download] --about Show credits banner [default: False] --product-version Override target version to download some product. Advice: Use it with "customFeed". [] + --skip-file-check Skip compare of file sizes if a local file already exists. + Existing file will be skipped to check and redownload. [default: False] --version Show version information -?, -h, --help Show help and usage information ``` diff --git a/src/Core/DownloaderService.cs b/src/Core/DownloaderService.cs index 9e9c10b..b25a863 100644 --- a/src/Core/DownloaderService.cs +++ b/src/Core/DownloaderService.cs @@ -113,6 +113,7 @@ internal class DownloaderService : IHostedService private async Task DownloadFilesFromFeed(string feedUrl, IDictionary versions, CancellationToken cancellationToken) { + var feedDir = Path.Combine(this.options.OutputDir, feedUrl[(feedUrl.LastIndexOf('/') + 1)..feedUrl.LastIndexOf('.')]); this.logger.LogInformation("Download from JSON \"{feedUrl}\" started", feedUrl); foreach (var version in versions) @@ -141,6 +142,8 @@ internal class DownloaderService : IHostedService continue; } + + var serverPath = file.ZipUrl.PathAndQuery; var outputFile = Path.Combine(directory, serverPath[(serverPath.LastIndexOf('/') + 1)..]); if (!File.Exists(outputFile)) @@ -149,46 +152,54 @@ internal class DownloaderService : IHostedService } else { - this.logger.LogWarning($"File \"{outputFile}\" already exists. File sizes will be compared."); - var localFileSize = new FileInfo(outputFile).Length; - this.logger.LogInformation($"Size of local file is {localFileSize} bytes."); - try + if (this.options.SkipFileCheck == false) { - var httpClient = new HttpClient(); - httpClient.DefaultRequestHeaders.Add("User-Agent", this.UserAgentString); - var response = await httpClient.SendAsync(new HttpRequestMessage(HttpMethod.Head, file.ZipUrl)); - if (response.IsSuccessStatusCode) + this.logger.LogWarning($"File \"{outputFile}\" already exists. File sizes will be compared."); + var localFileSize = new FileInfo(outputFile).Length; + this.logger.LogInformation($"Size of local file is {localFileSize} bytes."); + try { - if (response.Content.Headers.ContentLength.HasValue) + var httpClient = new HttpClient(); + httpClient.DefaultRequestHeaders.Add("User-Agent", this.UserAgentString); + var response = await httpClient.SendAsync(new HttpRequestMessage(HttpMethod.Head, file.ZipUrl)); + if (response.IsSuccessStatusCode) { - var remoteFileSize = response.Content.Headers.ContentLength.Value; - this.logger.LogInformation($"Size of remote file is \"{remoteFileSize}\" bytes."); - - if (remoteFileSize == localFileSize) + if (response.Content.Headers.ContentLength.HasValue) { - this.logger.LogInformation($"Size of remote and local files and are same ({remoteFileSize} bytes and {localFileSize} bytes). Nothing to download. Operation skipped."); + var remoteFileSize = response.Content.Headers.ContentLength.Value; + this.logger.LogInformation($"Size of remote file is \"{remoteFileSize}\" bytes."); + + if (remoteFileSize == localFileSize) + { + this.logger.LogInformation($"Size of remote and local files and are same ({remoteFileSize} bytes and {localFileSize} bytes). Nothing to download. Operation skipped."); + } + else + { + this.logger.LogWarning($"Size of remote and local files and are not same ({remoteFileSize} bytes and {localFileSize} bytes). Download started."); + File.Delete(outputFile); + await this.DownloadFile(file, outputFile, cancellationToken).ConfigureAwait(false); + } } else { - this.logger.LogWarning($"Size of remote and local files and are not same ({remoteFileSize} bytes and {localFileSize} bytes). Download started."); - File.Delete(outputFile); - await this.DownloadFile(file, outputFile, cancellationToken).ConfigureAwait(false); + this.logger.LogWarning($"Cant get size of remote file \"{file.ZipUrl}\". May be server not support it feature. Sorry."); + continue; } } else { - this.logger.LogWarning($"Cant get size of remote file \"{file.ZipUrl}\". May be server not support it feature. Sorry."); - continue; + this.logger.LogCritical($"Request execution error: \"{response.StatusCode}\". Sorry."); } } - else + catch (HttpRequestException ex) { - this.logger.LogCritical($"Request execution error: \"{response.StatusCode}\". Sorry."); + this.logger.LogCritical($"HTTP request error: \"{ex.Message}\", \"{ex.StackTrace}\", \"{ex.StatusCode}\". Sorry."); } - } - catch (HttpRequestException ex) + } + else { - this.logger.LogCritical($"HTTP request error: \"{ex.Message}\", \"{ex.StackTrace}\", \"{ex.StatusCode}\". Sorry."); + logger.LogWarning($"File \"{outputFile}\" already exists. Download from \"{file.ZipUrl}\" skipped."); + continue; } } } diff --git a/src/Models/DownloaderOptions.cs b/src/Models/DownloaderOptions.cs index 1b42621..b606feb 100644 --- a/src/Models/DownloaderOptions.cs +++ b/src/Models/DownloaderOptions.cs @@ -1,4 +1,4 @@ namespace EpicMorg.Atlassian.Downloader; using System; -public record DownloaderOptions(string OutputDir, Uri[] CustomFeed, DownloadAction Action,bool Version, string ProductVersion) { } \ No newline at end of file +public record DownloaderOptions(string OutputDir, Uri[] CustomFeed, DownloadAction Action, bool Version, string ProductVersion, bool SkipFileCheck) { } \ No newline at end of file diff --git a/src/Program.cs b/src/Program.cs index 9563ecb..10c4f23 100644 --- a/src/Program.cs +++ b/src/Program.cs @@ -22,7 +22,8 @@ public class Program /// Override URIs to import /// Show credits banner /// Override target version to download some product. Advice: Use it with "customFeed". - static async Task Main(string outputDir, Uri[] customFeed = null, DownloadAction action = DownloadAction.Download, bool about = false, string productVersion = null) => await + /// Skip compare of file sizes if a local file already exists. Existing file will be skipped to check and redownload. + static async Task Main(string outputDir, Uri[] customFeed = null, DownloadAction action = DownloadAction.Download, bool about = false, string productVersion = null, bool skipFileCheck = false) => await Host .CreateDefaultBuilder() .ConfigureHostConfiguration(configHost => configHost.AddEnvironmentVariables()) @@ -44,7 +45,7 @@ public class Program .AddSerilog(dispose: true); }) .AddHostedService() - .AddSingleton(new DownloaderOptions(outputDir, customFeed, action, about, productVersion)) + .AddSingleton(new DownloaderOptions(outputDir, customFeed, action, about, productVersion, skipFileCheck)) .AddHttpClient()) .RunConsoleAsync() .ConfigureAwait(false); diff --git a/src/Properties/launchSettings.json b/src/Properties/launchSettings.json index 874b865..fd271c9 100644 --- a/src/Properties/launchSettings.json +++ b/src/Properties/launchSettings.json @@ -2,8 +2,9 @@ "profiles": { "atlassian-downloader": { "commandName": "Project", - //"commandLineArgs": "--help" - "commandLineArgs": "--action=ShowRawJson --output-dir=F:\\temp\\atlassian\\test1" + //"commandLineArgs": "--version" + "commandLineArgs": "--help" + // "commandLineArgs": "--action=ShowRawJson --output-dir=F:\\temp\\atlassian\\test1" } } } \ No newline at end of file