- Timestamp:
- Aug 5, 2011, 5:00:07 PM (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
extensions/PiwigoLib/PiwigoWpf/Service/ImageCacheManager.cs
r11904 r11911 14 14 public class ImageCacheManager 15 15 { 16 #region WPF Converter 17 static readonly ImageUrlCachedConverter _imageUrlCachedConverter = new ImageUrlCachedConverter(); 18 19 public static ImageUrlCachedConverter ImageUrlCachedConverter 20 { 21 get 22 { 23 return _imageUrlCachedConverter; 24 } 25 } 26 27 public static String GetDefaultImage() 28 { 29 return ("pack://application:,,,/Pictures/Globe.png"); 30 } 31 32 #endregion WPF Converter 33 34 #region Singleton 16 35 static readonly ImageCacheManagerBase _instance = new ImageCacheManagerBase(); 17 18 static readonly ImageUrlCachedConverter _imageUrlCachedConverter = new ImageUrlCachedConverter();19 36 20 37 // Explicit static constructor to tell C# compiler … … 31 48 } 32 49 } 33 34 public static ImageUrlCachedConverter ImageUrlCachedConverter 35 { 36 get 37 { 38 return _imageUrlCachedConverter; 39 } 40 } 41 public class ImageCacheManagerBase 42 { 50 #endregion Singleton 51 52 #region singleton base class 53 public class ImageCacheManagerBase : IDisposable 54 { 55 #region DTO 43 56 // local DTO 44 private class ImageDictionnary : SortedDictionary<Int32, String> { }; 45 private class ServerDictionnary : SortedDictionary<String, ImageDictionnary> { }; 46 47 private Object _lock = new Object(); 48 57 internal class CategoryDictionnaryItem 58 { 59 internal DirectoryInfo BasePath; 60 internal String CategorieId; 61 }; 62 63 internal class CategoryDictionnary : SortedDictionary<String, CategoryDictionnaryItem> { }; 64 65 internal class ServerDictionnaryItem 66 { 67 internal DirectoryInfo BasePath; 68 internal CategoryDictionnary Categories; 69 internal Uri ServeurUrl; 70 }; 71 72 internal class ServerDictionnary : SortedDictionary<String, ServerDictionnaryItem> { }; 73 74 internal static DirectoryInfo AppCachedDataDirectory = new DirectoryInfo(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),"PiwigoWpf", "cache")); 75 76 internal Object _lock = new Object(); 77 internal ServerDictionnary _aServerDictionnary; 78 internal ServerDictionnaryItem _currentServer; 79 internal CategoryDictionnaryItem _currentCategory; 80 81 #endregion DTO 82 83 #region local helper 49 84 // local helper 50 85 static private String BuildServerDirCache(String serveurUri) 51 86 { 52 String AppCachedDataDirectory = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),"cache");53 S tring ServerDirCache = Path.Combine(AppCachedDataDirectory, serveurUri.Replace("http://", "_").Replace("/", "_"));87 String ServerDirCache = Path.Combine(AppCachedDataDirectory.FullName, serveurUri.Replace("http://", "_").Replace("/", "_")); 88 System.Diagnostics.Debug.Print(ServerDirCache); 54 89 55 90 return ServerDirCache; 56 91 } 57 92 58 59 60 private ServerDictionnary _aServerDictionnary = new ServerDictionnary(); 61 private String _currentServer; 62 private ImageDictionnary _currentImageDictionnary = new ImageDictionnary(); 93 static private String BuildCategoryDirCache(ServerDictionnaryItem server, String CatgeoryId) 94 { 95 String CategoryDirCache = String.Empty; 96 if (server != null) 97 { 98 CategoryDirCache = Path.Combine(server.BasePath.FullName, CatgeoryId.Replace("/", "_")); 99 } 100 else 101 { 102 throw new ApplicationException ("Current server not set before using category"); 103 } 104 105 return CategoryDirCache; 106 } 107 108 static private ServerDictionnary BuildServerDictionnaryFromCache() 109 { 110 ServerDictionnary retLstServerDictionnary = new ServerDictionnary(); 111 112 if (AppCachedDataDirectory.Exists) 113 { 114 foreach (DirectoryInfo Dir in AppCachedDataDirectory.GetDirectories()) 115 { 116 FileInfo xmlData = new FileInfo(Path.Combine(Dir.FullName, "serverdictionnaryitem.xml")); 117 if (xmlData.Exists) 118 { 119 ServerDictionnaryItem item = XMLHelper<ServerDictionnaryItem>.DeserializeObjectFromFile(xmlData); 120 retLstServerDictionnary.Add(item.ServeurUrl.ToString(), item); 121 } 122 } 123 } 124 125 return retLstServerDictionnary; 126 } 127 128 static void WriteServerDictionnaryToDisk(ServerDictionnary dcServer) 129 { 130 foreach (ServerDictionnaryItem Item in dcServer.Values) 131 { 132 FileInfo xmlData = new FileInfo(Path.Combine(Item.BasePath.FullName, "serverdictionnaryitem.xml")); 133 XMLHelper<ServerDictionnaryItem>.SerializeToXMLFile(Item, xmlData); 134 } 135 } 136 #endregion local helper 137 138 public void UnsetCurrentServer() 139 { 140 _currentCategory = null; 141 _currentServer = null; 142 } 63 143 64 144 public void SetCurrentServer(String serveurUri) … … 67 147 lock (_lock) 68 148 { 149 // load data from disk on first access 150 if (_aServerDictionnary == null) 151 { 152 _aServerDictionnary = BuildServerDictionnaryFromCache(); 153 } 154 69 155 if (_aServerDictionnary.ContainsKey(serveurUri)) 70 156 { 71 _currentServer = serveurUri;157 _currentServer = _aServerDictionnary[serveurUri]; 72 158 } 73 159 else 74 160 { 75 ImageDictionnary newImageDictionnary = new ImageDictionnary(); 76 _aServerDictionnary.Add(serveurUri, newImageDictionnary); 77 _currentServer = serveurUri; 78 } 79 } 80 if (!Directory.Exists(currentserverDir)) 81 { 82 //Create it 83 Directory.CreateDirectory(currentserverDir); 161 ServerDictionnaryItem server = new ServerDictionnaryItem(); 162 server.ServeurUrl = new Uri(serveurUri); 163 server.BasePath = new DirectoryInfo(currentserverDir); 164 server.Categories = new CategoryDictionnary(); 165 _aServerDictionnary.Add(serveurUri, server); 166 _currentServer = server; 167 } 168 169 if (!Directory.Exists(currentserverDir)) 170 { 171 //Create it 172 Directory.CreateDirectory(currentserverDir); 173 _currentServer.Categories.Clear(); 174 } 175 } 176 } 177 178 public void SetCurrentCategory(String CategorieId) 179 { 180 if (_currentServer != null) 181 { 182 lock (_lock) 183 { 184 String currentsCategoryDir = BuildCategoryDirCache(_currentServer, CategorieId); 185 if (_currentServer.Categories.ContainsKey(CategorieId)) 186 { 187 _currentCategory = _currentServer.Categories[CategorieId]; 188 } 189 else 190 { 191 CategoryDictionnaryItem category = new CategoryDictionnaryItem(); 192 category.BasePath = new DirectoryInfo(currentsCategoryDir); 193 category.CategorieId = CategorieId; 194 _currentServer.Categories.Add(CategorieId, category); 195 _currentCategory = category; 196 } 197 198 if (!Directory.Exists(currentsCategoryDir)) 199 { 200 //Create it 201 Directory.CreateDirectory(currentsCategoryDir); 202 } 203 } 204 } 205 else 206 { 207 throw new ApplicationException("Current server not set before using category"); 84 208 } 85 209 } … … 87 211 public String GetImageFilename(PwgImageWPF aImageWPF) 88 212 { 213 String retValue = String.Empty; 89 214 if (aImageWPF == null) 90 215 { 91 return String.Empty; 92 } 93 94 String retValue; 95 String currentserverDir = BuildServerDirCache(_currentServer); 96 String localFile = Path.Combine(currentserverDir, String.Format("{0}-{1}", aImageWPF.Id, aImageWPF.File)); 97 98 if (_aServerDictionnary[_currentServer].ContainsKey(aImageWPF.Id)) 99 { 100 retValue = _aServerDictionnary[_currentServer][aImageWPF.Id]; 216 return retValue; 217 } 218 219 if (_currentCategory != null) 220 { 221 String localFile = Path.Combine(_currentCategory.BasePath.FullName, String.Format("{0}-{1}", aImageWPF.Id, aImageWPF.File)); 222 223 if (!File.Exists(localFile)) 224 { 225 ThreadPool.QueueUserWorkItem((WaitCallback)delegate 226 { 227 WebRequest request = HttpWebRequest.Create(aImageWPF.UrlElement); 228 WebResponse response = request.GetResponse(); 229 230 //check the content type to assert that the file in the uri is an image 231 if (!response.ContentType.StartsWith("image")) 232 { 233 throw new FileFormatException(aImageWPF.UrlElement, String.Format("Uri passed to ImageCacher does not return an image. Content is of type {0}.", response.ContentType)); 234 } 235 236 //load the image from the stream 237 Image image = Image.FromStream(response.GetResponseStream()); 238 239 //save it 240 image.Save(localFile); 241 242 }); 243 244 retValue = localFile; 245 } 101 246 } 102 247 else 103 248 { 104 if (!File.Exists(localFile)) 105 { 106 ThreadPool.QueueUserWorkItem((WaitCallback)delegate 107 { 108 WebRequest request = HttpWebRequest.Create(aImageWPF.UrlElement); 109 WebResponse response = request.GetResponse(); 110 111 //check the content type to assert that the file in the uri is an image 112 if (!response.ContentType.StartsWith("image")) 113 { 114 throw new FileFormatException(aImageWPF.UrlElement, String.Format("Uri passed to ImageCacher does not return an image. Content is of type {0}.", response.ContentType)); 115 } 116 117 //load the image from the stream 118 Image image = Image.FromStream(response.GetResponseStream()); 119 120 //save it 121 image.Save(localFile); 122 123 }); 124 125 } 126 127 _aServerDictionnary[_currentServer].Add(aImageWPF.Id,localFile); 128 retValue = localFile; 249 throw new ApplicationException("Current category not set before using image cache."); 129 250 } 130 251 return retValue; 131 252 } 132 } 133 } 134 253 254 public void Dispose() 255 { 256 if (_aServerDictionnary != null) 257 { 258 WriteServerDictionnaryToDisk(_aServerDictionnary); 259 } 260 } 261 } 262 263 #endregion singleton base class 135 264 } 265 266 }
Note: See TracChangeset
for help on using the changeset viewer.