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;
|
|
|
|
|
|
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-28 23:35:03 +02:00
|
|
|
|
private readonly string folderPath;
|
2017-08-04 21:38:58 +02:00
|
|
|
|
|
2019-07-22 19:47:57 +02:00
|
|
|
|
public ImageFileCache(StorageFolder folder)
|
2021-06-28 23:35:03 +02:00
|
|
|
|
: this(folder.Path)
|
2017-08-04 21:38:58 +02:00
|
|
|
|
{
|
2021-06-28 23:35:03 +02:00
|
|
|
|
}
|
2017-08-04 21:38:58 +02:00
|
|
|
|
|
2021-06-28 23:35:03 +02:00
|
|
|
|
public ImageFileCache(string path)
|
|
|
|
|
|
{
|
|
|
|
|
|
folderPath = path;
|
|
|
|
|
|
Debug.WriteLine("Created ImageFileCache in " + folderPath);
|
2017-08-04 21:38:58 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2019-07-16 21:28:55 +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;
|
2019-07-22 19:47:57 +02:00
|
|
|
|
string path;
|
2017-08-04 21:38:58 +02:00
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
2019-07-18 21:09:54 +02:00
|
|
|
|
path = Path.Combine(GetPathElements(key));
|
2017-08-04 21:38:58 +02:00
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
Debug.WriteLine("ImageFileCache: Invalid key {0}: {1}", key, ex.Message);
|
2021-06-30 00:04:44 +02:00
|
|
|
|
return imageCacheItem;
|
2017-08-04 21:38:58 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-06-28 23:35:03 +02:00
|
|
|
|
var folder = await StorageFolder.GetFolderFromPathAsync(folderPath);
|
2019-07-22 19:47:57 +02:00
|
|
|
|
var item = await folder.TryGetItemAsync(path);
|
|
|
|
|
|
|
|
|
|
|
|
if (item != null && item.IsOfType(StorageItemTypes.File))
|
2017-08-04 21:38:58 +02:00
|
|
|
|
{
|
2019-07-22 19:47:57 +02:00
|
|
|
|
var file = (StorageFile)item;
|
2017-08-04 21:38:58 +02:00
|
|
|
|
|
2019-07-22 19:47:57 +02:00
|
|
|
|
try
|
2017-08-04 21:38:58 +02:00
|
|
|
|
{
|
2021-06-30 00:04:44 +02:00
|
|
|
|
var buffer = (await FileIO.ReadBufferAsync(file)).ToArray();
|
|
|
|
|
|
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
|
2019-07-22 19:47:57 +02:00
|
|
|
|
};
|
2021-06-30 00:04:44 +02:00
|
|
|
|
|
|
|
|
|
|
//Debug.WriteLine("ImageFileCache: Read {0}, Expires {1}", file.Path, expiration.ToLocalTime());
|
2019-07-22 19:47:57 +02:00
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
2021-06-30 00:04:44 +02:00
|
|
|
|
Debug.WriteLine("ImageFileCache: Failed reading {0}: {1}", file.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
|
|
|
|
}
|
|
|
|
|
|
|
2019-07-16 21:28:55 +02:00
|
|
|
|
public async Task SetAsync(string key, IBuffer buffer, DateTime expiration)
|
2017-08-04 21:38:58 +02:00
|
|
|
|
{
|
2021-06-30 00:04:44 +02:00
|
|
|
|
if (buffer != null && buffer.Length > 0)
|
2017-08-04 21:38:58 +02:00
|
|
|
|
{
|
2019-07-22 19:47:57 +02:00
|
|
|
|
var folders = GetPathElements(key);
|
2017-08-04 21:38:58 +02:00
|
|
|
|
|
2019-07-22 19:47:57 +02:00
|
|
|
|
try
|
2017-08-04 21:38:58 +02:00
|
|
|
|
{
|
2021-06-28 23:35:03 +02:00
|
|
|
|
var folder = await StorageFolder.GetFolderFromPathAsync(folderPath);
|
2017-08-04 21:38:58 +02:00
|
|
|
|
|
2019-07-22 19:47:57 +02:00
|
|
|
|
for (int i = 0; i < folders.Length - 1; i++)
|
|
|
|
|
|
{
|
|
|
|
|
|
folder = await folder.CreateFolderAsync(folders[i], CreationCollisionOption.OpenIfExists);
|
|
|
|
|
|
}
|
2017-08-04 21:38:58 +02:00
|
|
|
|
|
2019-07-22 19:47:57 +02:00
|
|
|
|
var file = await folder.CreateFileAsync(folders[folders.Length - 1], CreationCollisionOption.ReplaceExisting);
|
2019-07-18 21:09:54 +02:00
|
|
|
|
|
2021-06-30 00:04:44 +02:00
|
|
|
|
//Debug.WriteLine("ImageFileCache: Write {0}, Expires {1}", file.Path, expiration.ToLocalTime());
|
2019-07-22 19:47:57 +02:00
|
|
|
|
|
2021-06-30 00:04:44 +02:00
|
|
|
|
using (var stream = await file.OpenAsync(FileAccessMode.ReadWrite))
|
|
|
|
|
|
{
|
|
|
|
|
|
await stream.WriteAsync(buffer);
|
|
|
|
|
|
await stream.WriteAsync(Encoding.ASCII.GetBytes(expiresTag).AsBuffer());
|
|
|
|
|
|
await stream.WriteAsync(BitConverter.GetBytes(expiration.Ticks).AsBuffer());
|
|
|
|
|
|
}
|
2019-07-22 19:47:57 +02:00
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
2021-06-30 00:04:44 +02:00
|
|
|
|
Debug.WriteLine("ImageFileCache: Failed writing {0}: {1}", Path.Combine(folderPath, Path.Combine(folders)), ex.Message);
|
2019-07-22 19:47:57 +02:00
|
|
|
|
}
|
2017-08-04 21:38:58 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2019-07-18 21:09:54 +02:00
|
|
|
|
|
2021-06-28 23:35:03 +02:00
|
|
|
|
private static string[] GetPathElements(string key)
|
2019-07-18 21:09:54 +02:00
|
|
|
|
{
|
|
|
|
|
|
return key.Split('\\', '/', ',', ':', ';');
|
|
|
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
|
|
}
|