XAML-Map-Control/MapControl/UWP/ImageFileCache.UWP.cs

115 lines
3.7 KiB
C#
Raw Normal View History

2017-08-04 21:38:58 +02:00
// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
2021-01-13 21:19:27 +01:00
// © 2021 Clemens Fischer
2017-08-04 21:38:58 +02:00
// Licensed under the Microsoft Public License (Ms-PL)
using System;
using System.Diagnostics;
using System.IO;
2021-06-30 00:04:44 +02:00
using System.Runtime.InteropServices.WindowsRuntime;
using System.Text;
2017-08-04 21:38:58 +02:00
using System.Threading.Tasks;
using Windows.Storage.Streams;
namespace MapControl.Caching
{
public class ImageFileCache : IImageCache
{
2021-06-30 00:04:44 +02:00
private const string expiresTag = "EXPIRES:";
2021-06-30 12:18:21 +02:00
private readonly string rootDirectory;
2017-08-04 21:38:58 +02:00
2021-06-30 12:18:21 +02:00
public ImageFileCache(string directory)
{
2021-06-30 12:18:21 +02:00
if (string.IsNullOrEmpty(directory))
{
throw new ArgumentException("The directory argument must not be null or empty.", nameof(directory));
}
rootDirectory = directory;
Debug.WriteLine("Created ImageFileCache in " + rootDirectory);
2017-08-04 21:38:58 +02:00
}
public async Task<ImageCacheItem> GetAsync(string key)
2017-08-04 21:38:58 +02:00
{
2021-06-30 00:04:44 +02:00
ImageCacheItem imageCacheItem = null;
2021-06-30 12:18:21 +02:00
var path = GetPath(key);
2017-08-04 21:38:58 +02:00
try
{
2021-06-30 12:18:21 +02:00
if (path != null && File.Exists(path))
2017-08-04 21:38:58 +02:00
{
2021-06-30 12:18:21 +02:00
var buffer = await File.ReadAllBytesAsync(path);
2021-06-30 00:04:44 +02:00
var expiration = GetExpiration(ref buffer);
imageCacheItem = new ImageCacheItem
2017-08-04 21:38:58 +02:00
{
2021-06-30 00:04:44 +02:00
Buffer = buffer.AsBuffer(),
Expiration = expiration
};
2021-06-30 00:04:44 +02:00
2021-06-30 12:18:21 +02:00
//Debug.WriteLine("ImageFileCache: Read {0}, Expires {1}", path, expiration.ToLocalTime());
2017-08-04 21:38:58 +02:00
}
}
2021-06-30 12:18:21 +02:00
catch (Exception ex)
{
Debug.WriteLine("ImageFileCache: Failed reading {0}: {1}", path, ex.Message);
}
2017-08-04 21:38:58 +02:00
2021-06-30 00:04:44 +02:00
return imageCacheItem;
2017-08-04 21:38:58 +02:00
}
public async Task SetAsync(string key, IBuffer buffer, DateTime expiration)
2017-08-04 21:38:58 +02:00
{
2021-06-30 12:18:21 +02:00
string path;
2017-08-04 21:38:58 +02:00
2021-06-30 12:18:21 +02:00
if (buffer != null && buffer.Length > 0 && (path = GetPath(key)) != null)
{
try
2017-08-04 21:38:58 +02:00
{
2021-06-30 12:18:21 +02:00
Directory.CreateDirectory(Path.GetDirectoryName(path));
2021-06-30 12:18:21 +02:00
using (var stream = File.Create(path).AsOutputStream())
2021-06-30 00:04:44 +02:00
{
await stream.WriteAsync(buffer);
await stream.WriteAsync(Encoding.ASCII.GetBytes(expiresTag).AsBuffer());
await stream.WriteAsync(BitConverter.GetBytes(expiration.Ticks).AsBuffer());
}
2021-06-30 12:18:21 +02:00
//Debug.WriteLine("ImageFileCache: Wrote {0}, Expires {1}", path, expiration.ToLocalTime());
}
catch (Exception ex)
{
2021-06-30 12:18:21 +02:00
Debug.WriteLine("ImageFileCache: Failed writing {0}: {1}", path, ex.Message);
}
2017-08-04 21:38:58 +02:00
}
}
2021-06-30 12:18:21 +02:00
private string GetPath(string key)
{
2021-06-30 12:18:21 +02:00
try
{
return Path.Combine(rootDirectory, Path.Combine(key.Split('/', ':', ';', ',')));
}
catch (Exception ex)
{
Debug.WriteLine("ImageFileCache: Invalid key {0}/{1}: {2}", rootDirectory, key, ex.Message);
}
return null;
}
2021-06-30 00:04:44 +02:00
private static DateTime GetExpiration(ref byte[] buffer)
{
DateTime expiration = DateTime.Today;
if (buffer.Length > 16 && Encoding.ASCII.GetString(buffer, buffer.Length - 16, 8) == expiresTag)
{
expiration = new DateTime(BitConverter.ToInt64(buffer, buffer.Length - 8), DateTimeKind.Utc);
Array.Resize(ref buffer, buffer.Length - 16);
}
return expiration;
}
2017-08-04 21:38:58 +02:00
}
}