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