2012-11-22 21:42:29 +01:00
|
|
|
|
// XAML Map Control - http://xamlmapcontrol.codeplex.com/
|
2016-02-23 20:07:30 +01:00
|
|
|
|
// © 2016 Clemens Fischer
|
2012-11-22 21:42:29 +01:00
|
|
|
|
// Licensed under the Microsoft Public License (Ms-PL)
|
|
|
|
|
|
|
|
|
|
|
|
using System;
|
2012-08-15 21:31:10 +02:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Collections.Specialized;
|
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
|
using System.IO;
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using System.Runtime.Caching;
|
2013-10-10 00:46:47 +02:00
|
|
|
|
using System.Security.AccessControl;
|
|
|
|
|
|
using System.Security.Principal;
|
2012-08-15 21:31:10 +02:00
|
|
|
|
|
2014-11-19 21:11:14 +01:00
|
|
|
|
namespace MapControl.Caching
|
2012-08-15 21:31:10 +02:00
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// ObjectCache implementation based on local image files.
|
2015-11-28 21:09:25 +01:00
|
|
|
|
/// The only valid data type for cached values is byte[].
|
2012-08-15 21:31:10 +02:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
public class ImageFileCache : ObjectCache
|
|
|
|
|
|
{
|
2015-11-28 21:09:25 +01:00
|
|
|
|
private static readonly Tuple<string, byte[]>[] imageFileTypes = new Tuple<string, byte[]>[]
|
|
|
|
|
|
{
|
2015-12-03 20:04:10 +01:00
|
|
|
|
new Tuple<string, byte[]>(".png", new byte[] { 0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A }),
|
|
|
|
|
|
new Tuple<string, byte[]>(".jpg", new byte[] { 0xFF, 0xD8, 0xFF }),
|
2015-11-28 21:09:25 +01:00
|
|
|
|
new Tuple<string, byte[]>(".bmp", new byte[] { 0x42, 0x4D }),
|
|
|
|
|
|
new Tuple<string, byte[]>(".gif", new byte[] { 0x47, 0x49, 0x46 }),
|
2015-12-03 20:04:10 +01:00
|
|
|
|
new Tuple<string, byte[]>(".tif", new byte[] { 0x49, 0x49, 0x2A, 0x00 }),
|
|
|
|
|
|
new Tuple<string, byte[]>(".tif", new byte[] { 0x4D, 0x4D, 0x00, 0x2A }),
|
2015-11-28 21:09:25 +01:00
|
|
|
|
new Tuple<string, byte[]>(".bin", new byte[] { }),
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2013-10-10 00:46:47 +02:00
|
|
|
|
private static readonly FileSystemAccessRule fullControlRule = new FileSystemAccessRule(
|
|
|
|
|
|
new SecurityIdentifier(WellKnownSidType.BuiltinUsersSid, null),
|
|
|
|
|
|
FileSystemRights.FullControl, AccessControlType.Allow);
|
|
|
|
|
|
|
2014-11-19 21:11:14 +01:00
|
|
|
|
private readonly MemoryCache memoryCache = MemoryCache.Default;
|
2012-08-15 21:31:10 +02:00
|
|
|
|
private readonly string name;
|
2014-11-19 21:11:14 +01:00
|
|
|
|
private readonly string rootFolder;
|
2012-08-15 21:31:10 +02:00
|
|
|
|
|
|
|
|
|
|
public ImageFileCache(string name, NameValueCollection config)
|
2014-11-19 21:11:14 +01:00
|
|
|
|
: this(name, config["folder"])
|
2012-08-15 21:31:10 +02:00
|
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2014-11-19 21:11:14 +01:00
|
|
|
|
public ImageFileCache(string name, string folder)
|
2012-08-15 21:31:10 +02:00
|
|
|
|
{
|
2015-03-15 10:11:19 +01:00
|
|
|
|
if (string.IsNullOrEmpty(name))
|
2012-08-15 21:31:10 +02:00
|
|
|
|
{
|
2015-03-15 10:11:19 +01:00
|
|
|
|
throw new ArgumentException("The parameter name must not be null or empty.");
|
2012-08-15 21:31:10 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2015-03-15 10:11:19 +01:00
|
|
|
|
if (string.IsNullOrEmpty(folder))
|
2012-08-15 21:31:10 +02:00
|
|
|
|
{
|
2015-03-15 10:11:19 +01:00
|
|
|
|
throw new ArgumentException("The parameter folder must not be null or empty.");
|
2012-08-15 21:31:10 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
this.name = name;
|
2014-11-19 21:11:14 +01:00
|
|
|
|
rootFolder = Path.Combine(folder, name);
|
|
|
|
|
|
Directory.CreateDirectory(rootFolder);
|
2012-08-15 21:31:10 +02:00
|
|
|
|
|
2015-12-03 20:04:10 +01:00
|
|
|
|
Debug.WriteLine("Created ImageFileCache in " + rootFolder);
|
2012-08-15 21:31:10 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override string Name
|
|
|
|
|
|
{
|
|
|
|
|
|
get { return name; }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override DefaultCacheCapabilities DefaultCacheCapabilities
|
|
|
|
|
|
{
|
|
|
|
|
|
get { return DefaultCacheCapabilities.InMemoryProvider; }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override object this[string key]
|
|
|
|
|
|
{
|
|
|
|
|
|
get { return Get(key); }
|
|
|
|
|
|
set { Set(key, value, null); }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected override IEnumerator<KeyValuePair<string, object>> GetEnumerator()
|
|
|
|
|
|
{
|
2014-11-19 21:11:14 +01:00
|
|
|
|
throw new NotSupportedException("ImageFileCache does not support the ability to enumerate items.");
|
2012-08-15 21:31:10 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override CacheEntryChangeMonitor CreateCacheEntryChangeMonitor(IEnumerable<string> keys, string regionName = null)
|
|
|
|
|
|
{
|
2014-11-19 21:11:14 +01:00
|
|
|
|
throw new NotSupportedException("ImageFileCache does not support the ability to create change monitors.");
|
2012-08-15 21:31:10 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override long GetCount(string regionName = null)
|
|
|
|
|
|
{
|
2014-11-19 21:11:14 +01:00
|
|
|
|
throw new NotSupportedException("ImageFileCache does not support the ability to count items.");
|
2012-08-15 21:31:10 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override bool Contains(string key, string regionName = null)
|
|
|
|
|
|
{
|
2015-11-28 21:09:25 +01:00
|
|
|
|
if (key == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new ArgumentNullException("The parameter key must not be null.");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (regionName != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new NotSupportedException("The parameter regionName must be null.");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return memoryCache.Contains(key) || FindFile(key) != null;
|
2012-08-15 21:31:10 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override object Get(string key, string regionName = null)
|
|
|
|
|
|
{
|
2015-11-28 21:09:25 +01:00
|
|
|
|
if (key == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new ArgumentNullException("The parameter key must not be null.");
|
|
|
|
|
|
}
|
2012-08-15 21:31:10 +02:00
|
|
|
|
|
2015-11-28 21:09:25 +01:00
|
|
|
|
if (regionName != null)
|
2012-08-15 21:31:10 +02:00
|
|
|
|
{
|
2015-11-28 21:09:25 +01:00
|
|
|
|
throw new NotSupportedException("The parameter regionName must be null.");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var buffer = memoryCache.Get(key) as byte[];
|
2012-08-15 21:31:10 +02:00
|
|
|
|
|
2015-11-28 21:09:25 +01:00
|
|
|
|
if (buffer == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
var path = FindFile(key);
|
|
|
|
|
|
|
|
|
|
|
|
if (path != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
try
|
2012-08-15 21:31:10 +02:00
|
|
|
|
{
|
2015-12-03 20:04:10 +01:00
|
|
|
|
//Debug.WriteLine("ImageFileCache: Reading " + path);
|
2015-11-28 21:09:25 +01:00
|
|
|
|
buffer = File.ReadAllBytes(path);
|
|
|
|
|
|
memoryCache.Set(key, buffer, new CacheItemPolicy());
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
2015-12-03 20:04:10 +01:00
|
|
|
|
Debug.WriteLine("ImageFileCache: Reading {0}: {1}", path, ex.Message);
|
2012-08-15 21:31:10 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2015-11-28 21:09:25 +01:00
|
|
|
|
return buffer;
|
2012-08-15 21:31:10 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override CacheItem GetCacheItem(string key, string regionName = null)
|
|
|
|
|
|
{
|
|
|
|
|
|
var value = Get(key, regionName);
|
2014-10-19 21:50:23 +02:00
|
|
|
|
|
2012-08-15 21:31:10 +02:00
|
|
|
|
return value != null ? new CacheItem(key, value) : null;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override IDictionary<string, object> GetValues(IEnumerable<string> keys, string regionName = null)
|
|
|
|
|
|
{
|
2014-11-19 21:11:14 +01:00
|
|
|
|
return keys.ToDictionary(key => key, key => Get(key, regionName));
|
|
|
|
|
|
}
|
2012-08-15 21:31:10 +02:00
|
|
|
|
|
2014-11-19 21:11:14 +01:00
|
|
|
|
public override void Set(string key, object value, CacheItemPolicy policy, string regionName = null)
|
|
|
|
|
|
{
|
2015-11-28 21:09:25 +01:00
|
|
|
|
if (key == null)
|
2012-08-15 21:31:10 +02:00
|
|
|
|
{
|
2015-11-28 21:09:25 +01:00
|
|
|
|
throw new ArgumentNullException("The parameter key must not be null.");
|
2012-08-15 21:31:10 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2015-11-28 21:09:25 +01:00
|
|
|
|
if (regionName != null)
|
2012-08-15 21:31:10 +02:00
|
|
|
|
{
|
2015-11-28 21:09:25 +01:00
|
|
|
|
throw new NotSupportedException("The parameter regionName must be null.");
|
2012-08-15 21:31:10 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2015-11-28 21:09:25 +01:00
|
|
|
|
var buffer = value as byte[];
|
|
|
|
|
|
|
|
|
|
|
|
if (buffer == null || buffer.Length == 0)
|
2012-08-15 21:31:10 +02:00
|
|
|
|
{
|
2015-11-28 21:09:25 +01:00
|
|
|
|
throw new NotSupportedException("The parameter value must be a non-empty byte array.");
|
2012-08-15 21:31:10 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2015-11-28 21:09:25 +01:00
|
|
|
|
memoryCache.Set(key, buffer, policy);
|
2012-08-15 21:31:10 +02:00
|
|
|
|
|
2015-11-28 21:09:25 +01:00
|
|
|
|
var path = Path.Combine(rootFolder, key)
|
|
|
|
|
|
+ imageFileTypes.First(t => t.Item2.SequenceEqual(buffer.Take(t.Item2.Length))).Item1;
|
2012-08-15 21:31:10 +02:00
|
|
|
|
|
2013-11-12 21:14:53 +01:00
|
|
|
|
try
|
2012-08-15 21:31:10 +02:00
|
|
|
|
{
|
2015-12-03 20:04:10 +01:00
|
|
|
|
//Debug.WriteLine("ImageFileCache: Writing {0}, Expires {1}", path, policy.AbsoluteExpiration.DateTime.ToLocalTime());
|
2013-11-12 21:14:53 +01:00
|
|
|
|
Directory.CreateDirectory(Path.GetDirectoryName(path));
|
2015-11-28 21:09:25 +01:00
|
|
|
|
File.WriteAllBytes(path, buffer);
|
2013-11-12 21:14:53 +01:00
|
|
|
|
|
|
|
|
|
|
var fileSecurity = File.GetAccessControl(path);
|
|
|
|
|
|
fileSecurity.AddAccessRule(fullControlRule);
|
|
|
|
|
|
File.SetAccessControl(path, fileSecurity);
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
2015-12-03 20:04:10 +01:00
|
|
|
|
Debug.WriteLine("ImageFileCache: Writing {0}: {1}", path, ex.Message);
|
2012-08-15 21:31:10 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override void Set(CacheItem item, CacheItemPolicy policy)
|
|
|
|
|
|
{
|
|
|
|
|
|
Set(item.Key, item.Value, policy, item.RegionName);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override void Set(string key, object value, DateTimeOffset absoluteExpiration, string regionName = null)
|
|
|
|
|
|
{
|
|
|
|
|
|
Set(key, value, new CacheItemPolicy { AbsoluteExpiration = absoluteExpiration }, regionName);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override object AddOrGetExisting(string key, object value, CacheItemPolicy policy, string regionName = null)
|
|
|
|
|
|
{
|
|
|
|
|
|
var oldValue = Get(key, regionName);
|
2014-10-19 21:50:23 +02:00
|
|
|
|
|
2012-08-15 21:31:10 +02:00
|
|
|
|
Set(key, value, policy);
|
2014-10-19 21:50:23 +02:00
|
|
|
|
|
2012-08-15 21:31:10 +02:00
|
|
|
|
return oldValue;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override CacheItem AddOrGetExisting(CacheItem item, CacheItemPolicy policy)
|
|
|
|
|
|
{
|
|
|
|
|
|
var oldItem = GetCacheItem(item.Key, item.RegionName);
|
2014-10-19 21:50:23 +02:00
|
|
|
|
|
2012-08-15 21:31:10 +02:00
|
|
|
|
Set(item, policy);
|
2014-10-19 21:50:23 +02:00
|
|
|
|
|
2012-08-15 21:31:10 +02:00
|
|
|
|
return oldItem;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override object AddOrGetExisting(string key, object value, DateTimeOffset absoluteExpiration, string regionName = null)
|
|
|
|
|
|
{
|
|
|
|
|
|
return AddOrGetExisting(key, value, new CacheItemPolicy { AbsoluteExpiration = absoluteExpiration }, regionName);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override object Remove(string key, string regionName = null)
|
|
|
|
|
|
{
|
2015-11-28 21:09:25 +01:00
|
|
|
|
if (key == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new ArgumentNullException("The parameter key must not be null.");
|
|
|
|
|
|
}
|
2012-08-15 21:31:10 +02:00
|
|
|
|
|
2015-11-28 21:09:25 +01:00
|
|
|
|
if (regionName != null)
|
2012-08-15 21:31:10 +02:00
|
|
|
|
{
|
2015-11-28 21:09:25 +01:00
|
|
|
|
throw new NotSupportedException("The parameter regionName must be null.");
|
|
|
|
|
|
}
|
2012-08-15 21:31:10 +02:00
|
|
|
|
|
2015-11-28 21:09:25 +01:00
|
|
|
|
memoryCache.Remove(key);
|
|
|
|
|
|
|
|
|
|
|
|
var path = FindFile(key);
|
|
|
|
|
|
|
|
|
|
|
|
if (path != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
try
|
2012-08-15 21:31:10 +02:00
|
|
|
|
{
|
|
|
|
|
|
File.Delete(path);
|
|
|
|
|
|
}
|
2015-11-28 21:09:25 +01:00
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
2015-12-03 20:04:10 +01:00
|
|
|
|
Debug.WriteLine("ImageFileCache: Removing {0}: {1}", path, ex.Message);
|
2015-11-28 21:09:25 +01:00
|
|
|
|
}
|
2012-08-15 21:31:10 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2015-11-28 21:09:25 +01:00
|
|
|
|
return null;
|
2012-08-15 21:31:10 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2015-11-28 21:09:25 +01:00
|
|
|
|
private string FindFile(string key)
|
2012-08-15 21:31:10 +02:00
|
|
|
|
{
|
2015-11-28 21:09:25 +01:00
|
|
|
|
var path = Path.Combine(rootFolder, key);
|
2012-08-15 21:31:10 +02:00
|
|
|
|
|
2015-11-28 21:09:25 +01:00
|
|
|
|
try
|
2012-08-15 21:31:10 +02:00
|
|
|
|
{
|
2015-11-28 21:09:25 +01:00
|
|
|
|
if (!string.IsNullOrEmpty(Path.GetExtension(path)))
|
|
|
|
|
|
{
|
|
|
|
|
|
return path;
|
|
|
|
|
|
}
|
2012-08-15 21:31:10 +02:00
|
|
|
|
|
2015-11-28 21:09:25 +01:00
|
|
|
|
string folderName = Path.GetDirectoryName(path);
|
2012-08-15 21:31:10 +02:00
|
|
|
|
|
2015-11-28 21:09:25 +01:00
|
|
|
|
if (Directory.Exists(folderName))
|
|
|
|
|
|
{
|
|
|
|
|
|
return Directory.EnumerateFiles(folderName, Path.GetFileName(path) + ".*").FirstOrDefault();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
2012-08-15 21:31:10 +02:00
|
|
|
|
{
|
2015-12-03 20:04:10 +01:00
|
|
|
|
Debug.WriteLine("ImageFileCache: Finding {0}: {1}", path, ex.Message);
|
2012-08-15 21:31:10 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|