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;
|
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
using Windows.Storage;
|
|
|
|
|
|
using Windows.Storage.Streams;
|
|
|
|
|
|
|
|
|
|
|
|
namespace MapControl.Caching
|
|
|
|
|
|
{
|
|
|
|
|
|
public class ImageFileCache : IImageCache
|
|
|
|
|
|
{
|
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
|
|
|
|
{
|
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);
|
2019-07-22 19:47:57 +02:00
|
|
|
|
return null;
|
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;
|
|
|
|
|
|
//Debug.WriteLine("ImageFileCache: Reading " + file.Path);
|
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
|
|
|
|
{
|
2019-07-22 19:47:57 +02:00
|
|
|
|
return new ImageCacheItem
|
2017-08-04 21:38:58 +02:00
|
|
|
|
{
|
2019-07-22 19:47:57 +02:00
|
|
|
|
Buffer = await FileIO.ReadBufferAsync(file),
|
|
|
|
|
|
Expiration = (await file.Properties.GetImagePropertiesAsync()).DateTaken.UtcDateTime
|
|
|
|
|
|
};
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
Debug.WriteLine("ImageFileCache: Reading {0}: {1}", file.Path, ex.Message);
|
2017-08-04 21:38:58 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
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
|
|
|
|
{
|
2019-07-22 19:47:57 +02:00
|
|
|
|
if (buffer != null && buffer.Length > 0) // do not cache a no-tile entry
|
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);
|
|
|
|
|
|
//Debug.WriteLine("ImageFileCache: Writing {0}, Expires {1}", file.Path, expiration.ToLocalTime());
|
2017-08-04 21:38:58 +02:00
|
|
|
|
|
2019-07-22 19:47:57 +02:00
|
|
|
|
await FileIO.WriteBufferAsync(file, buffer);
|
2019-07-18 21:09:54 +02:00
|
|
|
|
|
2019-07-22 19:47:57 +02:00
|
|
|
|
// Store expiration date in ImageProperties.DateTaken
|
|
|
|
|
|
var properties = await file.Properties.GetImagePropertiesAsync();
|
|
|
|
|
|
properties.DateTaken = expiration;
|
|
|
|
|
|
|
|
|
|
|
|
await properties.SavePropertiesAsync();
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
2021-06-28 23:35:03 +02:00
|
|
|
|
Debug.WriteLine("ImageFileCache: 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('\\', '/', ',', ':', ';');
|
|
|
|
|
|
}
|
2017-08-04 21:38:58 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|