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