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