2014-11-19 21:11:14 +01:00
|
|
|
|
// XAML Map Control - http://xamlmapcontrol.codeplex.com/
|
2015-01-20 17:52:02 +01:00
|
|
|
|
// © 2015 Clemens Fischer
|
2014-11-19 21:11:14 +01:00
|
|
|
|
// Licensed under the Microsoft Public License (Ms-PL)
|
|
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
using Windows.Storage;
|
|
|
|
|
|
using Windows.Storage.Streams;
|
|
|
|
|
|
|
|
|
|
|
|
namespace MapControl.Caching
|
|
|
|
|
|
{
|
|
|
|
|
|
public class ImageFileCache : IImageCache
|
|
|
|
|
|
{
|
|
|
|
|
|
private readonly string name;
|
|
|
|
|
|
private StorageFolder rootFolder;
|
|
|
|
|
|
|
|
|
|
|
|
public ImageFileCache(string name = null, StorageFolder folder = null)
|
|
|
|
|
|
{
|
2015-03-15 10:11:19 +01:00
|
|
|
|
if (string.IsNullOrEmpty(name))
|
2014-11-19 21:11:14 +01:00
|
|
|
|
{
|
|
|
|
|
|
name = TileImageLoader.DefaultCacheName;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (folder == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
folder = TileImageLoader.DefaultCacheFolder;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
this.name = name;
|
|
|
|
|
|
|
|
|
|
|
|
folder.CreateFolderAsync(name, CreationCollisionOption.OpenIfExists).Completed = (o, s) =>
|
|
|
|
|
|
{
|
|
|
|
|
|
rootFolder = o.GetResults();
|
2015-12-03 20:04:10 +01:00
|
|
|
|
Debug.WriteLine("Created ImageFileCache in " + rootFolder.Path);
|
2014-11-19 21:11:14 +01:00
|
|
|
|
};
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public virtual async Task<ImageCacheItem> GetAsync(string key)
|
|
|
|
|
|
{
|
|
|
|
|
|
var item = await rootFolder.TryGetItemAsync(key);
|
|
|
|
|
|
|
2015-11-28 21:09:25 +01:00
|
|
|
|
if (item != null && item.IsOfType(StorageItemTypes.File))
|
2014-11-19 21:11:14 +01:00
|
|
|
|
{
|
2015-11-28 21:09:25 +01:00
|
|
|
|
var file = (StorageFile)item;
|
2015-12-03 20:04:10 +01:00
|
|
|
|
//Debug.WriteLine("ImageFileCache: Reading " + file.Path);
|
2014-11-19 21:11:14 +01:00
|
|
|
|
|
2015-11-28 21:09:25 +01:00
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
return new ImageCacheItem
|
|
|
|
|
|
{
|
|
|
|
|
|
Buffer = await FileIO.ReadBufferAsync(file),
|
2015-12-03 20:04:10 +01:00
|
|
|
|
Expiration = (await file.Properties.GetImagePropertiesAsync()).DateTaken.UtcDateTime
|
2015-11-28 21:09:25 +01:00
|
|
|
|
};
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
2015-12-03 20:04:10 +01:00
|
|
|
|
Debug.WriteLine("ImageFileCache: Reading {0}: {1}", file.Path, ex.Message);
|
2015-11-28 21:09:25 +01:00
|
|
|
|
}
|
2014-11-19 21:11:14 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2015-11-28 21:09:25 +01:00
|
|
|
|
return null;
|
2014-11-19 21:11:14 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2015-12-03 20:04:10 +01:00
|
|
|
|
public virtual async Task SetAsync(string key, IBuffer buffer, DateTime expiration)
|
2014-11-19 21:11:14 +01:00
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
var names = key.Split('\\');
|
|
|
|
|
|
var folder = rootFolder;
|
|
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < names.Length - 1; i++)
|
|
|
|
|
|
{
|
|
|
|
|
|
folder = await folder.CreateFolderAsync(names[i], CreationCollisionOption.OpenIfExists);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var file = await folder.CreateFileAsync(names[names.Length - 1], CreationCollisionOption.ReplaceExisting);
|
2015-12-03 20:04:10 +01:00
|
|
|
|
//Debug.WriteLine("ImageFileCache: Writing {0}, Expires {1}", file.Path, expiration.ToLocalTime());
|
2015-11-28 21:09:25 +01:00
|
|
|
|
|
2014-11-19 21:11:14 +01:00
|
|
|
|
await FileIO.WriteBufferAsync(file, buffer);
|
|
|
|
|
|
|
2015-11-28 21:09:25 +01:00
|
|
|
|
// Store expiration date in ImageProperties.DateTaken
|
|
|
|
|
|
var properties = await file.Properties.GetImagePropertiesAsync();
|
2015-12-03 20:04:10 +01:00
|
|
|
|
properties.DateTaken = expiration;
|
2015-11-28 21:09:25 +01:00
|
|
|
|
await properties.SavePropertiesAsync();
|
2014-11-19 21:11:14 +01:00
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
2015-12-03 20:04:10 +01:00
|
|
|
|
Debug.WriteLine("ImageFileCache: Writing {0}\\{1}: {2}", rootFolder.Path, key, ex.Message);
|
2014-11-19 21:11:14 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|