source: extensions/PiwigoLib/PiwigoLib/Service/PwgImagesService.cs @ 11850

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

MAj des truc existant et debut du upload de fichier

File size: 8.1 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Text;
4using System.Globalization;
5
6using Com.Piwigo.Lib.DTO;
7using Com.Piwigo.Lib.Proxy;
8using Com.Piwigo.Lib.Proxy.Response;
9using Com.Piwigo.Lib.DTO.Helper;
10using Com.Piwigo.Lib.IService;
11using System.IO;
12
13namespace Com.Piwigo.Lib.Service
14{
15
16    internal sealed class PwgImagesService : IPwgImagesService
17    {
18        public PwgImageRate RateImage(Int32 imageId, Int32 imageRate)
19        {
20            PwgImageRate returnValue = new PwgImageRate();
21
22            try
23            {
24                PwgImageRateProxyResponse response = PwgImagesProxy.pwg_images_rate(imageId, imageRate);
25
26                if (response.Retour != PwgBaseProxyReponseRetourEnum.Ok)
27                {
28                    if (response.Erreur != null)
29                    {
30                        throw new PwgServiceException("RateImage, the server has return the error.",
31                            response.Erreur.Code,
32                            response.Erreur.Message);
33                    }
34                    else
35                    {
36                        throw new PwgServiceException("RateImage : a error occurs during server process.");
37                    }
38                }
39                else
40                {
41                    returnValue = ConvertProxyResponseToDTO(response);
42                }
43            }
44            catch (PwgProxyException ex)
45            {
46                throw new PwgServiceException("RateImage : a error is raised by proxy.", ex);
47            }
48            return returnValue;
49        }
50
51        public Boolean addImageByMultiPartForm(FileInfo fileImage, Int32? imageId, Int32? categoryId, String imageName, String imageAuthor, String imageComment, Int32? imageLevel, String imageTags)
52        {
53            Boolean returnValue = false;
54
55            try
56            {
57                PwgBaseProxyReponse response = PwgImagesProxy.pwg_images_addSimple(fileImage,imageId, categoryId, imageName, imageAuthor, imageComment, imageLevel, imageTags );
58
59                if (response.Retour != PwgBaseProxyReponseRetourEnum.Ok)
60                {
61                    if (response.Erreur != null)
62                    {
63                        throw new PwgServiceException("addImageByMultiPartForm, the server has return the error.",
64                            response.Erreur.Code,
65                            response.Erreur.Message);
66                    }
67                    else
68                    {
69                        throw new PwgServiceException("addImageByMultiPartForm : a error occurs during server process.");
70                    }
71                }
72                else
73                {
74                    returnValue = true;
75                }
76            }
77            catch (PwgProxyException ex)
78            {
79                throw new PwgServiceException("addImageByMultiPartForm : a error is raised by proxy.", ex);
80            }
81            return returnValue;
82        }
83
84        /// <summary>
85        /// private: convert response to dto object
86        /// </summary>
87        /// <param name="response"></param>
88        /// <param name="session"></param>
89        internal static PwgImageRate ConvertProxyResponseToDTO(PwgImageRateProxyResponse response)
90        {
91            PwgImageRate returnValue = new PwgImageRate();
92            Double aDouble = 0;
93            NumberStyles style = NumberStyles.AllowDecimalPoint;
94            CultureInfo en_us = CultureInfo.CreateSpecificCulture("en-US");
95
96            returnValue.Count = response.Count;
97
98            returnValue.Stdev = 0.0 ;
99            if (Double.TryParse(response.Stdev, style, en_us, out aDouble))
100            {
101                returnValue.Stdev = aDouble;
102            }
103
104            returnValue.Average = 0.0;
105            if (Double.TryParse(response.Average, style, en_us, out aDouble))
106            {
107                returnValue.Average = aDouble;
108            }
109
110
111            return returnValue;
112        }
113
114        /// <summary>
115        /// private: convert response to dto object
116        /// </summary>
117        /// <param name="response"></param>
118        /// <param name="session"></param>
119        internal static void ConvertProxyResponseToDTO(PwgImagesProxyResponse response,
120                                                      List<PwgImage> lstImage, 
121                                                      ref Int32 Page, ref Int32 PerPagee, ref Int32 Count)
122        {
123                ConvertProxyResponseToDTO(response.ImageList, lstImage, ref Page, ref PerPagee, ref Count);
124        }
125
126        /// <summary>
127        /// private: convert response to dto object
128        /// </summary>
129        /// <param name="response"></param>
130        /// <param name="session"></param>
131        internal static void ConvertProxyResponseToDTO(PwgImageListProxyResponse response,
132                                                      List<PwgImage> lstImage, 
133                                                      ref Int32 Page, ref Int32 PerPagee, ref Int32 Count)
134        {
135            Page = response.Page;
136            PerPagee = response.PerPage;
137            Count = response.Count;
138
139            if (response.Images != null)
140            {
141                foreach (PwgImageProxyResponse respImg in response.Images)
142                {
143                    lstImage.Add(ConvertProxyResponseToDTO(respImg));
144                }
145            }
146        }
147
148        /// <summary>
149        /// private: convert response to dto object
150        /// </summary>
151        /// <param name="response"></param>
152        /// <param name="session"></param>
153        internal static PwgImage ConvertProxyResponseToDTO(PwgImageProxyResponse response)
154        {
155            PwgImage returnValue = new PwgImage();
156
157            try
158            {
159                returnValue.Counter = response.Counter;
160                returnValue.Id = response.Id;
161                returnValue.File = response.File;
162                returnValue.Height = response.Height;
163                returnValue.Width = response.Width;
164
165                if (String.IsNullOrEmpty(response.UrlElement))
166                {
167                    returnValue.UrlElement = null;
168                }
169                else
170                {
171                    returnValue.UrlElement = (new UriBuilder(response.UrlElement)).Uri;
172                }
173
174                if (String.IsNullOrEmpty(response.UrlThunb))
175                {
176                    returnValue.UrlThunb = null;
177                }
178                else
179                {
180                    returnValue.UrlThunb = (new UriBuilder(response.UrlThunb)).Uri;
181                }
182
183                if (String.IsNullOrEmpty(response.UrlHighDef))
184                {
185                    returnValue.UrlHighDef = null;
186                }
187                else
188                {
189                    returnValue.UrlHighDef = (new UriBuilder(response.UrlHighDef)).Uri;
190                }
191
192                returnValue.Tags = new List<PwgTag>();
193                if (response.Tags != null)
194                {
195                    foreach (PwgTagProxyResponse tagResp in response.Tags)
196                    {
197                        PwgTag aTag = PwgTagsService.ConvertProxyResponseToDTO(tagResp);
198                        returnValue.Tags.Add(aTag);
199                    }
200                }
201
202                returnValue.Categories = new List<PwgCategory>();
203                if (response.Categories != null)
204                {
205                    foreach (PwgCategoryProxyResponse catResp in response.Categories)
206                    {
207                        PwgCategory aCat = PwgCategoriesService.ConvertProxyResponseToDTO(catResp);
208                        returnValue.Categories.Add(aCat);
209                    }
210                }
211
212            }
213            catch (Exception ex)
214            {
215                throw new PwgServiceException("ConvertProxyResponseToDTO : a error is raised when converting PwgTagProxyResponse.", ex);
216            }
217
218            return returnValue;
219        }
220    }
221}
Note: See TracBrowser for help on using the repository browser.