mirror of
https://github.com/ClemensFischer/XAML-Map-Control.git
synced 2026-04-04 14:08:32 +00:00
Version 4.12.2 Dropped Windows.Web.Http.HtpClient
This commit is contained in:
parent
014f57ee1a
commit
9c55597a16
10 changed files with 199 additions and 280 deletions
|
|
@ -2,13 +2,7 @@
|
|||
// © 2019 Clemens Fischer
|
||||
// Licensed under the Microsoft Public License (Ms-PL)
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Net.Http;
|
||||
using System.Net.Http.Headers;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Media;
|
||||
using System.Windows.Media.Imaging;
|
||||
|
|
@ -67,72 +61,5 @@ namespace MapControl
|
|||
{
|
||||
return Task.Run(() => LoadImage(path));
|
||||
}
|
||||
|
||||
private static async Task<ImageSource> LoadImageAsync(HttpContent content)
|
||||
{
|
||||
using (var stream = new MemoryStream())
|
||||
{
|
||||
await content.CopyToAsync(stream);
|
||||
stream.Seek(0, SeekOrigin.Begin);
|
||||
|
||||
return LoadImage(stream);
|
||||
}
|
||||
}
|
||||
|
||||
internal class HttpStreamResponse
|
||||
{
|
||||
public readonly MemoryStream Stream;
|
||||
public readonly TimeSpan? MaxAge;
|
||||
|
||||
public HttpStreamResponse(MemoryStream stream, TimeSpan? maxAge)
|
||||
{
|
||||
Stream = stream;
|
||||
MaxAge = maxAge;
|
||||
}
|
||||
}
|
||||
|
||||
internal static async Task<HttpStreamResponse> LoadHttpStreamAsync(Uri uri)
|
||||
{
|
||||
HttpStreamResponse response = null;
|
||||
|
||||
try
|
||||
{
|
||||
using (var responseMessage = await HttpClient.GetAsync(uri).ConfigureAwait(false))
|
||||
{
|
||||
if (responseMessage.IsSuccessStatusCode)
|
||||
{
|
||||
MemoryStream stream = null;
|
||||
TimeSpan? maxAge = null;
|
||||
|
||||
if (ImageAvailable(responseMessage.Headers))
|
||||
{
|
||||
stream = new MemoryStream();
|
||||
await responseMessage.Content.CopyToAsync(stream).ConfigureAwait(false);
|
||||
stream.Seek(0, SeekOrigin.Begin);
|
||||
|
||||
maxAge = responseMessage.Headers.CacheControl?.MaxAge;
|
||||
}
|
||||
|
||||
response = new HttpStreamResponse(stream, maxAge);
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.WriteLine("ImageLoader: {0}: {1} {2}", uri, (int)responseMessage.StatusCode, responseMessage.ReasonPhrase);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Debug.WriteLine("ImageLoader: {0}: {1}", uri, ex.Message);
|
||||
}
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
private static bool ImageAvailable(HttpResponseHeaders responseHeaders)
|
||||
{
|
||||
IEnumerable<string> tileInfo;
|
||||
return !responseHeaders.TryGetValues("X-VE-Tile-Info", out tileInfo) || !tileInfo.Contains("no-tile");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -164,9 +164,6 @@
|
|||
<Compile Include="..\Shared\TileImageLoader.cs">
|
||||
<Link>TileImageLoader.cs</Link>
|
||||
</Compile>
|
||||
<Compile Include="..\Shared\TileQueue.cs">
|
||||
<Link>TileQueue.cs</Link>
|
||||
</Compile>
|
||||
<Compile Include="..\Shared\TileSource.cs">
|
||||
<Link>TileSource.cs</Link>
|
||||
</Compile>
|
||||
|
|
|
|||
|
|
@ -32,22 +32,23 @@ namespace MapControl
|
|||
{
|
||||
ImageSource image = null;
|
||||
DateTime expiration;
|
||||
var cacheBuffer = GetCachedImage(cacheKey, out expiration);
|
||||
byte[] cacheBuffer;
|
||||
|
||||
GetCachedImage(cacheKey, out cacheBuffer, out expiration);
|
||||
|
||||
if (cacheBuffer == null || expiration < DateTime.UtcNow)
|
||||
{
|
||||
var response = await ImageLoader.LoadHttpStreamAsync(uri).ConfigureAwait(false);
|
||||
|
||||
if (response != null) // download succeeded
|
||||
using (var stream = await ImageLoader.LoadImageStreamAsync(uri).ConfigureAwait(false))
|
||||
{
|
||||
cacheBuffer = null; // discard cached image
|
||||
|
||||
if (response.Stream != null) // tile image available
|
||||
if (stream != null) // download succeeded
|
||||
{
|
||||
using (var stream = response.Stream)
|
||||
cacheBuffer = null; // discard cached image
|
||||
|
||||
if (stream.Length > 0) // tile image available
|
||||
{
|
||||
image = ImageLoader.LoadImage(stream);
|
||||
SetCachedImage(cacheKey, stream, GetExpiration(response.MaxAge));
|
||||
|
||||
SetCachedImage(cacheKey, stream, GetExpiration(stream.MaxAge));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -79,9 +80,9 @@ namespace MapControl
|
|||
tile.Image.Dispatcher.InvokeAsync(() => tile.SetImage(image));
|
||||
}
|
||||
|
||||
private static byte[] GetCachedImage(string cacheKey, out DateTime expiration)
|
||||
private static void GetCachedImage(string cacheKey, out byte[] buffer, out DateTime expiration)
|
||||
{
|
||||
var buffer = Cache.Get(cacheKey) as byte[];
|
||||
buffer = Cache.Get(cacheKey) as byte[];
|
||||
|
||||
if (buffer != null && buffer.Length >= 16 &&
|
||||
Encoding.ASCII.GetString(buffer, buffer.Length - 16, 8) == "EXPIRES:")
|
||||
|
|
@ -92,8 +93,6 @@ namespace MapControl
|
|||
{
|
||||
expiration = DateTime.MinValue;
|
||||
}
|
||||
|
||||
return buffer;
|
||||
}
|
||||
|
||||
private static void SetCachedImage(string cacheKey, MemoryStream stream, DateTime expiration)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue