001
014
015 package com.liferay.portlet.imagegallery.service.impl;
016
017 import com.liferay.portal.kernel.exception.PortalException;
018 import com.liferay.portal.kernel.exception.SystemException;
019 import com.liferay.portal.kernel.image.ImageProcessor;
020 import com.liferay.portal.kernel.image.ImageProcessorUtil;
021 import com.liferay.portal.kernel.search.Indexer;
022 import com.liferay.portal.kernel.search.IndexerRegistryUtil;
023 import com.liferay.portal.kernel.util.ArrayUtil;
024 import com.liferay.portal.kernel.util.FileUtil;
025 import com.liferay.portal.kernel.util.GetterUtil;
026 import com.liferay.portal.kernel.util.MimeTypesUtil;
027 import com.liferay.portal.kernel.util.OrderByComparator;
028 import com.liferay.portal.kernel.util.PropsKeys;
029 import com.liferay.portal.kernel.util.StringPool;
030 import com.liferay.portal.kernel.util.StringUtil;
031 import com.liferay.portal.kernel.util.Validator;
032 import com.liferay.portal.model.Image;
033 import com.liferay.portal.model.ResourceConstants;
034 import com.liferay.portal.model.User;
035 import com.liferay.portal.service.ServiceContext;
036 import com.liferay.portal.util.PrefsPropsUtil;
037 import com.liferay.portal.util.PropsValues;
038 import com.liferay.portlet.imagegallery.DuplicateImageNameException;
039 import com.liferay.portlet.imagegallery.ImageNameException;
040 import com.liferay.portlet.imagegallery.ImageSizeException;
041 import com.liferay.portlet.imagegallery.NoSuchImageException;
042 import com.liferay.portlet.imagegallery.model.IGFolder;
043 import com.liferay.portlet.imagegallery.model.IGFolderConstants;
044 import com.liferay.portlet.imagegallery.model.IGImage;
045 import com.liferay.portlet.imagegallery.model.impl.IGImageImpl;
046 import com.liferay.portlet.imagegallery.service.base.IGImageLocalServiceBaseImpl;
047 import com.liferay.portlet.imagegallery.social.IGActivityKeys;
048 import com.liferay.portlet.imagegallery.util.comparator.ImageModifiedDateComparator;
049
050 import java.awt.image.RenderedImage;
051
052 import java.io.File;
053 import java.io.IOException;
054 import java.io.InputStream;
055
056 import java.util.Date;
057 import java.util.List;
058
059
064 public class IGImageLocalServiceImpl extends IGImageLocalServiceBaseImpl {
065
066 public IGImage addImage(
067 long userId, long groupId, long folderId, String name,
068 String description, File file, String contentType,
069 ServiceContext serviceContext)
070 throws PortalException, SystemException {
071
072 try {
073 String fileName = file.getName();
074 byte[] bytes = FileUtil.getBytes(file);
075
076 return addImage(
077 userId, groupId, folderId, name, description, fileName,
078 bytes, contentType, serviceContext);
079 }
080 catch (IOException ioe) {
081 throw new SystemException(ioe);
082 }
083 }
084
085 public IGImage addImage(
086 long userId, long groupId, long folderId, String name,
087 String description, String fileName, byte[] bytes,
088 String contentType, ServiceContext serviceContext)
089 throws PortalException, SystemException {
090
091 try {
092
093
094
095 String extension = FileUtil.getExtension(fileName);
096
097 if (Validator.isNotNull(name) &&
098 StringUtil.endsWith(name, extension)) {
099
100 name = FileUtil.stripExtension(name);
101 }
102
103 String nameWithExtension = name + StringPool.PERIOD + extension;
104
105 validate(groupId, folderId, nameWithExtension, fileName, bytes);
106
107 User user = userPersistence.findByPrimaryKey(userId);
108 RenderedImage renderedImage = ImageProcessorUtil.read(
109 bytes).getRenderedImage();
110 Date now = new Date();
111
112 long imageId = counterLocalService.increment();
113
114 if (Validator.isNull(name)) {
115 name = String.valueOf(imageId);
116 }
117
118 IGImage image = igImagePersistence.create(imageId);
119
120 image.setUuid(serviceContext.getUuid());
121 image.setGroupId(groupId);
122 image.setCompanyId(user.getCompanyId());
123 image.setUserId(user.getUserId());
124 image.setCreateDate(serviceContext.getCreateDate(now));
125 image.setModifiedDate(serviceContext.getModifiedDate(now));
126 image.setFolderId(folderId);
127 image.setName(name);
128 image.setDescription(description);
129 image.setSmallImageId(counterLocalService.increment());
130 image.setLargeImageId(counterLocalService.increment());
131
132 if (PropsValues.IG_IMAGE_CUSTOM_1_MAX_DIMENSION > 0) {
133 image.setCustom1ImageId(counterLocalService.increment());
134 }
135
136 if (PropsValues.IG_IMAGE_CUSTOM_2_MAX_DIMENSION > 0) {
137 image.setCustom2ImageId(counterLocalService.increment());
138 }
139
140 image.setExpandoBridgeAttributes(serviceContext);
141
142 igImagePersistence.update(image, false);
143
144
145
146 if (serviceContext.getAddCommunityPermissions() ||
147 serviceContext.getAddGuestPermissions()) {
148
149 addImageResources(
150 image, serviceContext.getAddCommunityPermissions(),
151 serviceContext.getAddGuestPermissions());
152 }
153 else {
154 addImageResources(
155 image, serviceContext.getCommunityPermissions(),
156 serviceContext.getGuestPermissions());
157 }
158
159
160
161 saveImages(
162 image.getLargeImageId(), renderedImage, image.getSmallImageId(),
163 image.getCustom1ImageId(), image.getCustom2ImageId(), bytes,
164 contentType);
165
166
167
168 updateAsset(
169 userId, image, serviceContext.getAssetCategoryIds(),
170 serviceContext.getAssetTagNames(), contentType);
171
172
173
174 socialActivityLocalService.addActivity(
175 userId, image.getGroupId(), IGImage.class.getName(), imageId,
176 IGActivityKeys.ADD_IMAGE, StringPool.BLANK, 0);
177
178
179
180 Indexer indexer = IndexerRegistryUtil.getIndexer(IGImage.class);
181
182 indexer.reindex(image);
183
184 return image;
185 }
186 catch (IOException ioe) {
187 throw new ImageSizeException(ioe);
188 }
189 }
190
191 public IGImage addImage(
192 long userId, long groupId, long folderId, String name,
193 String description, String fileName, InputStream is,
194 String contentType, ServiceContext serviceContext)
195 throws PortalException, SystemException {
196
197 try {
198 byte[] bytes = FileUtil.getBytes(is);
199
200 return addImage(
201 userId, groupId, folderId, name, description, fileName, bytes,
202 contentType, serviceContext);
203 }
204 catch (IOException ioe) {
205 throw new SystemException(ioe);
206 }
207 }
208
209 public void addImageResources(
210 IGImage image, boolean addCommunityPermissions,
211 boolean addGuestPermissions)
212 throws PortalException, SystemException {
213
214 resourceLocalService.addResources(
215 image.getCompanyId(), image.getGroupId(), image.getUserId(),
216 IGImage.class.getName(), image.getImageId(), false,
217 addCommunityPermissions, addGuestPermissions);
218 }
219
220 public void addImageResources(
221 IGImage image, String[] communityPermissions,
222 String[] guestPermissions)
223 throws PortalException, SystemException {
224
225 resourceLocalService.addModelResources(
226 image.getCompanyId(), image.getGroupId(), image.getUserId(),
227 IGImage.class.getName(), image.getImageId(), communityPermissions,
228 guestPermissions);
229 }
230
231 public void addImageResources(
232 long imageId, boolean addCommunityPermissions,
233 boolean addGuestPermissions)
234 throws PortalException, SystemException {
235
236 IGImage image = igImagePersistence.findByPrimaryKey(imageId);
237
238 addImageResources(image, addCommunityPermissions, addGuestPermissions);
239 }
240
241 public void addImageResources(
242 long imageId, String[] communityPermissions,
243 String[] guestPermissions)
244 throws PortalException, SystemException {
245
246 IGImage image = igImagePersistence.findByPrimaryKey(imageId);
247
248 addImageResources(image, communityPermissions, guestPermissions);
249 }
250
251 public void deleteImage(IGImage image)
252 throws PortalException, SystemException {
253
254
255
256 igImagePersistence.remove(image);
257
258
259
260 resourceLocalService.deleteResource(
261 image.getCompanyId(), IGImage.class.getName(),
262 ResourceConstants.SCOPE_INDIVIDUAL, image.getImageId());
263
264
265
266 imageLocalService.deleteImage(image.getSmallImageId());
267 imageLocalService.deleteImage(image.getLargeImageId());
268 imageLocalService.deleteImage(image.getCustom1ImageId());
269 imageLocalService.deleteImage(image.getCustom2ImageId());
270
271
272
273 assetEntryLocalService.deleteEntry(
274 IGImage.class.getName(), image.getImageId());
275
276
277
278 expandoValueLocalService.deleteValues(
279 IGImage.class.getName(), image.getImageId());
280
281
282
283 socialActivityLocalService.deleteActivities(
284 IGImage.class.getName(), image.getImageId());
285
286
287
288 Indexer indexer = IndexerRegistryUtil.getIndexer(IGImage.class);
289
290 indexer.delete(image);
291 }
292
293 public void deleteImage(long imageId)
294 throws PortalException, SystemException {
295
296 IGImage image = igImagePersistence.findByPrimaryKey(imageId);
297
298 deleteImage(image);
299 }
300
301 public void deleteImages(long groupId, long folderId)
302 throws PortalException, SystemException {
303
304 List<IGImage> images = igImagePersistence.findByG_F(groupId, folderId);
305
306 for (IGImage image : images) {
307 deleteImage(image);
308 }
309 }
310
311 public int getFoldersImagesCount(long groupId, List<Long> folderIds)
312 throws SystemException {
313
314 return igImagePersistence.countByG_F(
315 groupId,
316 ArrayUtil.toArray(folderIds.toArray(new Long[folderIds.size()])));
317 }
318
319 public List<IGImage> getGroupImages(long groupId, int start, int end)
320 throws SystemException {
321
322 return igImagePersistence.findByGroupId(
323 groupId, start, end, new ImageModifiedDateComparator());
324 }
325
326 public List<IGImage> getGroupImages(
327 long groupId, long userId, int start, int end)
328 throws SystemException {
329
330 OrderByComparator orderByComparator = new ImageModifiedDateComparator();
331
332 if (userId <= 0) {
333 return igImagePersistence.findByGroupId(
334 groupId, start, end, orderByComparator);
335 }
336 else {
337 return igImagePersistence.findByG_U(
338 groupId, userId, start, end, orderByComparator);
339 }
340 }
341
342 public int getGroupImagesCount(long groupId) throws SystemException {
343 return igImagePersistence.countByGroupId(groupId);
344 }
345
346 public int getGroupImagesCount(long groupId, long userId)
347 throws SystemException {
348
349 if (userId <= 0) {
350 return igImagePersistence.countByGroupId(groupId);
351 }
352 else {
353 return igImagePersistence.countByG_U(groupId, userId);
354 }
355 }
356
357 public IGImage getImage(long imageId)
358 throws PortalException, SystemException {
359
360 return igImagePersistence.findByPrimaryKey(imageId);
361 }
362
363 public IGImage getImageByCustom1ImageId(long custom1ImageId)
364 throws PortalException, SystemException {
365
366 return igImagePersistence.findByCustom1ImageId(custom1ImageId);
367 }
368
369 public IGImage getImageByCustom2ImageId(long custom2ImageId)
370 throws PortalException, SystemException {
371
372 return igImagePersistence.findByCustom2ImageId(custom2ImageId);
373 }
374
375 public IGImage getImageByFolderIdAndNameWithExtension(
376 long groupId, long folderId, String nameWithExtension)
377 throws PortalException, SystemException {
378
379 String name = FileUtil.stripExtension(nameWithExtension);
380
381 List<IGImage> images = igImagePersistence.findByG_F_N(
382 groupId, folderId, name);
383
384 if ((images.size() <= 0) && Validator.isNumber(name)) {
385 long imageId = GetterUtil.getLong(name);
386
387 IGImage image = igImagePersistence.fetchByPrimaryKey(imageId);
388
389 if (image != null) {
390 images.add(image);
391 }
392 }
393
394 for (IGImage image : images) {
395 if (nameWithExtension.equals(image.getNameWithExtension())) {
396 return image;
397 }
398 }
399
400 throw new NoSuchImageException();
401 }
402
403 public IGImage getImageByLargeImageId(long largeImageId)
404 throws PortalException, SystemException {
405
406 return igImagePersistence.findByLargeImageId(largeImageId);
407 }
408
409 public IGImage getImageBySmallImageId(long smallImageId)
410 throws PortalException, SystemException {
411
412 return igImagePersistence.findBySmallImageId(smallImageId);
413 }
414
415 public IGImage getImageByUuidAndGroupId(String uuid, long groupId)
416 throws PortalException, SystemException {
417
418 return igImagePersistence.findByUUID_G(uuid, groupId);
419 }
420
421 public List<IGImage> getImages(long groupId, long folderId)
422 throws SystemException {
423
424 return igImagePersistence.findByG_F(groupId, folderId);
425 }
426
427 public List<IGImage> getImages(
428 long groupId, long folderId, int start, int end)
429 throws SystemException {
430
431 return igImagePersistence.findByG_F(groupId, folderId, start, end);
432 }
433
434 public List<IGImage> getImages(
435 long groupId, long folderId, int start, int end,
436 OrderByComparator obc)
437 throws SystemException {
438
439 return igImagePersistence.findByG_F(groupId, folderId, start, end, obc);
440 }
441
442 public int getImagesCount(long groupId, long folderId)
443 throws SystemException {
444
445 return igImagePersistence.countByG_F(groupId, folderId);
446 }
447
448 public List<IGImage> getNoAssetImages() throws SystemException {
449 return igImageFinder.findByNoAssets();
450 }
451
452 public void updateAsset(
453 long userId, IGImage image, long[] assetCategoryIds,
454 String[] assetTagNames, String contentType)
455 throws PortalException, SystemException {
456
457 Image largeImage = imageLocalService.getImage(image.getLargeImageId());
458
459 if (largeImage == null) {
460 return;
461 }
462
463 if (contentType == null) {
464 contentType = MimeTypesUtil.getContentType(largeImage.getType());
465 }
466
467 assetEntryLocalService.updateEntry(
468 userId, image.getGroupId(), IGImage.class.getName(),
469 image.getImageId(), image.getUuid(), assetCategoryIds,
470 assetTagNames, true, null, null, null, null, contentType,
471 image.getName(), image.getDescription(), null, null,
472 largeImage.getHeight(), largeImage.getWidth(), null, false);
473 }
474
475 public IGImage updateImage(
476 long userId, long imageId, long groupId, long folderId, String name,
477 String description, byte[] bytes, String contentType,
478 ServiceContext serviceContext)
479 throws PortalException, SystemException {
480
481 try {
482
483
484
485 IGImage image = igImagePersistence.findByPrimaryKey(imageId);
486
487 folderId = getFolder(image, folderId);
488
489 RenderedImage renderedImage = null;
490
491 if (bytes != null) {
492 renderedImage = ImageProcessorUtil.read(
493 bytes).getRenderedImage();
494
495 validate(bytes);
496 }
497
498 if (Validator.isNotNull(name) && !name.equals(image.getName())) {
499 String nameWithExtension = IGImageImpl.getNameWithExtension(
500 name, image.getImageType());
501
502 validate(groupId, folderId, nameWithExtension);
503 }
504 else {
505 name = image.getName();
506 }
507
508 image.setModifiedDate(serviceContext.getModifiedDate(null));
509 image.setFolderId(folderId);
510 image.setName(name);
511 image.setDescription(description);
512 image.setExpandoBridgeAttributes(serviceContext);
513
514 igImagePersistence.update(image, false);
515
516
517
518 if (renderedImage != null) {
519 saveImages(
520 image.getLargeImageId(), renderedImage,
521 image.getSmallImageId(), image.getCustom1ImageId(),
522 image.getCustom2ImageId(), bytes, contentType);
523 }
524
525
526
527 long[] assetCategoryIds = serviceContext.getAssetCategoryIds();
528 String[] assetTagNames = serviceContext.getAssetTagNames();
529
530 updateAsset(
531 userId, image, assetCategoryIds, assetTagNames, contentType);
532
533
534
535 socialActivityLocalService.addActivity(
536 userId, image.getGroupId(), IGImage.class.getName(), imageId,
537 IGActivityKeys.UPDATE_IMAGE, StringPool.BLANK, 0);
538
539
540
541 Indexer indexer = IndexerRegistryUtil.getIndexer(IGImage.class);
542
543 indexer.reindex(image);
544
545 return image;
546 }
547 catch (IOException ioe) {
548 throw new ImageSizeException(ioe);
549 }
550 }
551
552 public IGImage updateImage(
553 long userId, long imageId, long groupId, long folderId, String name,
554 String description, File file, String contentType,
555 ServiceContext serviceContext)
556 throws PortalException, SystemException {
557
558 try {
559 byte[] bytes = null;
560
561 if ((file != null) && file.exists()) {
562 bytes = FileUtil.getBytes(file);
563 }
564
565 return updateImage(
566 userId, imageId, groupId, folderId, name, description, bytes,
567 contentType, serviceContext);
568 }
569 catch (IOException ioe) {
570 throw new SystemException(ioe);
571 }
572 }
573
574 public IGImage updateImage(
575 long userId, long imageId, long groupId, long folderId, String name,
576 String description, InputStream is, String contentType,
577 ServiceContext serviceContext)
578 throws PortalException, SystemException {
579
580 try {
581 byte[] bytes = null;
582
583 if (is != null) {
584 bytes = FileUtil.getBytes(is);
585 }
586
587 return updateImage(
588 userId, imageId, groupId, folderId, name, description, bytes,
589 contentType, serviceContext);
590 }
591 catch (IOException ioe) {
592 throw new SystemException(ioe);
593 }
594 }
595
596 public void updateSmallImage(long smallImageId, long largeImageId)
597 throws PortalException, SystemException {
598
599 try {
600 RenderedImage renderedImage = null;
601
602 Image largeImage = imageLocalService.getImage(largeImageId);
603
604 byte[] bytes = largeImage.getTextObj();
605 String contentType = largeImage.getType();
606
607 if (bytes != null) {
608 renderedImage = ImageProcessorUtil.read(
609 bytes).getRenderedImage();
610
611
612 }
613
614 if (renderedImage != null) {
615 saveScaledImage(
616 renderedImage, smallImageId, contentType,
617 PrefsPropsUtil.getInteger(
618 PropsKeys.IG_IMAGE_THUMBNAIL_MAX_DIMENSION));
619 }
620 }
621 catch (IOException ioe) {
622 throw new ImageSizeException(ioe);
623 }
624 }
625
626 protected long getFolder(IGImage image, long folderId)
627 throws SystemException {
628
629 if ((image.getFolderId() != folderId) &&
630 (folderId != IGFolderConstants.DEFAULT_PARENT_FOLDER_ID)) {
631
632 IGFolder newFolder = igFolderPersistence.fetchByPrimaryKey(
633 folderId);
634
635 if ((newFolder == null) ||
636 (image.getGroupId() != newFolder.getGroupId())) {
637
638 folderId = image.getFolderId();
639 }
640 }
641
642 return folderId;
643 }
644
645 protected void saveImages(
646 long largeImageId, RenderedImage renderedImage, long smallImageId,
647 long custom1ImageId, long custom2ImageId, byte[] bytes,
648 String contentType)
649 throws PortalException, SystemException {
650
651 try {
652
653
654
655 imageLocalService.updateImage(largeImageId, bytes);
656
657
658
659 saveScaledImage(
660 renderedImage, smallImageId, contentType,
661 PrefsPropsUtil.getInteger(
662 PropsKeys.IG_IMAGE_THUMBNAIL_MAX_DIMENSION));
663
664 if (custom1ImageId > 0) {
665 saveScaledImage(
666 renderedImage, custom1ImageId, contentType,
667 PropsValues.IG_IMAGE_CUSTOM_1_MAX_DIMENSION);
668 }
669
670 if (custom2ImageId > 0) {
671 saveScaledImage(
672 renderedImage, custom2ImageId, contentType,
673 PropsValues.IG_IMAGE_CUSTOM_2_MAX_DIMENSION);
674 }
675 }
676 catch (IOException ioe) {
677 throw new SystemException(ioe);
678 }
679 }
680
681 protected void saveScaledImage(
682 RenderedImage renderedImage, long imageId, String contentType,
683 int dimension)
684 throws IOException, PortalException, SystemException {
685
686 RenderedImage thumbnail = ImageProcessorUtil.scale(
687 renderedImage, dimension, dimension);
688
689 imageLocalService.updateImage(
690 imageId, ImageProcessorUtil.getBytes(thumbnail, contentType));
691 }
692
693 protected void validate(byte[] bytes)
694 throws ImageSizeException, SystemException {
695
696 if ((PrefsPropsUtil.getLong(PropsKeys.IG_IMAGE_MAX_SIZE) > 0) &&
697 ((bytes == null) ||
698 (bytes.length >
699 PrefsPropsUtil.getLong(PropsKeys.IG_IMAGE_MAX_SIZE)))) {
700
701 throw new ImageSizeException();
702 }
703 }
704
705 protected void validate(
706 long groupId, long folderId, String nameWithExtension)
707 throws PortalException, SystemException {
708
709 if ((nameWithExtension.indexOf("\\\\") != -1) ||
710 (nameWithExtension.indexOf("
711 (nameWithExtension.indexOf(":") != -1) ||
712 (nameWithExtension.indexOf("*") != -1) ||
713 (nameWithExtension.indexOf("?") != -1) ||
714 (nameWithExtension.indexOf("\"") != -1) ||
715 (nameWithExtension.indexOf("<") != -1) ||
716 (nameWithExtension.indexOf(">") != -1) ||
717 (nameWithExtension.indexOf("|") != -1) ||
718 (nameWithExtension.indexOf("[") != -1) ||
719 (nameWithExtension.indexOf("]") != -1) ||
720 (nameWithExtension.indexOf("'") != -1)) {
721
722 throw new ImageNameException();
723 }
724
725 boolean validImageExtension = false;
726
727 String[] imageExtensions = PrefsPropsUtil.getStringArray(
728 PropsKeys.IG_IMAGE_EXTENSIONS, StringPool.COMMA);
729
730 for (int i = 0; i < imageExtensions.length; i++) {
731 if (StringPool.STAR.equals(imageExtensions[i]) ||
732 StringUtil.endsWith(nameWithExtension, imageExtensions[i])) {
733
734 validImageExtension = true;
735
736 break;
737 }
738 }
739
740 if (!validImageExtension) {
741 throw new ImageNameException();
742 }
743
744 String name = FileUtil.stripExtension(nameWithExtension);
745 String imageType = FileUtil.getExtension(nameWithExtension);
746
747 List<IGImage> images = igImagePersistence.findByG_F_N(
748 groupId, folderId, name);
749
750 if (imageType.equals("jpeg")) {
751 imageType = ImageProcessor.TYPE_JPEG;
752 }
753 else if (imageType.equals("tif")) {
754 imageType = ImageProcessor.TYPE_TIFF;
755 }
756
757 for (IGImage image : images) {
758 if (imageType.equals(image.getImageType())) {
759 throw new DuplicateImageNameException();
760 }
761 }
762 }
763
764 protected void validate(
765 long groupId, long folderId, String nameWithExtension,
766 String fileName, byte[] bytes)
767 throws PortalException, SystemException {
768
769 if (Validator.isNotNull(fileName)) {
770 String extension = FileUtil.getExtension(fileName);
771
772 if (Validator.isNull(nameWithExtension)) {
773 nameWithExtension = fileName;
774 }
775 else if (!StringUtil.endsWith(nameWithExtension, extension)) {
776 throw new ImageNameException();
777 }
778 }
779
780 validate(groupId, folderId, nameWithExtension);
781 validate(bytes);
782 }
783
784 }