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