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