Skip to content
Open
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
65 changes: 65 additions & 0 deletions NGeoNames/GeoFileDownloader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
using System.Linq;
using System.Net;
using System.Net.Cache;
using System.Threading;
using System.Threading.Tasks;

namespace NGeoNames
{
Expand Down Expand Up @@ -158,6 +160,17 @@ public string[] DownloadFile(string uri, string destinationpath)
return DownloadFile(new Uri(uri, UriKind.RelativeOrAbsolute), destinationpath);
}

/// <summary>
/// Same as DownloadFile but async
/// </summary>
/// <param name="uri"></param>
/// <param name="destinationpath"></param>
/// <returns></returns>
public async Task<string[]> DownloadFileAsync(string uri, string destinationpath)
{
return await DownloadFileAsync(new Uri(uri, UriKind.RelativeOrAbsolute), destinationpath);
}

/// <summary>
/// Downloads the specified file to the destination path using the <see cref="DefaultTTL"/> to determine if an
/// existing version is still valid.
Expand All @@ -179,6 +192,17 @@ public string[] DownloadFile(Uri uri, string destinationpath)
return DownloadFileWhenOlderThan(uri, destinationpath, DefaultTTL);
}

/// <summary>
/// Same as DownloadFile but async
/// </summary>
/// <param name="uri"></param>
/// <param name="destinationpath"></param>
/// <returns></returns>
public async Task<string[]> DownloadFileAsync(Uri uri, string destinationpath)
{
return await DownloadFileWhenOlderThanAsync(uri, destinationpath, DefaultTTL);
}

/// <summary>
/// Downloads the specified file to the destination path using the specified TTL.
/// </summary>
Expand All @@ -204,6 +228,18 @@ public string[] DownloadFileWhenOlderThan(string uri, string destinationpath, Ti
return DownloadFileWhenOlderThan(new Uri(uri, UriKind.RelativeOrAbsolute), destinationpath, ttl);
}

/// <summary>
/// Same as DownloadFileWhenOlderThan but async
/// </summary>
/// <param name="uri"></param>
/// <param name="destinationpath"></param>
/// <param name="ttl"></param>
/// <returns></returns>
public async Task<string[]> DownloadFileWhenOlderThanAsync(string uri, string destinationpath, TimeSpan ttl)
{
return await DownloadFileWhenOlderThanAsync(new Uri(uri, UriKind.RelativeOrAbsolute), destinationpath, ttl);
}

/// <summary>
/// Downloads the specified file to the destination path using the specified TTL.
/// </summary>
Expand Down Expand Up @@ -246,6 +282,35 @@ public string[] DownloadFileWhenOlderThan(Uri uri, string destinationpath, TimeS
return new[] { destinationpath };
}

/// <summary>
/// Same as DownloadFileWhenOlderThan but async
/// </summary>
/// <param name="uri"></param>
/// <param name="destinationpath"></param>
/// <param name="ttl"></param>
/// <returns></returns>
public async Task<string[]> 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 };
}

/// <summary>
/// Determines if a file is 'expired' by it's TTL.
/// </summary>
Expand Down