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