From 698b0489b142e963755a6200dd9bd8a231087e4d Mon Sep 17 00:00:00 2001 From: efaruk Date: Sun, 31 Aug 2025 15:14:48 +0300 Subject: [PATCH] async methods added --- NGeoNames/GeoFileDownloader.cs | 65 ++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/NGeoNames/GeoFileDownloader.cs b/NGeoNames/GeoFileDownloader.cs index 1f76290..76a0088 100644 --- a/NGeoNames/GeoFileDownloader.cs +++ b/NGeoNames/GeoFileDownloader.cs @@ -5,6 +5,8 @@ using System.Linq; using System.Net; using System.Net.Cache; +using System.Threading; +using System.Threading.Tasks; namespace NGeoNames { @@ -158,6 +160,17 @@ public string[] DownloadFile(string uri, string destinationpath) return DownloadFile(new Uri(uri, UriKind.RelativeOrAbsolute), destinationpath); } + /// + /// Same as DownloadFile but async + /// + /// + /// + /// + public async Task DownloadFileAsync(string uri, string destinationpath) + { + return await DownloadFileAsync(new Uri(uri, UriKind.RelativeOrAbsolute), destinationpath); + } + /// /// Downloads the specified file to the destination path using the to determine if an /// existing version is still valid. @@ -179,6 +192,17 @@ public string[] DownloadFile(Uri uri, string destinationpath) return DownloadFileWhenOlderThan(uri, destinationpath, DefaultTTL); } + /// + /// Same as DownloadFile but async + /// + /// + /// + /// + public async Task DownloadFileAsync(Uri uri, string destinationpath) + { + return await DownloadFileWhenOlderThanAsync(uri, destinationpath, DefaultTTL); + } + /// /// Downloads the specified file to the destination path using the specified TTL. /// @@ -204,6 +228,18 @@ public string[] DownloadFileWhenOlderThan(string uri, string destinationpath, Ti return DownloadFileWhenOlderThan(new Uri(uri, UriKind.RelativeOrAbsolute), destinationpath, ttl); } + /// + /// Same as DownloadFileWhenOlderThan but async + /// + /// + /// + /// + /// + public async Task DownloadFileWhenOlderThanAsync(string uri, string destinationpath, TimeSpan ttl) + { + return await DownloadFileWhenOlderThanAsync(new Uri(uri, UriKind.RelativeOrAbsolute), destinationpath, ttl); + } + /// /// Downloads the specified file to the destination path using the specified TTL. /// @@ -246,6 +282,35 @@ public string[] DownloadFileWhenOlderThan(Uri uri, string destinationpath, TimeS return new[] { destinationpath }; } + /// + /// Same as DownloadFileWhenOlderThan but async + /// + /// + /// + /// + /// + public async Task DownloadFileWhenOlderThanAsync(Uri uri, string destinationpath, TimeSpan ttl) + { + var downloaduri = DetermineDownloadPath(uri); + destinationpath = DetermineDestinationPath(downloaduri, destinationpath); + + if (IsFileExpired(destinationpath, ttl)) + { + using (var w = new WebClient()) + { + w.CachePolicy = CachePolicy; + w.Credentials = Credentials; + w.Proxy = Proxy; + w.Headers.Add(HttpRequestHeader.UserAgent, USERAGENT); + await w.DownloadFileTaskAsync(downloaduri, destinationpath); + } + } + + if (Path.GetExtension(destinationpath).Equals(".zip", StringComparison.OrdinalIgnoreCase)) + return UnzipFiles(destinationpath, ttl); + return new[] { destinationpath }; + } + /// /// Determines if a file is 'expired' by it's TTL. ///