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