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

Last change on this file since 12336 was 12336, checked in by bayral, 12 years ago

rename category to Albums

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