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

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

First draw of pwg.image.add and pwg.imae.addtrunck.
addtrunck seem to be ok, but image.add return server error 500.

File size: 21.8 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 PwgImageAdded addImageByMultiPartForm(FileInfo fileImage, Int32? imageId, Int32? categoryId, String imageName, String imageAuthor, String imageComment, Int32? imageLevel, List<PwgTag> imageTags)
52        {
53            PwgImageAdded returnValue = new PwgImageAdded();
54
55            try
56            {
57                PwgAddSimpleImageProxyResponse 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 = ConvertProxyResponseToDTO(response);
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        public Boolean addImage(FileInfo highResFile, FileInfo lowResFile, FileInfo thumbFile, String imageName, String imageAuthor, DateTime creationDate,
85            String authorComment, List<String> lstCategories, List<String> lstTags, PwgConfidentLevelEnum? confidentLevel)
86        {
87            Boolean returnValue = false;
88
89            try
90            {
91                String fileGuid =  uploadImageByChunk(lowResFile, PwgFileTypeEnum.LowRes);
92                String thunbGuid = uploadImageByChunk(thumbFile, PwgFileTypeEnum.Thumb);
93                String HighresGuid = uploadImageByChunk(highResFile, PwgFileTypeEnum.HighDef);
94                String imageGuid = (highResFile != null ? HighresGuid : fileGuid);  //Guid.NewGuid().ToString();
95                String imageFilename = (highResFile != null ? highResFile.Name : lowResFile.Name);
96
97                PwgBaseProxyReponse response = PwgImagesProxy.pwg_images_add( fileGuid, thunbGuid, HighresGuid, 
98                                                    imageGuid,  imageFilename, imageName, imageAuthor, creationDate, 
99                                                    authorComment, lstCategories, lstTags, confidentLevel);
100
101                if (response.Retour != PwgBaseProxyReponseRetourEnum.Ok)
102                {
103                    if (response.Erreur != null)
104                    {
105                        throw new PwgServiceException("addImage, the server has return the error.",
106                            response.Erreur.Code,
107                            response.Erreur.Message);
108                    }
109                    else
110                    {
111                        throw new PwgServiceException("addImage : a error occurs during server process.");
112                    }
113                }
114                else
115                {
116                    returnValue = true;
117                }
118            }
119            catch (PwgProxyException ex)
120            {
121                throw new PwgServiceException("addImage : a error is raised by proxy.", ex);
122            }
123            return returnValue;
124        }
125
126        public String uploadImageByChunk(FileInfo imageFile, PwgFileTypeEnum fileType)
127        {
128            byte[] buffer = new byte[5000];
129            string strGuid = String.Empty; ;
130            int read;
131            int chunkNumber = 1;
132
133            try
134            {
135                if (imageFile != null)
136                {
137                    FileStream fs = File.OpenRead(imageFile.FullName);
138                    strGuid = Com.Piwigo.Lib.Proxy.Helper.PwgBase64Helper.GetphpMd5Sum(fs);
139                   
140                    fs.Seek(0, SeekOrigin.Begin);
141                    while ((read = fs.Read(buffer, 0, buffer.Length)) > 0)
142                    {
143                        String strB64Data = Com.Piwigo.Lib.Proxy.Helper.PwgBase64Helper.base64Encode(buffer);
144                        PwgBaseProxyReponse response = PwgImagesProxy.pwg_images_addChunk(strB64Data, strGuid, fileType, chunkNumber);
145
146                        chunkNumber += 1;
147
148                        if (response.Retour != PwgBaseProxyReponseRetourEnum.Ok)
149                        {
150                            if (response.Erreur != null)
151                            {
152                                throw new PwgServiceException("uploadImageByChunk, the server has return the error.",
153                                    response.Erreur.Code,
154                                    response.Erreur.Message);
155                            }
156                            else
157                            {
158                                throw new PwgServiceException("uploadImageByChunk : a error occurs during server process.");
159                            }
160                        }
161                    }
162                }
163                else
164                {
165                    strGuid = String.Empty;
166                }
167            }
168            catch (PwgProxyException ex)
169            {
170                throw new PwgServiceException("uploadImageByChunk : a error is raised by proxy.", ex);
171            }
172            return strGuid;
173        }
174
175        /// <summary>
176        /// Delete a image
177        /// </summary>
178        /// <param name="imageId"></param>
179        /// <param name="SecurityToken"></param>
180        /// <returns></returns>
181        public Boolean DeleteImage(Int32 imageId, String SecurityToken)
182        {
183            Boolean returnValue = false;
184
185            try
186            {
187
188                PwgBaseProxyReponse response = PwgImagesProxy.pwg_images_delete(imageId, SecurityToken);
189
190                if (response.Retour != PwgBaseProxyReponseRetourEnum.Ok)
191                {
192                    if (response.Erreur != null)
193                    {
194                        throw new PwgServiceException("DeleteImage, the server has return the error.",
195                            response.Erreur.Code,
196                            response.Erreur.Message);
197                    }
198                    else
199                    {
200                        throw new PwgServiceException("DeleteImage : a error occurs during server process.");
201                    }
202                }
203                else
204                {
205                    returnValue = true;
206                }
207            }
208            catch (PwgProxyException ex)
209            {
210                throw new PwgServiceException("DeleteImage : a error is raised by proxy.", ex);
211            }
212            return returnValue;
213        }
214
215        public PwgImageInfo getImageInfo(Int32 imageId, Int32? commentPage, Int32? commentsPerPage)
216        {
217            PwgImageInfo returnValue = new PwgImageInfo();
218
219            try
220            {
221                PwgImageInfoProxyResponse response = PwgImagesProxy.pwg_images_getInfos(imageId, commentPage, commentsPerPage);
222
223                if (response.Retour != PwgBaseProxyReponseRetourEnum.Ok)
224                {
225                    if (response.Erreur != null)
226                    {
227                        throw new PwgServiceException("RateImage, the server has return the error.",
228                            response.Erreur.Code,
229                            response.Erreur.Message);
230                    }
231                    else
232                    {
233                        throw new PwgServiceException("RateImage : a error occurs during server process.");
234                    }
235                }
236                else
237                {
238                    returnValue = ConvertProxyResponseToDTO(response.ImageInfo);
239                }
240            }
241            catch (PwgProxyException ex)
242            {
243                throw new PwgServiceException("RateImage : a error is raised by proxy.", ex);
244            }
245            return returnValue;
246        }       
247
248        /// <summary>
249        /// private: convert response to dto object
250        /// </summary>
251        /// <param name="response"></param>
252        /// <param name="session"></param>
253        internal static PwgImageAdded ConvertProxyResponseToDTO(PwgAddSimpleImageProxyResponse response)
254        {
255            PwgImageAdded returnValue = new PwgImageAdded();
256
257            returnValue.Id = response.Id;
258
259            if (String.IsNullOrEmpty(response.UrlImage))
260            {
261                returnValue.UrlElement = null;
262            }
263            else
264            {
265                returnValue.UrlElement = (new UriBuilder(response.UrlImage)).Uri;
266            }
267           
268            return returnValue;
269        }
270
271
272        /// <summary>
273        /// private: convert response to dto object
274        /// </summary>
275        /// <param name="response"></param>
276        /// <param name="session"></param>
277        internal static PwgImageRate ConvertProxyResponseToDTO(PwgImageRateProxyResponse response)
278        {
279            PwgImageRate returnValue = new PwgImageRate();
280            Double aDouble = 0;
281            NumberStyles style = NumberStyles.AllowDecimalPoint;
282            CultureInfo en_us = CultureInfo.CreateSpecificCulture("en-US");
283
284            returnValue.Count = response.Count;
285
286            returnValue.Stdev = 0.0 ;
287            if (Double.TryParse(response.Stdev, style, en_us, out aDouble))
288            {
289                returnValue.Stdev = aDouble;
290            }
291
292            returnValue.Average = 0.0;
293            if (Double.TryParse(response.Average, style, en_us, out aDouble))
294            {
295                returnValue.Average = aDouble;
296            }
297
298
299            return returnValue;
300        }
301
302        /// <summary>
303        /// private: convert response to dto object
304        /// </summary>
305        /// <param name="response"></param>
306        /// <param name="session"></param>
307        internal static void ConvertProxyResponseToDTO(PwgImagesProxyResponse response,
308                                                      List<PwgImage> lstImage, 
309                                                      ref Int32 Page, ref Int32 PerPagee, ref Int32 Count)
310        {
311                ConvertProxyResponseToDTO(response.ImageList, lstImage, ref Page, ref PerPagee, ref Count);
312        }
313
314        /// <summary>
315        /// private: convert response to dto object
316        /// </summary>
317        /// <param name="response"></param>
318        /// <param name="session"></param>
319        internal static void ConvertProxyResponseToDTO(PwgImageListProxyResponse response,
320                                                      List<PwgImage> lstImage, 
321                                                      ref Int32 Page, ref Int32 PerPagee, ref Int32 Count)
322        {
323            Page = response.Page;
324            PerPagee = response.PerPage;
325            Count = response.Count;
326
327            if (response.Images != null)
328            {
329                foreach (PwgImageProxyResponse respImg in response.Images)
330                {
331                    lstImage.Add(ConvertProxyResponseToDTO(respImg));
332                }
333            }
334        }
335
336        /// <summary>
337        /// private: convert response to dto object
338        /// </summary>
339        /// <param name="response"></param>
340        /// <param name="session"></param>
341        internal static PwgImageInfo ConvertProxyResponseToDTO(PwgImageInfoImageProxyResponse response)
342        {
343            PwgImageInfo returnValue = null;
344
345            try
346            {
347                PwgImage pwgImg = ConvertProxyResponseToDTO(response as PwgImageProxyResponse as PwgImageProxyResponse);
348                returnValue = new PwgImageInfo(){   Id           = pwgImg.Id,
349                                                    Width        = pwgImg.Width,
350                                                    Height       = pwgImg.Height,
351                                                    File         = pwgImg.File ,
352                                                    UrlThunb     = pwgImg.UrlThunb,
353                                                    UrlElement   = pwgImg.UrlElement,
354                                                    UrlHighDef   = pwgImg.UrlHighDef,
355                                                    Counter      = pwgImg.Counter,
356                                                    CreatingDate = pwgImg.CreatingDate,
357                                                    AvailableDate= pwgImg.AvailableDate,
358                                                    Tags         = pwgImg.Tags,
359                                                    Categories   = pwgImg.Categories};
360
361               returnValue.ExtThumbail       = response.ExtThumbail       ;
362               returnValue.Author            = response.Author            ;
363               returnValue.ImageFileSize     = response.ImageFileSize     ;
364               returnValue.hasHighDef        = response.hasHighDef        ;
365               returnValue.HighDefFileSize   = response.HighDefFileSize   ;
366               returnValue.ConfidentialLevel = response.ConfidentialLevel ;
367               returnValue.md5Sum            = response.md5Sum            ;
368               returnValue.UserIdAddedBy     = response.UserIdAddedBy     ;
369               returnValue.Name              = response.Name              ;
370               
371                if (response.DateUpdateMetaData != null)
372               {
373                   DateTime tryDate;
374                   if (DateTime.TryParse(response.DateUpdateMetaData, out tryDate))
375                   {
376                       returnValue.DateUpdateMetaData = tryDate;
377                   }
378               }
379
380               if (response.ImageRate != null)
381               {
382                    returnValue.ImageRate = ConvertProxyResponseToDTO(response.ImageRate);
383               }
384               if (response.ImageComments != null)
385               {
386                    returnValue.ImageComments = ConvertProxyResponseToDTO(response.ImageComments);
387               }
388               if (response.PostCommentSecurityData != null)
389               {
390                   returnValue.PostCommenSecurityData = ConvertProxyResponseToDTO(response.PostCommentSecurityData);
391               }
392            }
393            catch (Exception ex)
394            {
395                throw new PwgServiceException("ConvertProxyResponseToDTO : a error is raised when converting PwgTagProxyResponse.", ex);
396            }
397
398            return returnValue;
399        }
400
401        /// <summary>
402        /// private: convert response to dto object
403        /// </summary>
404        /// <param name="response"></param>
405        /// <param name="session"></param>
406        internal static PwgPostCommentSecurityInfo ConvertProxyResponseToDTO(PwgPostCommentSecurityInfo response)
407        {
408            PwgPostCommentSecurityInfo returnValue = new PwgPostCommentSecurityInfo();
409
410            returnValue.Author = response.Author;
411            returnValue.AllowPostKey = response.AllowPostKey;
412
413            return returnValue;
414        }
415
416        /// <summary>
417        /// private: convert response to dto object
418        /// </summary>
419        /// <param name="response"></param>
420        /// <param name="session"></param>
421        internal static PwgCommentPage ConvertProxyResponseToDTO(PwgCommentPageProxyResponse response)
422        {
423            PwgCommentPage returnValue = new PwgCommentPage();
424   
425            returnValue.Page = response.Page;
426            returnValue.PerPage = response.PerPage;
427            returnValue.CountOf = response.CountOf;
428            returnValue.TotalCommentNumber = response.TotalCommentNumber;
429
430            //PwgCommentPage inherit List<PwgComment>
431            if (response.Comments != null)
432            {
433                foreach (PwgCommentProxyResponse respComment in response.Comments)
434                {
435                    PwgComment pwgCom = ConvertProxyResponseToDTO(respComment);
436                    if (pwgCom != null)
437                    {
438                        returnValue.Add(pwgCom);
439                    }
440                }
441            }
442
443            return returnValue;
444        }
445
446        /// <summary>
447        /// private: convert response to dto object
448        /// </summary>
449        /// <param name="response"></param>
450        /// <param name="session"></param>
451        internal static PwgComment ConvertProxyResponseToDTO(PwgCommentProxyResponse response)
452        {
453            PwgComment returnValue = new PwgComment();
454
455            returnValue.Id          = response.Id;         
456            returnValue.CommentDate = response.CommentDate;
457            returnValue.Author      = response.Author;
458            returnValue.Content     = response.Content;     
459
460            return returnValue;
461        }
462
463        /// <summary>
464        /// private: convert response to dto object
465        /// </summary>
466        /// <param name="response"></param>
467        /// <param name="session"></param>
468        internal static PwgImage ConvertProxyResponseToDTO(PwgImageProxyResponse response)
469        {
470            PwgImage returnValue = new PwgImage();
471
472            try
473            {
474                returnValue.Counter = response.Counter;
475                returnValue.Id = response.Id;
476                returnValue.File = response.File;
477                returnValue.Height = response.Height;
478                returnValue.Width = response.Width;
479
480                if (response.AvailableDate != null)
481                {
482                    DateTime tryDate;
483                    if (DateTime.TryParse(response.AvailableDate, out tryDate))
484                    {
485                        returnValue.AvailableDate = tryDate;
486                    }
487                }
488
489                if (response.CreatingDate != null)
490                {
491                    DateTime tryDate;
492                    if (DateTime.TryParse(response.CreatingDate, out tryDate))
493                    {
494                        returnValue.CreatingDate = tryDate;
495                    }
496                }
497
498                if (String.IsNullOrEmpty(response.UrlElement))
499                {
500                    returnValue.UrlElement = null;
501                }
502                else
503                {
504                    returnValue.UrlElement = (new UriBuilder(response.UrlElement)).Uri;
505                }
506
507                if (String.IsNullOrEmpty(response.UrlThunb))
508                {
509                    returnValue.UrlThunb = null;
510                }
511                else
512                {
513                    returnValue.UrlThunb = (new UriBuilder(response.UrlThunb)).Uri;
514                }
515
516                if (String.IsNullOrEmpty(response.UrlHighDef))
517                {
518                    returnValue.UrlHighDef = null;
519                }
520                else
521                {
522                    returnValue.UrlHighDef = (new UriBuilder(response.UrlHighDef)).Uri;
523                }
524
525                returnValue.Tags = new List<PwgTag>();
526                if (response.Tags != null)
527                {
528                    foreach (PwgTagProxyResponse tagResp in response.Tags)
529                    {
530                        PwgTag aTag = PwgTagsService.ConvertProxyResponseToDTO(tagResp);
531                        returnValue.Tags.Add(aTag);
532                    }
533                }
534
535                returnValue.Categories = new List<PwgCategory>();
536                if (response.Categories != null)
537                {
538                    foreach (PwgCategoryProxyResponse catResp in response.Categories)
539                    {
540                        PwgCategory aCat = PwgCategoriesService.ConvertProxyResponseToDTO(catResp);
541                        returnValue.Categories.Add(aCat);
542                    }
543                }
544
545            }
546            catch (Exception ex)
547            {
548                throw new PwgServiceException("ConvertProxyResponseToDTO : a error is raised when converting PwgTagProxyResponse.", ex);
549            }
550
551            return returnValue;
552        }
553    }
554}
Note: See TracBrowser for help on using the repository browser.