| | 1 | | using System; |
| | 2 | | using System.IO; |
| | 3 | | using System.IO.Compression; |
| | 4 | | using System.Net; |
| | 5 | | using System.Net.Http; |
| | 6 | | using System.Net.Http.Headers; |
| | 7 | | using System.Web; |
| | 8 | | using Itinero.Logging; |
| | 9 | |
|
| | 10 | | namespace Itinero.IO.Osm.Tiles.Download; |
| | 11 | |
|
| | 12 | | internal static class DownloadHelper |
| | 13 | | { |
| | 14 | | /// <summary> |
| | 15 | | /// Gets a stream for the content at the given url. |
| | 16 | | /// </summary> |
| | 17 | | /// <param name="url">The url.</param> |
| | 18 | | /// <returns>An open stream for the content at the given url.</returns> |
| | 19 | | public static Stream? Download(string url) |
| 0 | 20 | | { |
| | 21 | | try |
| 0 | 22 | | { |
| 0 | 23 | | var client = new HttpClient(); |
| 0 | 24 | | client.DefaultRequestHeaders.AcceptEncoding.Add(new StringWithQualityHeaderValue("gzip")); |
| 0 | 25 | | var response = client.GetAsync(url); |
| 0 | 26 | | if (response.Result.StatusCode == HttpStatusCode.NotFound) |
| 0 | 27 | | { |
| 0 | 28 | | return null; |
| | 29 | | } |
| | 30 | |
|
| 0 | 31 | | var stream = response.GetAwaiter().GetResult().Content.ReadAsStreamAsync().GetAwaiter() |
| 0 | 32 | | .GetResult(); |
| 0 | 33 | | Logger.Log(nameof(DownloadHelper), TraceEventType.Verbose, |
| 0 | 34 | | $"Downloaded from {url}."); |
| 0 | 35 | | return new GZipStream(stream, CompressionMode.Decompress); |
| | 36 | | } |
| 0 | 37 | | catch (Exception ex) |
| 0 | 38 | | { |
| 0 | 39 | | Logger.Log(nameof(DownloadHelper), TraceEventType.Warning, |
| 0 | 40 | | $"Failed to download from {url}: {ex}."); |
| 0 | 41 | | return null; |
| | 42 | | } |
| 0 | 43 | | } |
| | 44 | | } |