1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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.FileUtil;
34  import com.liferay.portal.kernel.util.GetterUtil;
35  import com.liferay.portal.kernel.util.OrderByComparator;
36  import com.liferay.portal.kernel.util.PropsKeys;
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.PropsValues;
46  import com.liferay.portlet.expando.model.ExpandoBridge;
47  import com.liferay.portlet.imagegallery.DuplicateImageNameException;
48  import com.liferay.portlet.imagegallery.ImageNameException;
49  import com.liferay.portlet.imagegallery.ImageSizeException;
50  import com.liferay.portlet.imagegallery.NoSuchImageException;
51  import com.liferay.portlet.imagegallery.model.IGFolder;
52  import com.liferay.portlet.imagegallery.model.IGImage;
53  import com.liferay.portlet.imagegallery.model.impl.IGImageImpl;
54  import com.liferay.portlet.imagegallery.service.base.IGImageLocalServiceBaseImpl;
55  import com.liferay.portlet.imagegallery.social.IGActivityKeys;
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.ByteArrayOutputStream;
66  import java.io.File;
67  import java.io.IOException;
68  import java.io.InputStream;
69  
70  import java.util.Date;
71  import java.util.List;
72  
73  import javax.imageio.ImageIO;
74  
75  /**
76   * <a href="IGImageLocalServiceImpl.java.html"><b><i>View Source</i></b></a>
77   *
78   * @author Brian Wing Shun Chan
79   * @author Raymond Augé
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             // Social
221 
222             socialActivityLocalService.addActivity(
223                 userId, image.getGroupId(), IGImage.class.getName(), imageId,
224                 IGActivityKeys.ADD_IMAGE, StringPool.BLANK, 0);
225 
226             // Tags
227 
228             updateTagsAsset(
229                 userId, image, serviceContext.getTagsCategories(),
230                 serviceContext.getTagsEntries());
231 
232             // Indexer
233 
234             reIndex(image);
235 
236             return image;
237         }
238         catch (IOException ioe) {
239             throw new ImageSizeException(ioe);
240         }
241     }
242 
243     public IGImage addImage(
244             String uuid, long userId, long folderId, String name,
245             String description, String fileName, InputStream is,
246             String contentType, ServiceContext serviceContext)
247         throws PortalException, SystemException {
248 
249         try {
250             byte[] bytes = FileUtil.getBytes(is);
251 
252             return addImage(
253                 uuid, userId, folderId, name, description, fileName, bytes,
254                 contentType, serviceContext);
255         }
256         catch (IOException ioe) {
257             throw new SystemException(ioe);
258         }
259     }
260 
261     public void addImageResources(
262             long imageId, boolean addCommunityPermissions,
263             boolean addGuestPermissions)
264         throws PortalException, SystemException {
265 
266         IGImage image = igImagePersistence.findByPrimaryKey(imageId);
267 
268         addImageResources(image, addCommunityPermissions, addGuestPermissions);
269     }
270 
271     public void addImageResources(
272             IGImage image, boolean addCommunityPermissions,
273             boolean addGuestPermissions)
274         throws PortalException, SystemException {
275 
276         resourceLocalService.addResources(
277             image.getCompanyId(), image.getGroupId(), image.getUserId(),
278             IGImage.class.getName(), image.getImageId(), false,
279             addCommunityPermissions, addGuestPermissions);
280     }
281 
282     public void addImageResources(
283             long imageId, String[] communityPermissions,
284             String[] guestPermissions)
285         throws PortalException, SystemException {
286 
287         IGImage image = igImagePersistence.findByPrimaryKey(imageId);
288 
289         addImageResources(image, communityPermissions, guestPermissions);
290     }
291 
292     public void addImageResources(
293             IGImage image, String[] communityPermissions,
294             String[] guestPermissions)
295         throws PortalException, SystemException {
296 
297         resourceLocalService.addModelResources(
298             image.getCompanyId(), image.getGroupId(), image.getUserId(),
299             IGImage.class.getName(), image.getImageId(), communityPermissions,
300             guestPermissions);
301     }
302 
303     public void deleteImage(long imageId)
304         throws PortalException, SystemException {
305 
306         IGImage image = igImagePersistence.findByPrimaryKey(imageId);
307 
308         deleteImage(image);
309     }
310 
311     public void deleteImage(IGImage image)
312         throws PortalException, SystemException {
313 
314         // Indexer
315 
316         try {
317             Indexer.deleteImage(image.getCompanyId(), image.getImageId());
318         }
319         catch (SearchException se) {
320             _log.error("Deleting index " + image.getImageId(), se);
321         }
322 
323         // Tags
324 
325         tagsAssetLocalService.deleteAsset(
326             IGImage.class.getName(), image.getImageId());
327 
328         // Social
329 
330         socialActivityLocalService.deleteActivities(
331             IGImage.class.getName(), image.getImageId());
332 
333         // Expando
334 
335         expandoValueLocalService.deleteValues(
336             IGImage.class.getName(), image.getImageId());
337 
338         // Resources
339 
340         resourceLocalService.deleteResource(
341             image.getCompanyId(), IGImage.class.getName(),
342             ResourceConstants.SCOPE_INDIVIDUAL, image.getImageId());
343 
344         // Images
345 
346         imageLocalService.deleteImage(image.getSmallImageId());
347         imageLocalService.deleteImage(image.getLargeImageId());
348 
349         // Image
350 
351         igImagePersistence.remove(image);
352     }
353 
354     public void deleteImages(long folderId)
355         throws PortalException, SystemException {
356 
357         List<IGImage> images = igImagePersistence.findByFolderId(folderId);
358 
359         for (IGImage image : images) {
360             deleteImage(image);
361         }
362     }
363 
364     public int getFoldersImagesCount(List<Long> folderIds)
365         throws SystemException {
366 
367         return igImageFinder.countByFolderIds(folderIds);
368     }
369 
370     public List<IGImage> getGroupImages(long groupId, int start, int end)
371         throws SystemException {
372 
373         return igImagePersistence.findByGroupId(
374             groupId, start, end, new ImageModifiedDateComparator());
375     }
376 
377     public List<IGImage> getGroupImages(
378             long groupId, long userId, int start, int end)
379         throws SystemException {
380 
381         OrderByComparator orderByComparator = new ImageModifiedDateComparator();
382 
383         if (userId <= 0) {
384             return igImagePersistence.findByGroupId(
385                 groupId, start, end, orderByComparator);
386         }
387         else {
388             return igImagePersistence.findByG_U(
389                 groupId, userId, start, end, orderByComparator);
390         }
391     }
392 
393     public int getGroupImagesCount(long groupId) throws SystemException {
394         return igImagePersistence.countByGroupId(groupId);
395     }
396 
397     public int getGroupImagesCount(long groupId, long userId)
398         throws SystemException {
399 
400         if (userId <= 0) {
401             return igImagePersistence.countByGroupId(groupId);
402         }
403         else {
404             return igImagePersistence.countByG_U(groupId, userId);
405         }
406     }
407 
408     public IGImage getImage(long imageId)
409         throws PortalException, SystemException {
410 
411         return igImagePersistence.findByPrimaryKey(imageId);
412     }
413 
414     public IGImage getImageByCustom1ImageId(long custom1ImageId)
415         throws PortalException, SystemException {
416 
417         return igImagePersistence.findByCustom1ImageId(custom1ImageId);
418     }
419 
420     public IGImage getImageByCustom2ImageId(long custom2ImageId)
421         throws PortalException, SystemException {
422 
423         return igImagePersistence.findByCustom2ImageId(custom2ImageId);
424     }
425 
426     public IGImage getImageByFolderIdAndNameWithExtension(
427             long folderId, String nameWithExtension)
428         throws PortalException, SystemException {
429 
430         String name = FileUtil.stripExtension(nameWithExtension);
431 
432         List<IGImage> images = igImagePersistence.findByF_N(folderId, name);
433 
434         if ((images.size() <= 0) && Validator.isNumber(name)) {
435             long imageId = GetterUtil.getLong(name);
436 
437             IGImage image = igImagePersistence.fetchByPrimaryKey(imageId);
438 
439             if (image != null) {
440                 images.add(image);
441             }
442         }
443 
444         for (IGImage image : images) {
445             if (nameWithExtension.equals(image.getNameWithExtension())) {
446                 return image;
447             }
448         }
449 
450         throw new NoSuchImageException();
451     }
452 
453     public IGImage getImageByLargeImageId(long largeImageId)
454         throws PortalException, SystemException {
455 
456         return igImagePersistence.findByLargeImageId(largeImageId);
457     }
458 
459     public IGImage getImageBySmallImageId(long smallImageId)
460         throws PortalException, SystemException {
461 
462         return igImagePersistence.findBySmallImageId(smallImageId);
463     }
464 
465     public IGImage getImageByUuidAndGroupId(String uuid, long groupId)
466         throws PortalException, SystemException {
467 
468         return igImagePersistence.findByUUID_G(uuid, groupId);
469     }
470 
471     public List<IGImage> getImages(long folderId) throws SystemException {
472         return igImagePersistence.findByFolderId(folderId);
473     }
474 
475     public List<IGImage> getImages(long folderId, int start, int end)
476         throws SystemException {
477 
478         return igImagePersistence.findByFolderId(folderId, start, end);
479     }
480 
481     public List<IGImage> getImages(
482             long folderId, int start, int end, OrderByComparator obc)
483         throws SystemException {
484 
485         return igImagePersistence.findByFolderId(folderId, start, end, obc);
486     }
487 
488     public int getImagesCount(long folderId) throws SystemException {
489         return igImagePersistence.countByFolderId(folderId);
490     }
491 
492     public List<IGImage> getNoAssetImages() throws SystemException {
493         return igImageFinder.findByNoAssets();
494     }
495 
496     public void reIndex(long imageId) throws SystemException {
497         if (SearchEngineUtil.isIndexReadOnly()) {
498             return;
499         }
500 
501         IGImage image = igImagePersistence.fetchByPrimaryKey(imageId);
502 
503         if (image == null) {
504             return;
505         }
506 
507         reIndex(image);
508     }
509 
510     public void reIndex(IGImage image) throws SystemException {
511         long companyId = image.getCompanyId();
512         long groupId = image.getGroupId();
513         long folderId = image.getFolderId();
514         long imageId = image.getImageId();
515         String name = image.getName();
516         String description = image.getDescription();
517         Date modifiedDate = image.getModifiedDate();
518 
519         String[] tagsCategories = tagsEntryLocalService.getEntryNames(
520             IGImage.class.getName(), imageId,
521             TagsEntryConstants.FOLKSONOMY_CATEGORY);
522         String[] tagsEntries = tagsEntryLocalService.getEntryNames(
523             IGImage.class.getName(), imageId);
524 
525         ExpandoBridge expandoBridge = image.getExpandoBridge();
526 
527         try {
528             Indexer.updateImage(
529                 companyId, groupId, folderId, imageId, name, description,
530                 modifiedDate, tagsCategories, tagsEntries, expandoBridge);
531         }
532         catch (SearchException se) {
533             _log.error("Reindexing " + imageId, se);
534         }
535     }
536 
537     public IGImage updateImage(
538             long userId, long imageId, long folderId, String name,
539             String description, byte[] bytes, String contentType,
540             ServiceContext serviceContext)
541         throws PortalException, SystemException {
542 
543         try {
544 
545             // Image
546 
547             IGImage image = igImagePersistence.findByPrimaryKey(imageId);
548 
549             IGFolder folder = getFolder(image, folderId);
550 
551             RenderedImage renderedImage = null;
552 
553             if (bytes != null) {
554                 renderedImage = ImageProcessorUtil.read(
555                     bytes).getRenderedImage();
556 
557                 validate(bytes);
558             }
559 
560             if (Validator.isNotNull(name) && !name.equals(image.getName())) {
561                 String nameWithExtension = IGImageImpl.getNameWithExtension(
562                     name, image.getImageType());
563 
564                 validate(folderId, nameWithExtension);
565             }
566             else {
567                 name = image.getName();
568             }
569 
570             image.setModifiedDate(new Date());
571             image.setFolderId(folder.getFolderId());
572             image.setName(name);
573             image.setDescription(description);
574 
575             igImagePersistence.update(image, false);
576 
577             // Images
578 
579             if (renderedImage != null) {
580                 saveImages(
581                     image.getLargeImageId(), renderedImage,
582                     image.getSmallImageId(), image.getCustom1ImageId(),
583                     image.getCustom2ImageId(), bytes, contentType);
584             }
585 
586             // Expando
587 
588             ExpandoBridge expandoBridge = image.getExpandoBridge();
589 
590             expandoBridge.setAttributes(serviceContext);
591 
592             // Social
593 
594             socialActivityLocalService.addActivity(
595                 userId, image.getGroupId(), IGImage.class.getName(), imageId,
596                 IGActivityKeys.UPDATE_IMAGE, StringPool.BLANK, 0);
597 
598             // Tags
599 
600             String[] tagsCategories = serviceContext.getTagsCategories();
601             String[] tagsEntries = serviceContext.getTagsEntries();
602 
603             updateTagsAsset(userId, image, tagsCategories, tagsEntries);
604 
605             // Indexer
606 
607             reIndex(image);
608 
609             return image;
610         }
611         catch (IOException ioe) {
612             throw new ImageSizeException(ioe);
613         }
614     }
615 
616     public IGImage updateImage(
617             long userId, long imageId, long folderId, String name,
618             String description, File file, String contentType,
619             ServiceContext serviceContext)
620         throws PortalException, SystemException {
621 
622         try {
623             byte[] bytes = null;
624 
625             if ((file != null) && file.exists()) {
626                 bytes = FileUtil.getBytes(file);
627             }
628 
629             return updateImage(
630                 userId, imageId, folderId, name, description, bytes,
631                 contentType, serviceContext);
632         }
633         catch (IOException ioe) {
634             throw new SystemException(ioe);
635         }
636     }
637 
638     public IGImage updateImage(
639             long userId, long imageId, long folderId, String name,
640             String description, InputStream is, String contentType,
641             ServiceContext serviceContext)
642         throws PortalException, SystemException {
643 
644         try {
645             byte[] bytes = null;
646 
647             if (is != null) {
648                 bytes = FileUtil.getBytes(is);
649             }
650 
651             return updateImage(
652                 userId, imageId, folderId, name, description, bytes,
653                 contentType, serviceContext);
654         }
655         catch (IOException ioe) {
656             throw new SystemException(ioe);
657         }
658     }
659 
660     public void updateTagsAsset(
661             long userId, IGImage image, String[] tagsCategories,
662             String[] tagsEntries)
663         throws PortalException, SystemException {
664 
665         Image largeImage = imageLocalService.getImage(image.getLargeImageId());
666 
667         if (largeImage == null) {
668             return;
669         }
670 
671         tagsAssetLocalService.updateAsset(
672             userId, image.getGroupId(), IGImage.class.getName(),
673             image.getImageId(), tagsCategories, tagsEntries, true, null, null,
674             null, null, largeImage.getType(), image.getName(),
675             image.getDescription(), null, null, largeImage.getHeight(),
676             largeImage.getWidth(), null, false);
677     }
678 
679     protected IGFolder getFolder(IGImage image, long folderId)
680         throws PortalException, SystemException {
681 
682         if (image.getFolderId() != folderId) {
683             IGFolder oldFolder = igFolderPersistence.findByPrimaryKey(
684                 image.getFolderId());
685 
686             IGFolder newFolder = igFolderPersistence.fetchByPrimaryKey(
687                 folderId);
688 
689             if ((newFolder == null) ||
690                 (oldFolder.getGroupId() != newFolder.getGroupId())) {
691 
692                 folderId = image.getFolderId();
693             }
694         }
695 
696         return igFolderPersistence.findByPrimaryKey(folderId);
697     }
698 
699     protected void saveImages(
700             long largeImageId, RenderedImage renderedImage, long smallImageId,
701             long custom1ImageId, long custom2ImageId, byte[] bytes,
702             String contentType)
703         throws PortalException, SystemException {
704 
705         try {
706 
707             // Image
708 
709             imageLocalService.updateImage(largeImageId, bytes);
710 
711             // Thumbnail and custom sizes
712 
713             saveScaledImage(
714                 renderedImage, smallImageId, contentType,
715                 PrefsPropsUtil.getInteger(
716                     PropsKeys.IG_IMAGE_THUMBNAIL_MAX_DIMENSION));
717 
718             if (custom1ImageId > 0) {
719                 saveScaledImage(
720                     renderedImage, custom1ImageId, contentType,
721                     PropsValues.IG_IMAGE_CUSTOM_1_MAX_DIMENSION);
722             }
723 
724             if (custom2ImageId > 0) {
725                 saveScaledImage(
726                     renderedImage, custom2ImageId, contentType,
727                     PropsValues.IG_IMAGE_CUSTOM_2_MAX_DIMENSION);
728             }
729         }
730         catch (IOException ioe) {
731             throw new SystemException(ioe);
732         }
733     }
734 
735     protected void saveScaledImage(
736             RenderedImage renderedImage, long imageId, String contentType,
737             int dimension)
738         throws IOException, PortalException, SystemException {
739 
740         RenderedImage thumbnail = ImageProcessorUtil.scale(
741             renderedImage, dimension, dimension);
742 
743         ByteArrayOutputStream baos = new ByteArrayOutputStream();
744 
745         if (contentType.indexOf("bmp") != -1) {
746             ImageEncoder encoder = ImageCodec.createImageEncoder(
747                 "BMP", baos, null);
748 
749             encoder.encode(thumbnail);
750         }
751         else if (contentType.indexOf("gif") != -1) {
752             ImageProcessorUtil.encodeGIF(thumbnail, baos);
753         }
754         else if (contentType.indexOf("jpg") != -1 ||
755                  contentType.indexOf("jpeg") != -1) {
756 
757             ImageIO.write(thumbnail, "jpeg", baos);
758         }
759         else if (contentType.indexOf("png") != -1) {
760             ImageIO.write(thumbnail, "png", baos);
761         }
762         else if (contentType.indexOf("tif") != -1) {
763             ImageEncoder encoder = ImageCodec.createImageEncoder(
764                 "TIFF", baos, null);
765 
766             encoder.encode(thumbnail);
767         }
768 
769         imageLocalService.updateImage(imageId, baos.toByteArray());
770     }
771 
772     protected void validate(byte[] bytes)
773         throws ImageSizeException, SystemException {
774 
775         if ((PrefsPropsUtil.getLong(PropsKeys.IG_IMAGE_MAX_SIZE) > 0) &&
776             ((bytes == null) ||
777              (bytes.length >
778                  PrefsPropsUtil.getLong(PropsKeys.IG_IMAGE_MAX_SIZE)))) {
779 
780             throw new ImageSizeException();
781         }
782     }
783 
784     protected void validate(long folderId, String nameWithExtension)
785         throws PortalException, SystemException {
786 
787         if ((nameWithExtension.indexOf("\\\\") != -1) ||
788             (nameWithExtension.indexOf("//") != -1) ||
789             (nameWithExtension.indexOf(":") != -1) ||
790             (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 
801             throw new ImageNameException();
802         }
803 
804         boolean validImageExtension = false;
805 
806         String[] imageExtensions = PrefsPropsUtil.getStringArray(
807             PropsKeys.IG_IMAGE_EXTENSIONS, StringPool.COMMA);
808 
809         for (int i = 0; i < imageExtensions.length; i++) {
810             if (StringPool.STAR.equals(imageExtensions[i]) ||
811                 StringUtil.endsWith(nameWithExtension, imageExtensions[i])) {
812 
813                 validImageExtension = true;
814 
815                 break;
816             }
817         }
818 
819         if (!validImageExtension) {
820             throw new ImageNameException();
821         }
822 
823         String name = FileUtil.stripExtension(nameWithExtension);
824         String imageType = FileUtil.getExtension(nameWithExtension);
825 
826         List<IGImage> images = igImagePersistence.findByF_N(folderId, name);
827 
828         if (imageType.equals("jpeg")) {
829             imageType = ImageProcessor.TYPE_JPEG;
830         }
831         else if (imageType.equals("tif")) {
832             imageType = ImageProcessor.TYPE_TIFF;
833         }
834 
835         for (IGImage image : images) {
836             if (imageType.equals(image.getImageType())) {
837                 throw new DuplicateImageNameException();
838             }
839         }
840     }
841 
842     protected void validate(
843             long folderId, String nameWithExtension, String fileName,
844             byte[] bytes)
845         throws PortalException, SystemException {
846 
847         if (Validator.isNotNull(fileName)) {
848             String extension = FileUtil.getExtension(fileName);
849 
850             if (Validator.isNull(nameWithExtension)) {
851                 nameWithExtension = fileName;
852             }
853             else if (!StringUtil.endsWith(nameWithExtension, extension)) {
854                 throw new ImageNameException();
855             }
856         }
857 
858         validate(folderId, nameWithExtension);
859         validate(bytes);
860     }
861 
862     private static Log _log =
863         LogFactoryUtil.getLog(IGImageLocalServiceImpl.class);
864 
865 }