1 | using System; |
---|
2 | using System.Collections.Generic; |
---|
3 | using System.Linq; |
---|
4 | using System.Text; |
---|
5 | using Com.Piwigo.Wpf.DTO; |
---|
6 | using System.Threading; |
---|
7 | using System.IO; |
---|
8 | using System.Net; |
---|
9 | using System.Drawing; |
---|
10 | using Com.Piwigo.Wpf.Helper; |
---|
11 | |
---|
12 | namespace Com.Piwigo.Wpf.Service |
---|
13 | { |
---|
14 | public class ImageCacheManager |
---|
15 | { |
---|
16 | static readonly ImageCacheManagerBase _instance = new ImageCacheManagerBase(); |
---|
17 | |
---|
18 | static readonly ImageUrlCachedConverter _imageUrlCachedConverter = new ImageUrlCachedConverter(); |
---|
19 | |
---|
20 | // Explicit static constructor to tell C# compiler |
---|
21 | // not to mark type as beforefieldinit |
---|
22 | static ImageCacheManager() |
---|
23 | { |
---|
24 | } |
---|
25 | |
---|
26 | public static ImageCacheManagerBase Instance |
---|
27 | { |
---|
28 | get |
---|
29 | { |
---|
30 | return _instance; |
---|
31 | } |
---|
32 | } |
---|
33 | |
---|
34 | public static ImageUrlCachedConverter ImageUrlCachedConverter |
---|
35 | { |
---|
36 | get |
---|
37 | { |
---|
38 | return _imageUrlCachedConverter; |
---|
39 | } |
---|
40 | } |
---|
41 | public class ImageCacheManagerBase |
---|
42 | { |
---|
43 | // local DTO |
---|
44 | private class ImageDictionnary : SortedDictionary<Int32, String> { }; |
---|
45 | private class ServerDictionnary : SortedDictionary<String, ImageDictionnary> { }; |
---|
46 | |
---|
47 | private Object _lock = new Object(); |
---|
48 | |
---|
49 | // local helper |
---|
50 | static private String BuildServerDirCache(String serveurUri) |
---|
51 | { |
---|
52 | String AppCachedDataDirectory = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),"cache"); |
---|
53 | String ServerDirCache = Path.Combine(AppCachedDataDirectory, serveurUri.Replace("http://", "_").Replace("/", "_")); |
---|
54 | |
---|
55 | return ServerDirCache; |
---|
56 | } |
---|
57 | |
---|
58 | |
---|
59 | |
---|
60 | private ServerDictionnary _aServerDictionnary = new ServerDictionnary(); |
---|
61 | private String _currentServer; |
---|
62 | private ImageDictionnary _currentImageDictionnary = new ImageDictionnary(); |
---|
63 | |
---|
64 | public void SetCurrentServer(String serveurUri) |
---|
65 | { |
---|
66 | String currentserverDir = BuildServerDirCache(serveurUri); |
---|
67 | lock (_lock) |
---|
68 | { |
---|
69 | if (_aServerDictionnary.ContainsKey(serveurUri)) |
---|
70 | { |
---|
71 | _currentServer = serveurUri; |
---|
72 | } |
---|
73 | else |
---|
74 | { |
---|
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); |
---|
84 | } |
---|
85 | } |
---|
86 | |
---|
87 | public String GetImageFilename(PwgImageWPF aImageWPF) |
---|
88 | { |
---|
89 | if (aImageWPF == null) |
---|
90 | { |
---|
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]; |
---|
101 | } |
---|
102 | else |
---|
103 | { |
---|
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; |
---|
129 | } |
---|
130 | return retValue; |
---|
131 | } |
---|
132 | } |
---|
133 | } |
---|
134 | |
---|
135 | } |
---|