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