001
014
015 package com.liferay.portlet.documentlibrary.util;
016
017 import com.liferay.portal.kernel.exception.PortalException;
018 import com.liferay.portal.kernel.exception.SystemException;
019 import com.liferay.portal.kernel.image.ImageBag;
020 import com.liferay.portal.kernel.image.ImageToolUtil;
021 import com.liferay.portal.kernel.io.FileFilter;
022 import com.liferay.portal.kernel.lar.PortletDataContext;
023 import com.liferay.portal.kernel.log.Log;
024 import com.liferay.portal.kernel.log.LogFactoryUtil;
025 import com.liferay.portal.kernel.repository.model.FileEntry;
026 import com.liferay.portal.kernel.repository.model.FileVersion;
027 import com.liferay.portal.kernel.util.FileUtil;
028 import com.liferay.portal.kernel.util.GetterUtil;
029 import com.liferay.portal.kernel.util.PrefsPropsUtil;
030 import com.liferay.portal.kernel.util.PropsKeys;
031 import com.liferay.portal.kernel.util.PropsUtil;
032 import com.liferay.portal.kernel.util.StringBundler;
033 import com.liferay.portal.kernel.util.StringPool;
034 import com.liferay.portal.kernel.util.SystemProperties;
035 import com.liferay.portal.kernel.xml.Element;
036 import com.liferay.portal.model.CompanyConstants;
037 import com.liferay.portal.util.PortalUtil;
038 import com.liferay.portal.util.PortletKeys;
039 import com.liferay.portlet.documentlibrary.DuplicateDirectoryException;
040 import com.liferay.portlet.documentlibrary.store.DLStoreUtil;
041
042 import java.awt.image.RenderedImage;
043
044 import java.io.File;
045 import java.io.FileInputStream;
046 import java.io.IOException;
047 import java.io.InputStream;
048
049
052 public abstract class DLPreviewableProcessor implements DLProcessor {
053
054 public static final String PREVIEW_PATH = "document_preview/";
055
056 public static final String PREVIEW_TMP_PATH =
057 SystemProperties.get(SystemProperties.TMP_DIR) +
058 "/liferay/" + PREVIEW_PATH;
059
060 public static final long REPOSITORY_ID = CompanyConstants.SYSTEM;
061
062 public static final int THUMBNAIL_INDEX_CUSTOM_1 = 1;
063
064 public static final int THUMBNAIL_INDEX_CUSTOM_2 = 2;
065
066 public static final int THUMBNAIL_INDEX_DEFAULT = 0;
067
068 public static final String THUMBNAIL_PATH = "document_thumbnail/";
069
070 public static final String THUMBNAIL_TMP_PATH =
071 SystemProperties.get(SystemProperties.TMP_DIR) +
072 "/liferay/" + THUMBNAIL_PATH;
073
074 public static void deleteFiles() {
075 long[] companyIds = PortalUtil.getCompanyIds();
076
077 for (long companyId : companyIds) {
078 try {
079 DLStoreUtil.deleteDirectory(
080 companyId, REPOSITORY_ID, PREVIEW_PATH);
081 }
082 catch (Exception e) {
083 }
084
085 try {
086 DLStoreUtil.deleteDirectory(
087 companyId, REPOSITORY_ID, THUMBNAIL_PATH);
088 }
089 catch (Exception e) {
090 }
091 }
092 }
093
094 public static void deleteFiles(FileEntry fileEntry, String thumbnailType) {
095 deleteFiles(
096 fileEntry.getCompanyId(), fileEntry.getRepositoryId(),
097 fileEntry.getFileEntryId(), -1, thumbnailType);
098 }
099
100 public static void deleteFiles(
101 FileVersion fileVersion, String thumbnailType) {
102
103 deleteFiles(
104 fileVersion.getCompanyId(), fileVersion.getRepositoryId(),
105 fileVersion.getFileEntryId(), fileVersion.getFileVersionId(),
106 thumbnailType);
107 }
108
109 public void cleanUp(FileEntry fileEntry) {
110 deleteFiles(fileEntry, getThumbnailType());
111 }
112
113 public void cleanUp(FileVersion fileVersion) {
114 deleteFiles(fileVersion, getThumbnailType());
115 }
116
117 public boolean isSupported(FileVersion fileVersion) {
118 if (fileVersion == null) {
119 return false;
120 }
121
122 return isSupported(fileVersion.getMimeType());
123 }
124
125 protected static void deleteFiles(
126 long companyId, long groupId, long fileEntryId, long fileVersionId,
127 String thumbnailType) {
128
129 try {
130 DLStoreUtil.deleteDirectory(
131 companyId, REPOSITORY_ID,
132 getPathSegment(groupId, fileEntryId, fileVersionId, true));
133 }
134 catch (Exception e) {
135 }
136
137 try {
138 String dirName = getPathSegment(
139 groupId, fileEntryId, fileVersionId, false);
140
141 if (fileVersionId > 0) {
142 dirName = dirName.concat(StringPool.PERIOD);
143 dirName = dirName.concat(thumbnailType);
144 }
145
146 DLStoreUtil.deleteDirectory(companyId, REPOSITORY_ID, dirName);
147 }
148 catch (Exception e) {
149 }
150 }
151
152 protected static String getPathSegment(
153 FileVersion fileVersion, boolean preview) {
154
155 return getPathSegment(
156 fileVersion.getGroupId(), fileVersion.getFileEntryId(),
157 fileVersion.getFileVersionId(), preview);
158 }
159
160 protected static String getPathSegment(
161 long groupId, long fileEntryId, long fileVersionId, boolean preview) {
162
163 StringBundler sb = null;
164
165 if (fileVersionId > 0) {
166 sb = new StringBundler(5);
167 }
168 else {
169 sb = new StringBundler(3);
170 }
171
172 if (preview) {
173 sb.append(PREVIEW_PATH);
174 }
175 else {
176 sb.append(THUMBNAIL_PATH);
177 }
178
179 sb.append(groupId);
180 sb.append(DLUtil.getDividedPath(fileEntryId));
181
182 if (fileVersionId > 0) {
183 sb.append(StringPool.SLASH);
184 sb.append(fileVersionId);
185 }
186
187 return sb.toString();
188 }
189
190 protected void addFileToStore(
191 long companyId, String dirName, String filePath, File srcFile)
192 throws Exception {
193
194 try {
195 DLStoreUtil.addDirectory(companyId, REPOSITORY_ID, dirName);
196 }
197 catch (DuplicateDirectoryException dde) {
198 }
199
200 DLStoreUtil.addFile(companyId, REPOSITORY_ID, filePath, srcFile);
201 }
202
203 protected void addFileToStore(
204 long companyId, String dirName, String filePath, InputStream is)
205 throws Exception {
206
207 try {
208 DLStoreUtil.addDirectory(companyId, REPOSITORY_ID, dirName);
209 }
210 catch (DuplicateDirectoryException dde) {
211 }
212
213 DLStoreUtil.addFile(companyId, REPOSITORY_ID, filePath, is);
214 }
215
216 protected InputStream doGetPreviewAsStream(
217 FileVersion fileVersion, int index, String type)
218 throws Exception {
219
220 return DLStoreUtil.getFileAsStream(
221 fileVersion.getCompanyId(), CompanyConstants.SYSTEM,
222 getPreviewFilePath(fileVersion, index, type));
223 }
224
225 protected InputStream doGetPreviewAsStream(
226 FileVersion fileVersion, String type)
227 throws Exception {
228
229 return doGetPreviewAsStream(fileVersion, 0, type);
230 }
231
232 protected int doGetPreviewFileCount(FileVersion fileVersion)
233 throws Exception {
234
235 try {
236 String[] fileNames = DLStoreUtil.getFileNames(
237 fileVersion.getCompanyId(), REPOSITORY_ID,
238 getPathSegment(fileVersion, true));
239
240 return fileNames.length;
241 }
242 catch (Exception e) {
243 }
244
245 return 0;
246 }
247
248 protected long doGetPreviewFileSize(FileVersion fileVersion, int index)
249 throws Exception {
250
251 return doGetPreviewFileSize(fileVersion, index, getPreviewType());
252 }
253
254 protected long doGetPreviewFileSize(
255 FileVersion fileVersion, int index, String type)
256 throws Exception {
257
258 return DLStoreUtil.getFileSize(
259 fileVersion.getCompanyId(), CompanyConstants.SYSTEM,
260 getPreviewFilePath(fileVersion, index, type));
261 }
262
263 protected long doGetPreviewFileSize(FileVersion fileVersion, String type)
264 throws Exception {
265
266 return doGetPreviewFileSize(fileVersion, 0, type);
267 }
268
269 protected File doGetThumbnail(FileVersion fileVersion, int index) {
270 try {
271 String imageType = getThumbnailType(fileVersion);
272
273 return DLStoreUtil.getFile(
274 fileVersion.getCompanyId(), REPOSITORY_ID,
275 getThumbnailFilePath(fileVersion, imageType, index));
276 }
277 catch (Exception e) {
278 _log.error(e, e);
279 }
280
281 return null;
282 }
283
284 protected InputStream doGetThumbnailAsStream(
285 FileVersion fileVersion, int index)
286 throws Exception {
287
288 String type = getThumbnailType(fileVersion);
289
290 return DLStoreUtil.getFileAsStream(
291 fileVersion.getCompanyId(), CompanyConstants.SYSTEM,
292 getThumbnailFilePath(fileVersion, type, index));
293 }
294
295 protected long doGetThumbnailFileSize(FileVersion fileVersion, int index)
296 throws Exception {
297
298 String type = getThumbnailType(fileVersion);
299
300 return DLStoreUtil.getFileSize(
301 fileVersion.getCompanyId(), CompanyConstants.SYSTEM,
302 getThumbnailFilePath(fileVersion, type, index));
303 }
304
305 protected void exportBinary(
306 PortletDataContext portletDataContext, Element fileEntryElement,
307 FileVersion fileVersion, File file, String binPath,
308 String binPathName)
309 throws SystemException {
310
311 try {
312 InputStream is = new FileInputStream(file);
313
314 exportBinary(
315 portletDataContext, fileEntryElement, fileVersion, is, binPath,
316 binPathName);
317 }
318 catch (IOException ioe) {
319 throw new SystemException(ioe);
320 }
321 }
322
323 protected void exportBinary(
324 PortletDataContext portletDataContext, Element fileEntryElement,
325 FileVersion fileVersion, InputStream is, String binPath,
326 String binPathName)
327 throws SystemException {
328
329 fileEntryElement.addAttribute(binPathName, binPath);
330
331 if (is == null) {
332 if (_log.isWarnEnabled()) {
333 _log.warn(
334 "No input stream found for file entry " +
335 fileVersion.getFileEntryId());
336 }
337
338 fileEntryElement.detach();
339
340 return;
341 }
342
343 portletDataContext.addZipEntry(binPath, is);
344 }
345
346 protected void exportPreview(
347 PortletDataContext portletDataContext, FileEntry fileEntry,
348 Element fileEntryElement, String binPathSuffix, String previewType)
349 throws Exception {
350
351 exportPreview(
352 portletDataContext, fileEntry, fileEntryElement, binPathSuffix,
353 previewType, -1);
354 }
355
356 protected void exportPreview(
357 PortletDataContext portletDataContext, FileEntry fileEntry,
358 Element fileEntryElement, String binPathSuffix, String previewType,
359 int fileIndex)
360 throws Exception {
361
362 if (portletDataContext.isPerformDirectBinaryImport()) {
363 return;
364 }
365
366 String binPathSegment = null;
367
368 if (fileIndex < 0) {
369 binPathSegment = previewType;
370 }
371 else {
372 binPathSegment = Integer.toString(fileIndex + 1);
373 }
374
375 String binPath = getBinPath(
376 portletDataContext, fileEntry, binPathSegment);
377
378 StringBundler sb = new StringBundler(4);
379
380 sb.append("bin-path-preview-");
381 sb.append(binPathSegment);
382 sb.append("-");
383 sb.append(binPathSuffix);
384
385 String binPathName = sb.toString();
386
387 fileEntryElement.addAttribute(binPathName, binPath);
388
389 FileVersion fileVersion = fileEntry.getFileVersion();
390
391 InputStream is = null;
392
393 if (fileIndex < 0) {
394 is = doGetPreviewAsStream(fileVersion, previewType);
395 }
396 else {
397 is = doGetPreviewAsStream(fileVersion, fileIndex + 1, previewType);
398 }
399
400 exportBinary(
401 portletDataContext, fileEntryElement, fileVersion, is, binPath,
402 binPathName);
403 }
404
405 protected void exportThumbnail(
406 PortletDataContext portletDataContext, FileEntry fileEntry,
407 Element fileEntryElement, String binPathName, int index)
408 throws PortalException, SystemException {
409
410 FileVersion fileVersion = fileEntry.getFileVersion();
411
412 if (!hasThumbnail(fileVersion, index)) {
413 return;
414 }
415
416 File file = doGetThumbnail(fileVersion, index);
417
418 String binPath = getBinPath(portletDataContext, fileEntry, index);
419
420 fileEntryElement.addAttribute(binPathName, binPath);
421
422 exportBinary(
423 portletDataContext, fileEntryElement, fileVersion, file, binPath,
424 binPathName);
425 }
426
427 protected void exportThumbnails(
428 PortletDataContext portletDataContext, FileEntry fileEntry,
429 Element fileEntryElement, String binPathSuffix)
430 throws PortalException, SystemException {
431
432 FileVersion fileVersion = fileEntry.getFileVersion();
433
434 if (!isSupported(fileVersion) || !hasThumbnails(fileVersion)) {
435 return;
436 }
437
438 if (!portletDataContext.isPerformDirectBinaryImport()) {
439 exportThumbnail(
440 portletDataContext, fileEntry, fileEntryElement,
441 "bin-path-thumbnail-default-" + binPathSuffix,
442 THUMBNAIL_INDEX_DEFAULT);
443
444 exportThumbnail(
445 portletDataContext, fileEntry, fileEntryElement,
446 "bin-path-thumbnail-custom-1-" + binPathSuffix,
447 THUMBNAIL_INDEX_CUSTOM_1);
448
449 exportThumbnail(
450 portletDataContext, fileEntry, fileEntryElement,
451 "bin-path-thumbnail-custom-2-" + binPathSuffix,
452 THUMBNAIL_INDEX_CUSTOM_2);
453 }
454 }
455
456 protected String getBinPath(
457 PortletDataContext portletDataContext, FileEntry fileEntry, int index) {
458
459 StringBundler sb = new StringBundler(8);
460
461 sb.append(
462 portletDataContext.getPortletPath(PortletKeys.DOCUMENT_LIBRARY));
463 sb.append("/bin/");
464 sb.append(fileEntry.getFileEntryId());
465 sb.append(StringPool.SLASH);
466 sb.append(THUMBNAIL_PATH);
467 sb.append(fileEntry.getVersion());
468 sb.append(StringPool.SLASH);
469 sb.append(index);
470
471 return sb.toString();
472 }
473
474 protected String getBinPath(
475 PortletDataContext portletDataContext, FileEntry fileEntry,
476 String type) {
477
478 StringBundler sb = new StringBundler(8);
479
480 sb.append(
481 portletDataContext.getPortletPath(PortletKeys.DOCUMENT_LIBRARY));
482 sb.append("/bin/");
483 sb.append(fileEntry.getFileEntryId());
484 sb.append(StringPool.SLASH);
485 sb.append(PREVIEW_PATH);
486 sb.append(fileEntry.getVersion());
487 sb.append(StringPool.SLASH);
488 sb.append(type);
489
490 return sb.toString();
491 }
492
493 protected String getPreviewFilePath(FileVersion fileVersion) {
494 return getPreviewFilePath(fileVersion, 0);
495 }
496
497 protected String getPreviewFilePath(FileVersion fileVersion, int index) {
498 return getPreviewFilePath(fileVersion, index, getPreviewType());
499 }
500
501 protected String getPreviewFilePath(
502 FileVersion fileVersion, int index, String type) {
503
504 StringBundler sb = null;
505
506 if (index > 0) {
507 sb = new StringBundler(5);
508 }
509 else {
510 sb = new StringBundler(3);
511 }
512
513 sb.append(getPathSegment(fileVersion, true));
514
515 if (index > 0) {
516 sb.append(StringPool.SLASH);
517 sb.append(index - 1);
518 }
519
520 sb.append(StringPool.PERIOD);
521 sb.append(type);
522
523 return sb.toString();
524 }
525
526 protected String getPreviewFilePath(FileVersion fileVersion, String type) {
527 return getPreviewFilePath(fileVersion, 0, type);
528 }
529
530 protected File getPreviewTempFile(String id) {
531 return getPreviewTempFile(id, 0);
532 }
533
534 protected File getPreviewTempFile(String id, int index) {
535 return getPreviewTempFile(id, index, getPreviewType());
536 }
537
538 protected File getPreviewTempFile(String id, int index, String type) {
539 String previewTempFilePath = getPreviewTempFilePath(id, index, type);
540
541 return new File(previewTempFilePath);
542 }
543
544 protected File getPreviewTempFile(String id, String type) {
545 return getPreviewTempFile(id, 0, type);
546 }
547
548 protected int getPreviewTempFileCount(FileVersion fileVersion) {
549 return getPreviewTempFileCount(fileVersion, getPreviewType());
550 }
551
552 protected int getPreviewTempFileCount(
553 FileVersion fileVersion, String type) {
554
555 String tempFileId = DLUtil.getTempFileId(
556 fileVersion.getFileEntryId(), fileVersion.getVersion());
557
558 StringBundler sb = new StringBundler(5);
559
560 sb.append(tempFileId);
561 sb.append(StringPool.DASH);
562 sb.append("(.*)");
563 sb.append(StringPool.PERIOD);
564 sb.append(type);
565
566 File dir = new File(PREVIEW_TMP_PATH);
567
568 File[] files = dir.listFiles(new FileFilter(sb.toString()));
569
570 if (_log.isDebugEnabled()) {
571 for (File file : files) {
572 _log.debug("Preview page for " + tempFileId + " " + file);
573 }
574 }
575
576 return files.length;
577 }
578
579 protected String getPreviewTempFilePath(String id) {
580 return getPreviewTempFilePath(id, 0);
581 }
582
583 protected String getPreviewTempFilePath(String id, int index) {
584 return getPreviewTempFilePath(id, index, getPreviewType());
585 }
586
587 protected String getPreviewTempFilePath(String id, int index, String type) {
588 StringBundler sb = null;
589
590 if (index > 0) {
591 sb = new StringBundler(6);
592 }
593 else {
594 sb = new StringBundler(4);
595 }
596
597 sb.append(PREVIEW_TMP_PATH);
598 sb.append(id);
599
600 if (index > 0) {
601 sb.append(StringPool.DASH);
602 sb.append(index - 1);
603 }
604
605 sb.append(StringPool.PERIOD);
606 sb.append(type);
607
608 return sb.toString();
609 }
610
611 protected String getPreviewTempFilePath(String id, String type) {
612 return getPreviewTempFilePath(id, 0, type);
613 }
614
615 protected String getPreviewType() {
616 return getPreviewType(null);
617 }
618
619 protected abstract String getPreviewType(FileVersion fileVersion);
620
621 protected String getPreviewType(int index) {
622 String[] previewTypes = getPreviewTypes();
623
624 if ((previewTypes != null) && (previewTypes.length > index)) {
625 return previewTypes[index];
626 }
627 else {
628 return getPreviewType();
629 }
630 }
631
632 protected String[] getPreviewTypes() {
633 return new String[] {getPreviewType()};
634 }
635
636 protected String getThumbnailFilePath(FileVersion fileVersion, int index) {
637 return getThumbnailFilePath(fileVersion, getThumbnailType(), index);
638 }
639
640 protected String getThumbnailFilePath(
641 FileVersion fileVersion, String type, int index) {
642
643 StringBundler sb = new StringBundler(5);
644
645 sb.append(getPathSegment(fileVersion, false));
646
647 if (index != THUMBNAIL_INDEX_DEFAULT) {
648 sb.append(StringPool.DASH);
649 sb.append(index);
650 }
651
652 sb.append(StringPool.PERIOD);
653 sb.append(type);
654
655 return sb.toString();
656 }
657
658 protected File getThumbnailTempFile(String id) {
659 return getThumbnailTempFile(id, getThumbnailType());
660 }
661
662 protected File getThumbnailTempFile(String id, String type) {
663 String thumbnailTempFilePath = getThumbnailTempFilePath(id, type);
664
665 return new File(thumbnailTempFilePath);
666 }
667
668 protected String getThumbnailTempFilePath(String id) {
669 return getThumbnailTempFilePath(id, getThumbnailType());
670 }
671
672 protected String getThumbnailTempFilePath(String id, String type) {
673 StringBundler sb = new StringBundler(4);
674
675 sb.append(THUMBNAIL_TMP_PATH);
676 sb.append(id);
677 sb.append(StringPool.PERIOD);
678 sb.append(type);
679
680 return sb.toString();
681 }
682
683 protected String getThumbnailType() {
684 return getThumbnailType(null);
685 }
686
687 protected abstract String getThumbnailType(FileVersion fileVersion);
688
689 protected boolean hasThumbnail(FileVersion fileVersion, int index) {
690 try {
691 String imageType = getThumbnailType(fileVersion);
692
693 return DLStoreUtil.hasFile(
694 fileVersion.getCompanyId(), REPOSITORY_ID,
695 getThumbnailFilePath(fileVersion, imageType, index));
696 }
697 catch (Exception e) {
698 _log.error(e, e);
699 }
700
701 return false;
702 }
703
704 protected boolean hasThumbnails(FileVersion fileVersion) {
705 try {
706 if (isThumbnailEnabled(THUMBNAIL_INDEX_DEFAULT)) {
707 if (!hasThumbnail(fileVersion, THUMBNAIL_INDEX_DEFAULT)) {
708 return false;
709 }
710 }
711
712 if (isThumbnailEnabled(THUMBNAIL_INDEX_CUSTOM_1)) {
713 if (!hasThumbnail(fileVersion, THUMBNAIL_INDEX_CUSTOM_1)) {
714 return false;
715 }
716 }
717
718 if (isThumbnailEnabled(THUMBNAIL_INDEX_CUSTOM_2)) {
719 if (!hasThumbnail(fileVersion, THUMBNAIL_INDEX_CUSTOM_2)) {
720 return false;
721 }
722 }
723 }
724 catch (Exception e) {
725 _log.error(e, e);
726 }
727
728 return true;
729 }
730
731 protected void importPreview(
732 PortletDataContext portletDataContext, FileEntry fileEntry,
733 FileEntry importedFileEntry, Element fileEntryElement,
734 String binPathSuffix, String previewType)
735 throws Exception {
736
737 importPreview(
738 portletDataContext, fileEntry, importedFileEntry, fileEntryElement,
739 binPathSuffix, previewType, -1);
740 }
741
742 protected void importPreview(
743 PortletDataContext portletDataContext, FileEntry fileEntry,
744 FileEntry importedFileEntry, Element fileEntryElement,
745 String binPathSuffix, String previewType, int fileIndex)
746 throws Exception {
747
748 if (!portletDataContext.isPerformDirectBinaryImport()) {
749 importPreviewFromLAR(
750 portletDataContext, importedFileEntry, fileEntryElement,
751 binPathSuffix, fileIndex);
752 }
753 else {
754 FileVersion importedFileVersion =
755 importedFileEntry.getFileVersion();
756
757 String previewFilePath = getPreviewFilePath(
758 importedFileVersion, previewType);
759
760 FileVersion fileVersion = fileEntry.getFileVersion();
761
762 InputStream is = null;
763
764 if (fileIndex < 0) {
765 is = doGetPreviewAsStream(fileVersion, previewType);
766 }
767 else {
768 is = doGetPreviewAsStream(fileVersion, fileIndex, previewType);
769 }
770
771 addFileToStore(
772 portletDataContext.getCompanyId(), PREVIEW_PATH,
773 previewFilePath, is);
774 }
775 }
776
777 protected void importPreviewFromLAR(
778 PortletDataContext portletDataContext, FileEntry fileEntry,
779 Element fileEntryElement, String binPathSuffix, int fileIndex)
780 throws Exception {
781
782 FileVersion fileVersion = fileEntry.getFileVersion();
783
784 String binPathSegment = null;
785
786 if (fileIndex < 0) {
787 binPathSegment = getPreviewType(fileVersion);
788 }
789 else {
790 binPathSegment = Integer.toString(fileIndex + 1);
791 }
792
793 StringBundler sb = new StringBundler(4);
794
795 sb.append("bin-path-preview-");
796 sb.append(binPathSegment);
797 sb.append("-");
798 sb.append(binPathSuffix);
799
800 String binPathName = sb.toString();
801
802 String binPath = fileEntryElement.attributeValue(binPathName);
803
804 InputStream is = portletDataContext.getZipEntryAsInputStream(binPath);
805
806 if (is == null) {
807 return;
808 }
809
810 String previewFilePath = null;
811
812 if (fileIndex < 0) {
813 previewFilePath = getPreviewFilePath(
814 fileVersion, getPreviewType(fileVersion));
815 }
816 else {
817 previewFilePath = getPreviewFilePath(fileVersion, fileIndex + 1);
818 }
819
820 addFileToStore(
821 portletDataContext.getCompanyId(), PREVIEW_PATH, previewFilePath,
822 is);
823 }
824
825 protected void importThumbnail(
826 PortletDataContext portletDataContext, FileEntry fileEntry,
827 FileEntry importedFileEntry, Element fileEntryElement,
828 String binPathName, int index)
829 throws Exception {
830
831 if (!portletDataContext.isPerformDirectBinaryImport()) {
832 importThumbnailFromLAR(
833 portletDataContext, importedFileEntry, fileEntryElement,
834 binPathName, index);
835 }
836 else {
837 FileVersion fileVersion = fileEntry.getFileVersion();
838
839 if (!hasThumbnail(fileVersion, index)) {
840 return;
841 }
842
843 File file = doGetThumbnail(fileVersion, index);
844
845 try {
846 InputStream is = new FileInputStream(file);
847
848 FileVersion importedFileVersion =
849 importedFileEntry.getFileVersion();
850
851 String thumbnailFilePath = getThumbnailFilePath(
852 importedFileVersion, getThumbnailType(importedFileVersion),
853 index);
854
855 addFileToStore(
856 portletDataContext.getCompanyId(), THUMBNAIL_PATH,
857 thumbnailFilePath, is);
858 }
859 catch (IOException ioe) {
860 throw new SystemException(ioe);
861 }
862 }
863 }
864
865 protected void importThumbnailFromLAR(
866 PortletDataContext portletDataContext, FileEntry fileEntry,
867 Element fileEntryElement, String binPathName, int index)
868 throws Exception {
869
870 FileVersion fileVersion = fileEntry.getFileVersion();
871
872 String binPath = fileEntryElement.attributeValue(binPathName);
873
874 InputStream is = portletDataContext.getZipEntryAsInputStream(binPath);
875
876 if (is == null) {
877 return;
878 }
879
880 String thumbnailFilePath = getThumbnailFilePath(
881 fileVersion, getThumbnailType(fileVersion), index);
882
883 addFileToStore(
884 portletDataContext.getCompanyId(), THUMBNAIL_PATH,
885 thumbnailFilePath, is);
886 }
887
888 protected void importThumbnails(
889 PortletDataContext portletDataContext, FileEntry fileEntry,
890 FileEntry importedFileEntry, Element fileEntryElement,
891 String binPathSuffix)
892 throws Exception {
893
894 importThumbnail(
895 portletDataContext, fileEntry, importedFileEntry, fileEntryElement,
896 "bin-path-thumbnail-default-" + binPathSuffix,
897 THUMBNAIL_INDEX_DEFAULT);
898
899 importThumbnail(
900 portletDataContext, fileEntry, importedFileEntry, fileEntryElement,
901 "bin-path-thumbnail-custom-1-" + binPathSuffix,
902 THUMBNAIL_INDEX_CUSTOM_1);
903
904 importThumbnail(
905 portletDataContext, fileEntry, importedFileEntry, fileEntryElement,
906 "bin-path-thumbnail-custom-2-" + binPathSuffix,
907 THUMBNAIL_INDEX_CUSTOM_2);
908 }
909
910 protected boolean isThumbnailEnabled(int index) throws Exception {
911 if (index == THUMBNAIL_INDEX_DEFAULT) {
912 if (GetterUtil.getBoolean(
913 PropsUtil.get(
914 PropsKeys.DL_FILE_ENTRY_THUMBNAIL_ENABLED))) {
915
916 return true;
917 }
918 }
919 else if (index == THUMBNAIL_INDEX_CUSTOM_1) {
920 if ((PrefsPropsUtil.getInteger(
921 PropsKeys.
922 DL_FILE_ENTRY_THUMBNAIL_CUSTOM_1_MAX_HEIGHT) > 0) ||
923 (PrefsPropsUtil.getInteger(
924 PropsKeys.
925 DL_FILE_ENTRY_THUMBNAIL_CUSTOM_1_MAX_WIDTH) > 0)) {
926
927 return true;
928 }
929 }
930 else if (index == THUMBNAIL_INDEX_CUSTOM_2) {
931 if ((PrefsPropsUtil.getInteger(
932 PropsKeys.
933 DL_FILE_ENTRY_THUMBNAIL_CUSTOM_2_MAX_HEIGHT) > 0) ||
934 (PrefsPropsUtil.getInteger(
935 PropsKeys.
936 DL_FILE_ENTRY_THUMBNAIL_CUSTOM_2_MAX_WIDTH) > 0)) {
937
938 return true;
939 }
940 }
941
942 return false;
943 }
944
945 protected void storeThumbnailImages(FileVersion fileVersion, File file)
946 throws Exception {
947
948 ImageBag imageBag = ImageToolUtil.read(file);
949
950 RenderedImage renderedImage = imageBag.getRenderedImage();
951
952 storeThumbnailImages(fileVersion, renderedImage);
953 }
954
955 protected void storeThumbnailImages(
956 FileVersion fileVersion, RenderedImage renderedImage)
957 throws Exception {
958
959 storeThumbnailmage(fileVersion, renderedImage, THUMBNAIL_INDEX_DEFAULT);
960 storeThumbnailmage(
961 fileVersion, renderedImage, THUMBNAIL_INDEX_CUSTOM_1);
962 storeThumbnailmage(
963 fileVersion, renderedImage, THUMBNAIL_INDEX_CUSTOM_2);
964 }
965
966 protected void storeThumbnailmage(
967 FileVersion fileVersion, RenderedImage renderedImage, int index)
968 throws Exception {
969
970 if (!isThumbnailEnabled(index) || hasThumbnail(fileVersion, index)) {
971 return;
972 }
973
974 String type = getThumbnailType(fileVersion);
975
976 String maxHeightPropsKey = PropsKeys.DL_FILE_ENTRY_THUMBNAIL_MAX_HEIGHT;
977 String maxWidthPropsKey = PropsKeys.DL_FILE_ENTRY_THUMBNAIL_MAX_WIDTH;
978
979 if (index == THUMBNAIL_INDEX_CUSTOM_1) {
980 maxHeightPropsKey =
981 PropsKeys.DL_FILE_ENTRY_THUMBNAIL_CUSTOM_1_MAX_HEIGHT;
982 maxWidthPropsKey =
983 PropsKeys.DL_FILE_ENTRY_THUMBNAIL_CUSTOM_1_MAX_WIDTH;
984 }
985 else if (index == THUMBNAIL_INDEX_CUSTOM_2) {
986 maxHeightPropsKey =
987 PropsKeys.DL_FILE_ENTRY_THUMBNAIL_CUSTOM_2_MAX_HEIGHT;
988 maxWidthPropsKey =
989 PropsKeys.DL_FILE_ENTRY_THUMBNAIL_CUSTOM_2_MAX_WIDTH;
990 }
991
992 RenderedImage thumbnailRenderedImage = ImageToolUtil.scale(
993 renderedImage, PrefsPropsUtil.getInteger(maxHeightPropsKey),
994 PrefsPropsUtil.getInteger(maxWidthPropsKey));
995
996 byte[] bytes = ImageToolUtil.getBytes(thumbnailRenderedImage, type);
997
998 File file = FileUtil.createTempFile(bytes);
999
1000 try {
1001 addFileToStore(
1002 fileVersion.getCompanyId(), THUMBNAIL_PATH,
1003 getThumbnailFilePath(fileVersion, type, index), file);
1004 }
1005 finally {
1006 FileUtil.delete(file);
1007 }
1008 }
1009
1010 private static Log _log = LogFactoryUtil.getLog(
1011 DLPreviewableProcessor.class);
1012
1013 }