source: extensions/PiwigoLib/PiwigoWpf/Service/ImageCacheManager.cs @ 11904

Last change on this file since 11904 was 11904, checked in by bayral, 13 years ago

PiwigoWPF

File size: 5.0 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using Com.Piwigo.Wpf.DTO;
6using System.Threading;
7using System.IO;
8using System.Net;
9using System.Drawing;
10using Com.Piwigo.Wpf.Helper;
11
12namespace 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}
Note: See TracBrowser for help on using the repository browser.