1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portlet.imagegallery.service.impl;
16  
17  import com.liferay.portal.PortalException;
18  import com.liferay.portal.SystemException;
19  import com.liferay.portal.kernel.image.ImageProcessor;
20  import com.liferay.portal.kernel.image.ImageProcessorUtil;
21  import com.liferay.portal.kernel.log.Log;
22  import com.liferay.portal.kernel.log.LogFactoryUtil;
23  import com.liferay.portal.kernel.search.SearchEngineUtil;
24  import com.liferay.portal.kernel.search.SearchException;
25  import com.liferay.portal.kernel.util.FileUtil;
26  import com.liferay.portal.kernel.util.GetterUtil;
27  import com.liferay.portal.kernel.util.MimeTypesUtil;
28  import com.liferay.portal.kernel.util.OrderByComparator;
29  import com.liferay.portal.kernel.util.PropsKeys;
30  import com.liferay.portal.kernel.util.StringPool;
31  import com.liferay.portal.kernel.util.StringUtil;
32  import com.liferay.portal.kernel.util.Validator;
33  import com.liferay.portal.model.Image;
34  import com.liferay.portal.model.ResourceConstants;
35  import com.liferay.portal.model.User;
36  import com.liferay.portal.service.ServiceContext;
37  import com.liferay.portal.util.PrefsPropsUtil;
38  import com.liferay.portal.util.PropsValues;
39  import com.liferay.portlet.expando.model.ExpandoBridge;
40  import com.liferay.portlet.imagegallery.DuplicateImageNameException;
41  import com.liferay.portlet.imagegallery.ImageNameException;
42  import com.liferay.portlet.imagegallery.ImageSizeException;
43  import com.liferay.portlet.imagegallery.NoSuchImageException;
44  import com.liferay.portlet.imagegallery.model.IGFolder;
45  import com.liferay.portlet.imagegallery.model.IGImage;
46  import com.liferay.portlet.imagegallery.model.impl.IGImageImpl;
47  import com.liferay.portlet.imagegallery.service.base.IGImageLocalServiceBaseImpl;
48  import com.liferay.portlet.imagegallery.social.IGActivityKeys;
49  import com.liferay.portlet.imagegallery.util.Indexer;
50  import com.liferay.portlet.imagegallery.util.comparator.ImageModifiedDateComparator;
51  import com.liferay.portlet.tags.model.TagsEntryConstants;
52  
53  import java.awt.image.RenderedImage;
54  
55  import java.io.File;
56  import java.io.IOException;
57  import java.io.InputStream;
58  
59  import java.util.Date;
60  import java.util.List;
61  
62  /**
63   * <a href="IGImageLocalServiceImpl.java.html"><b><i>View Source</i></b></a>
64   *
65   * @author Brian Wing Shun Chan
66   * @author Raymond Augé
67   */
68  public class IGImageLocalServiceImpl extends IGImageLocalServiceBaseImpl {
69  
70      public IGImage addImage(
71              long userId, long folderId, String name, String description,
72              File file, String contentType, ServiceContext serviceContext)
73          throws PortalException, SystemException {
74  
75          return addImage(
76              null, userId, folderId, name, description, file, contentType,
77              serviceContext);
78      }
79  
80      public IGImage addImage(
81              long userId, long folderId, String name, String description,
82              String fileName, byte[] bytes, String contentType,
83              ServiceContext serviceContext)
84          throws PortalException, SystemException {
85  
86          return addImage(
87              null, userId, folderId, name, description, fileName, bytes,
88              contentType, serviceContext);
89      }
90  
91      public IGImage addImage(
92              long userId, long folderId, String name, String description,
93              String fileName, InputStream is, String contentType,
94              ServiceContext serviceContext)
95          throws PortalException, SystemException {
96  
97          return addImage(
98              null, userId, folderId, name, description, fileName, is,
99              contentType, serviceContext);
100     }
101 
102     public IGImage addImage(
103             String uuid, long userId, long folderId, String name,
104             String description, File file, String contentType,
105             ServiceContext serviceContext)
106         throws PortalException, SystemException {
107 
108         try {
109             String fileName = file.getName();
110             byte[] bytes = FileUtil.getBytes(file);
111 
112             return addImage(
113                 uuid, userId, folderId, name, description, fileName, bytes,
114                 contentType, serviceContext);
115         }
116         catch (IOException ioe) {
117             throw new SystemException(ioe);
118         }
119     }
120 
121     public IGImage addImage(
122             String uuid, long userId, long folderId, String name,
123             String description, String fileName, byte[] bytes,
124             String contentType, ServiceContext serviceContext)
125         throws PortalException, SystemException {
126 
127         try {
128 
129             // Image
130 
131             String extension = FileUtil.getExtension(fileName);
132 
133             if (Validator.isNotNull(name) &&
134                 StringUtil.endsWith(name, extension)) {
135 
136                 name = FileUtil.stripExtension(name);
137             }
138 
139             String nameWithExtension = name + StringPool.PERIOD + extension;
140 
141             validate(folderId, nameWithExtension, fileName, bytes);
142 
143             User user = userPersistence.findByPrimaryKey(userId);
144             IGFolder folder = igFolderPersistence.findByPrimaryKey(folderId);
145             RenderedImage renderedImage = ImageProcessorUtil.read(
146                 bytes).getRenderedImage();
147             Date now = new Date();
148 
149             long imageId = counterLocalService.increment();
150 
151             if (Validator.isNull(name)) {
152                 name = String.valueOf(imageId);
153             }
154 
155             IGImage image = igImagePersistence.create(imageId);
156 
157             image.setUuid(uuid);
158             image.setGroupId(folder.getGroupId());
159             image.setCompanyId(user.getCompanyId());
160             image.setUserId(user.getUserId());
161             image.setCreateDate(serviceContext.getCreateDate(now));
162             image.setModifiedDate(serviceContext.getModifiedDate(now));
163             image.setFolderId(folderId);
164             image.setName(name);
165             image.setDescription(description);
166             image.setSmallImageId(counterLocalService.increment());
167             image.setLargeImageId(counterLocalService.increment());
168 
169             if (PropsValues.IG_IMAGE_CUSTOM_1_MAX_DIMENSION > 0) {
170                 image.setCustom1ImageId(counterLocalService.increment());
171             }
172 
173             if (PropsValues.IG_IMAGE_CUSTOM_2_MAX_DIMENSION > 0) {
174                 image.setCustom2ImageId(counterLocalService.increment());
175             }
176 
177             image.setExpandoBridgeAttributes(serviceContext);
178 
179             igImagePersistence.update(image, false);
180 
181             // Resources
182 
183             if (serviceContext.getAddCommunityPermissions() ||
184                 serviceContext.getAddGuestPermissions()) {
185 
186                 addImageResources(
187                     image, serviceContext.getAddCommunityPermissions(),
188                     serviceContext.getAddGuestPermissions());
189             }
190             else {
191                 addImageResources(
192                     image, serviceContext.getCommunityPermissions(),
193                     serviceContext.getGuestPermissions());
194             }
195 
196             // Images
197 
198             saveImages(
199                 image.getLargeImageId(), renderedImage, image.getSmallImageId(),
200                 image.getCustom1ImageId(), image.getCustom2ImageId(), bytes,
201                 contentType);
202 
203             // Social
204 
205             socialActivityLocalService.addActivity(
206                 userId, image.getGroupId(), IGImage.class.getName(), imageId,
207                 IGActivityKeys.ADD_IMAGE, StringPool.BLANK, 0);
208 
209             // Tags
210 
211             updateTagsAsset(
212                 userId, image, serviceContext.getTagsCategories(),
213                 serviceContext.getTagsEntries(), contentType);
214 
215             // Indexer
216 
217             reIndex(image);
218 
219             return image;
220         }
221         catch (IOException ioe) {
222             throw new ImageSizeException(ioe);
223         }
224     }
225 
226     public IGImage addImage(
227             String uuid, long userId, long folderId, String name,
228             String description, String fileName, InputStream is,
229             String contentType, ServiceContext serviceContext)
230         throws PortalException, SystemException {
231 
232         try {
233             byte[] bytes = FileUtil.getBytes(is);
234 
235             return addImage(
236                 uuid, userId, folderId, name, description, fileName, bytes,
237                 contentType, serviceContext);
238         }
239         catch (IOException ioe) {
240             throw new SystemException(ioe);
241         }
242     }
243 
244     public void addImageResources(
245             IGImage image, boolean addCommunityPermissions,
246             boolean addGuestPermissions)
247         throws PortalException, SystemException {
248 
249         resourceLocalService.addResources(
250             image.getCompanyId(), image.getGroupId(), image.getUserId(),
251             IGImage.class.getName(), image.getImageId(), false,
252             addCommunityPermissions, addGuestPermissions);
253     }
254 
255     public void addImageResources(
256             IGImage image, String[] communityPermissions,
257             String[] guestPermissions)
258         throws PortalException, SystemException {
259 
260         resourceLocalService.addModelResources(
261             image.getCompanyId(), image.getGroupId(), image.getUserId(),
262             IGImage.class.getName(), image.getImageId(), communityPermissions,
263             guestPermissions);
264     }
265 
266     public void addImageResources(
267             long imageId, boolean addCommunityPermissions,
268             boolean addGuestPermissions)
269         throws PortalException, SystemException {
270 
271         IGImage image = igImagePersistence.findByPrimaryKey(imageId);
272 
273         addImageResources(image, addCommunityPermissions, addGuestPermissions);
274     }
275 
276     public void addImageResources(
277             long imageId, String[] communityPermissions,
278             String[] guestPermissions)
279         throws PortalException, SystemException {
280 
281         IGImage image = igImagePersistence.findByPrimaryKey(imageId);
282 
283         addImageResources(image, communityPermissions, guestPermissions);
284     }
285 
286     public void deleteImage(IGImage image)
287         throws PortalException, SystemException {
288 
289         // Image
290 
291         igImagePersistence.remove(image);
292 
293         // Resources
294 
295         resourceLocalService.deleteResource(
296             image.getCompanyId(), IGImage.class.getName(),
297             ResourceConstants.SCOPE_INDIVIDUAL, image.getImageId());
298 
299         // Images
300 
301         imageLocalService.deleteImage(image.getSmallImageId());
302         imageLocalService.deleteImage(image.getLargeImageId());
303         imageLocalService.deleteImage(image.getCustom1ImageId());
304         imageLocalService.deleteImage(image.getCustom2ImageId());
305 
306         // Expando
307 
308         expandoValueLocalService.deleteValues(
309             IGImage.class.getName(), image.getImageId());
310 
311         // Social
312 
313         socialActivityLocalService.deleteActivities(
314             IGImage.class.getName(), image.getImageId());
315 
316         // Tags
317 
318         tagsAssetLocalService.deleteAsset(
319             IGImage.class.getName(), image.getImageId());
320 
321         // Indexer
322 
323         try {
324             Indexer.deleteImage(image.getCompanyId(), image.getImageId());
325         }
326         catch (SearchException se) {
327             _log.error("Deleting index " + image.getImageId(), se);
328         }
329     }
330 
331     public void deleteImage(long imageId)
332         throws PortalException, SystemException {
333 
334         IGImage image = igImagePersistence.findByPrimaryKey(imageId);
335 
336         deleteImage(image);
337     }
338 
339     public void deleteImages(long folderId)
340         throws PortalException, SystemException {
341 
342         List<IGImage> images = igImagePersistence.findByFolderId(folderId);
343 
344         for (IGImage image : images) {
345             deleteImage(image);
346         }
347     }
348 
349     public int getFoldersImagesCount(List<Long> folderIds)
350         throws SystemException {
351 
352         return igImageFinder.countByFolderIds(folderIds);
353     }
354 
355     public List<IGImage> getGroupImages(long groupId, int start, int end)
356         throws SystemException {
357 
358         return igImagePersistence.findByGroupId(
359             groupId, start, end, new ImageModifiedDateComparator());
360     }
361 
362     public List<IGImage> getGroupImages(
363             long groupId, long userId, int start, int end)
364         throws SystemException {
365 
366         OrderByComparator orderByComparator = new ImageModifiedDateComparator();
367 
368         if (userId <= 0) {
369             return igImagePersistence.findByGroupId(
370                 groupId, start, end, orderByComparator);
371         }
372         else {
373             return igImagePersistence.findByG_U(
374                 groupId, userId, start, end, orderByComparator);
375         }
376     }
377 
378     public int getGroupImagesCount(long groupId) throws SystemException {
379         return igImagePersistence.countByGroupId(groupId);
380     }
381 
382     public int getGroupImagesCount(long groupId, long userId)
383         throws SystemException {
384 
385         if (userId <= 0) {
386             return igImagePersistence.countByGroupId(groupId);
387         }
388         else {
389             return igImagePersistence.countByG_U(groupId, userId);
390         }
391     }
392 
393     public IGImage getImage(long imageId)
394         throws PortalException, SystemException {
395 
396         return igImagePersistence.findByPrimaryKey(imageId);
397     }
398 
399     public IGImage getImageByCustom1ImageId(long custom1ImageId)
400         throws PortalException, SystemException {
401 
402         return igImagePersistence.findByCustom1ImageId(custom1ImageId);
403     }
404 
405     public IGImage getImageByCustom2ImageId(long custom2ImageId)
406         throws PortalException, SystemException {
407 
408         return igImagePersistence.findByCustom2ImageId(custom2ImageId);
409     }
410 
411     public IGImage getImageByFolderIdAndNameWithExtension(
412             long folderId, String nameWithExtension)
413         throws PortalException, SystemException {
414 
415         String name = FileUtil.stripExtension(nameWithExtension);
416 
417         List<IGImage> images = igImagePersistence.findByF_N(folderId, name);
418 
419         if ((images.size() <= 0) && Validator.isNumber(name)) {
420             long imageId = GetterUtil.getLong(name);
421 
422             IGImage image = igImagePersistence.fetchByPrimaryKey(imageId);
423 
424             if (image != null) {
425                 images.add(image);
426             }
427         }
428 
429         for (IGImage image : images) {
430             if (nameWithExtension.equals(image.getNameWithExtension())) {
431                 return image;
432             }
433         }
434 
435         throw new NoSuchImageException();
436     }
437 
438     public IGImage getImageByLargeImageId(long largeImageId)
439         throws PortalException, SystemException {
440 
441         return igImagePersistence.findByLargeImageId(largeImageId);
442     }
443 
444     public IGImage getImageBySmallImageId(long smallImageId)
445         throws PortalException, SystemException {
446 
447         return igImagePersistence.findBySmallImageId(smallImageId);
448     }
449 
450     public IGImage getImageByUuidAndGroupId(String uuid, long groupId)
451         throws PortalException, SystemException {
452 
453         return igImagePersistence.findByUUID_G(uuid, groupId);
454     }
455 
456     public List<IGImage> getImages(long folderId) throws SystemException {
457         return igImagePersistence.findByFolderId(folderId);
458     }
459 
460     public List<IGImage> getImages(long folderId, int start, int end)
461         throws SystemException {
462 
463         return igImagePersistence.findByFolderId(folderId, start, end);
464     }
465 
466     public List<IGImage> getImages(
467             long folderId, int start, int end, OrderByComparator obc)
468         throws SystemException {
469 
470         return igImagePersistence.findByFolderId(folderId, start, end, obc);
471     }
472 
473     public int getImagesCount(long folderId) throws SystemException {
474         return igImagePersistence.countByFolderId(folderId);
475     }
476 
477     public List<IGImage> getNoAssetImages() throws SystemException {
478         return igImageFinder.findByNoAssets();
479     }
480 
481     public void reIndex(IGImage image) throws SystemException {
482         long companyId = image.getCompanyId();
483         long groupId = image.getGroupId();
484         long userId = image.getUserId();
485         long folderId = image.getFolderId();
486         long imageId = image.getImageId();
487         String name = image.getName();
488         String description = image.getDescription();
489         Date modifiedDate = image.getModifiedDate();
490 
491         String[] tagsCategories = tagsEntryLocalService.getEntryNames(
492             IGImage.class.getName(), imageId,
493             TagsEntryConstants.FOLKSONOMY_CATEGORY);
494         String[] tagsEntries = tagsEntryLocalService.getEntryNames(
495             IGImage.class.getName(), imageId);
496 
497         ExpandoBridge expandoBridge = image.getExpandoBridge();
498 
499         try {
500             Indexer.updateImage(
501                 companyId, groupId, userId, folderId, imageId, name,
502                 description, modifiedDate, tagsCategories, tagsEntries,
503                 expandoBridge);
504         }
505         catch (SearchException se) {
506             _log.error("Reindexing " + imageId, se);
507         }
508     }
509 
510     public void reIndex(long imageId) throws SystemException {
511         if (SearchEngineUtil.isIndexReadOnly()) {
512             return;
513         }
514 
515         IGImage image = igImagePersistence.fetchByPrimaryKey(imageId);
516 
517         if (image == null) {
518             return;
519         }
520 
521         reIndex(image);
522     }
523 
524     public IGImage updateImage(
525             long userId, long imageId, long folderId, String name,
526             String description, byte[] bytes, String contentType,
527             ServiceContext serviceContext)
528         throws PortalException, SystemException {
529 
530         try {
531 
532             // Image
533 
534             IGImage image = igImagePersistence.findByPrimaryKey(imageId);
535 
536             IGFolder folder = getFolder(image, folderId);
537 
538             RenderedImage renderedImage = null;
539 
540             if (bytes != null) {
541                 renderedImage = ImageProcessorUtil.read(
542                     bytes).getRenderedImage();
543 
544                 validate(bytes);
545             }
546 
547             if (Validator.isNotNull(name) && !name.equals(image.getName())) {
548                 String nameWithExtension = IGImageImpl.getNameWithExtension(
549                     name, image.getImageType());
550 
551                 validate(folderId, nameWithExtension);
552             }
553             else {
554                 name = image.getName();
555             }
556 
557             image.setModifiedDate(serviceContext.getModifiedDate(null));
558             image.setFolderId(folder.getFolderId());
559             image.setName(name);
560             image.setDescription(description);
561             image.setExpandoBridgeAttributes(serviceContext);
562 
563             igImagePersistence.update(image, false);
564 
565             // Images
566 
567             if (renderedImage != null) {
568                 saveImages(
569                     image.getLargeImageId(), renderedImage,
570                     image.getSmallImageId(), image.getCustom1ImageId(),
571                     image.getCustom2ImageId(), bytes, contentType);
572             }
573 
574             // Social
575 
576             socialActivityLocalService.addActivity(
577                 userId, image.getGroupId(), IGImage.class.getName(), imageId,
578                 IGActivityKeys.UPDATE_IMAGE, StringPool.BLANK, 0);
579 
580             // Tags
581 
582             String[] tagsCategories = serviceContext.getTagsCategories();
583             String[] tagsEntries = serviceContext.getTagsEntries();
584 
585             updateTagsAsset(
586                 userId, image, tagsCategories, tagsEntries, contentType);
587 
588             // Indexer
589 
590             reIndex(image);
591 
592             return image;
593         }
594         catch (IOException ioe) {
595             throw new ImageSizeException(ioe);
596         }
597     }
598 
599     public IGImage updateImage(
600             long userId, long imageId, long folderId, String name,
601             String description, File file, String contentType,
602             ServiceContext serviceContext)
603         throws PortalException, SystemException {
604 
605         try {
606             byte[] bytes = null;
607 
608             if ((file != null) && file.exists()) {
609                 bytes = FileUtil.getBytes(file);
610             }
611 
612             return updateImage(
613                 userId, imageId, folderId, name, description, bytes,
614                 contentType, serviceContext);
615         }
616         catch (IOException ioe) {
617             throw new SystemException(ioe);
618         }
619     }
620 
621     public IGImage updateImage(
622             long userId, long imageId, long folderId, String name,
623             String description, InputStream is, String contentType,
624             ServiceContext serviceContext)
625         throws PortalException, SystemException {
626 
627         try {
628             byte[] bytes = null;
629 
630             if (is != null) {
631                 bytes = FileUtil.getBytes(is);
632             }
633 
634             return updateImage(
635                 userId, imageId, folderId, name, description, bytes,
636                 contentType, serviceContext);
637         }
638         catch (IOException ioe) {
639             throw new SystemException(ioe);
640         }
641     }
642 
643     public void updateSmallImage(long smallImageId, long largeImageId)
644         throws PortalException, SystemException {
645 
646         try {
647             RenderedImage renderedImage = null;
648 
649             Image largeImage = imageLocalService.getImage(largeImageId);
650 
651             byte[] bytes = largeImage.getTextObj();
652             String contentType = largeImage.getType();
653 
654             if (bytes != null) {
655                 renderedImage = ImageProcessorUtil.read(
656                     bytes).getRenderedImage();
657 
658                 //validate(bytes);
659             }
660 
661             if (renderedImage != null) {
662                 saveScaledImage(
663                     renderedImage, smallImageId, contentType,
664                     PrefsPropsUtil.getInteger(
665                         PropsKeys.IG_IMAGE_THUMBNAIL_MAX_DIMENSION));
666             }
667         }
668         catch (IOException ioe) {
669             throw new ImageSizeException(ioe);
670         }
671     }
672 
673     /**
674      * @deprecated
675      */
676     public void updateTagsAsset(
677             long userId, IGImage image, String[] tagsCategories,
678             String[] tagsEntries)
679         throws PortalException, SystemException {
680 
681         updateTagsAsset(userId, image, tagsCategories, tagsEntries, null);
682     }
683 
684     public void updateTagsAsset(
685             long userId, IGImage image, String[] tagsCategories,
686             String[] tagsEntries, String contentType)
687         throws PortalException, SystemException {
688 
689         Image largeImage = imageLocalService.getImage(image.getLargeImageId());
690 
691         if (largeImage == null) {
692             return;
693         }
694 
695         if (contentType == null) {
696             contentType = MimeTypesUtil.getContentType(largeImage.getType());
697         }
698 
699         tagsAssetLocalService.updateAsset(
700             userId, image.getGroupId(), IGImage.class.getName(),
701             image.getImageId(), tagsCategories, tagsEntries, true, null, null,
702             null, null, contentType, image.getName(), image.getDescription(),
703             null, null, largeImage.getHeight(), largeImage.getWidth(), null,
704             false);
705     }
706 
707     protected IGFolder getFolder(IGImage image, long folderId)
708         throws PortalException, SystemException {
709 
710         if (image.getFolderId() != folderId) {
711             IGFolder oldFolder = igFolderPersistence.findByPrimaryKey(
712                 image.getFolderId());
713 
714             IGFolder newFolder = igFolderPersistence.fetchByPrimaryKey(
715                 folderId);
716 
717             if ((newFolder == null) ||
718                 (oldFolder.getGroupId() != newFolder.getGroupId())) {
719 
720                 folderId = image.getFolderId();
721             }
722         }
723 
724         return igFolderPersistence.findByPrimaryKey(folderId);
725     }
726 
727     protected void saveImages(
728             long largeImageId, RenderedImage renderedImage, long smallImageId,
729             long custom1ImageId, long custom2ImageId, byte[] bytes,
730             String contentType)
731         throws PortalException, SystemException {
732 
733         try {
734 
735             // Image
736 
737             imageLocalService.updateImage(largeImageId, bytes);
738 
739             // Thumbnail and custom sizes
740 
741             saveScaledImage(
742                 renderedImage, smallImageId, contentType,
743                 PrefsPropsUtil.getInteger(
744                     PropsKeys.IG_IMAGE_THUMBNAIL_MAX_DIMENSION));
745 
746             if (custom1ImageId > 0) {
747                 saveScaledImage(
748                     renderedImage, custom1ImageId, contentType,
749                     PropsValues.IG_IMAGE_CUSTOM_1_MAX_DIMENSION);
750             }
751 
752             if (custom2ImageId > 0) {
753                 saveScaledImage(
754                     renderedImage, custom2ImageId, contentType,
755                     PropsValues.IG_IMAGE_CUSTOM_2_MAX_DIMENSION);
756             }
757         }
758         catch (IOException ioe) {
759             throw new SystemException(ioe);
760         }
761     }
762 
763     protected void saveScaledImage(
764             RenderedImage renderedImage, long imageId, String contentType,
765             int dimension)
766         throws IOException, PortalException, SystemException {
767 
768         RenderedImage thumbnail = ImageProcessorUtil.scale(
769             renderedImage, dimension, dimension);
770 
771         imageLocalService.updateImage(
772             imageId, ImageProcessorUtil.getBytes(thumbnail, contentType));
773     }
774 
775     protected void validate(byte[] bytes)
776         throws ImageSizeException, SystemException {
777 
778         if ((PrefsPropsUtil.getLong(PropsKeys.IG_IMAGE_MAX_SIZE) > 0) &&
779             ((bytes == null) ||
780              (bytes.length >
781                  PrefsPropsUtil.getLong(PropsKeys.IG_IMAGE_MAX_SIZE)))) {
782 
783             throw new ImageSizeException();
784         }
785     }
786 
787     protected void validate(long folderId, String nameWithExtension)
788         throws PortalException, SystemException {
789 
790         if ((nameWithExtension.indexOf("\\\\") != -1) ||
791             (nameWithExtension.indexOf("//") != -1) ||
792             (nameWithExtension.indexOf(":") != -1) ||
793             (nameWithExtension.indexOf("*") != -1) ||
794             (nameWithExtension.indexOf("?") != -1) ||
795             (nameWithExtension.indexOf("\"") != -1) ||
796             (nameWithExtension.indexOf("<") != -1) ||
797             (nameWithExtension.indexOf(">") != -1) ||
798             (nameWithExtension.indexOf("|") != -1) ||
799             (nameWithExtension.indexOf("[") != -1) ||
800             (nameWithExtension.indexOf("]") != -1) ||
801             (nameWithExtension.indexOf("'") != -1)) {
802 
803             throw new ImageNameException();
804         }
805 
806         boolean validImageExtension = false;
807 
808         String[] imageExtensions = PrefsPropsUtil.getStringArray(
809             PropsKeys.IG_IMAGE_EXTENSIONS, StringPool.COMMA);
810 
811         for (int i = 0; i < imageExtensions.length; i++) {
812             if (StringPool.STAR.equals(imageExtensions[i]) ||
813                 StringUtil.endsWith(nameWithExtension, imageExtensions[i])) {
814 
815                 validImageExtension = true;
816 
817                 break;
818             }
819         }
820 
821         if (!validImageExtension) {
822             throw new ImageNameException();
823         }
824 
825         String name = FileUtil.stripExtension(nameWithExtension);
826         String imageType = FileUtil.getExtension(nameWithExtension);
827 
828         List<IGImage> images = igImagePersistence.findByF_N(folderId, name);
829 
830         if (imageType.equals("jpeg")) {
831             imageType = ImageProcessor.TYPE_JPEG;
832         }
833         else if (imageType.equals("tif")) {
834             imageType = ImageProcessor.TYPE_TIFF;
835         }
836 
837         for (IGImage image : images) {
838             if (imageType.equals(image.getImageType())) {
839                 throw new DuplicateImageNameException();
840             }
841         }
842     }
843 
844     protected void validate(
845             long folderId, String nameWithExtension, String fileName,
846             byte[] bytes)
847         throws PortalException, SystemException {
848 
849         if (Validator.isNotNull(fileName)) {
850             String extension = FileUtil.getExtension(fileName);
851 
852             if (Validator.isNull(nameWithExtension)) {
853                 nameWithExtension = fileName;
854             }
855             else if (!StringUtil.endsWith(nameWithExtension, extension)) {
856                 throw new ImageNameException();
857             }
858         }
859 
860         validate(folderId, nameWithExtension);
861         validate(bytes);
862     }
863 
864     private static Log _log = LogFactoryUtil.getLog(
865         IGImageLocalServiceImpl.class);
866 
867 }