001/*
002 * (C) Copyright 2006-2013 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Vladimir Pasquier <vpasquier@nuxeo.com>
018 *     Antoine Taillefer <ataillefer@nuxeo.com>
019 *
020 */
021package org.nuxeo.ecm.platform.thumbnail.factories;
022
023import static org.nuxeo.ecm.platform.thumbnail.ThumbnailConstants.ANY_TO_THUMBNAIL_CONVERTER_NAME;
024
025import java.io.File;
026import java.io.IOException;
027import java.io.Serializable;
028import java.util.HashMap;
029import java.util.Map;
030
031import javax.servlet.ServletContext;
032
033import org.apache.commons.logging.Log;
034import org.apache.commons.logging.LogFactory;
035import org.nuxeo.ecm.core.api.Blob;
036import org.nuxeo.ecm.core.api.Blobs;
037import org.nuxeo.ecm.core.api.CoreSession;
038import org.nuxeo.ecm.core.api.DocumentModel;
039import org.nuxeo.ecm.core.api.NuxeoException;
040import org.nuxeo.ecm.core.api.PropertyException;
041import org.nuxeo.ecm.core.api.blobholder.BlobHolder;
042import org.nuxeo.ecm.core.api.thumbnail.ThumbnailFactory;
043import org.nuxeo.ecm.core.convert.api.ConversionService;
044import org.nuxeo.ecm.platform.mimetype.interfaces.MimetypeRegistry;
045import org.nuxeo.ecm.platform.thumbnail.ThumbnailConstants;
046import org.nuxeo.ecm.platform.types.adapter.TypeInfo;
047import org.nuxeo.ecm.platform.web.common.ServletHelper;
048import org.nuxeo.runtime.api.Framework;
049
050/**
051 * Default thumbnail factory for all non folderish documents Return the main blob converted in thumbnail or get the
052 * document big icon as a thumbnail
053 *
054 * @since 5.7
055 */
056public class ThumbnailDocumentFactory implements ThumbnailFactory {
057
058    private static final Log log = LogFactory.getLog(ThumbnailDocumentFactory.class);
059
060    @Override
061    public Blob getThumbnail(DocumentModel doc, CoreSession session) {
062        Blob thumbnailBlob = null;
063        try {
064            if (doc.hasFacet(ThumbnailConstants.THUMBNAIL_FACET)) {
065                thumbnailBlob = (Blob) doc.getPropertyValue(ThumbnailConstants.THUMBNAIL_PROPERTY_NAME);
066            }
067        } catch (PropertyException e) {
068            log.warn("Could not fetch the thumbnail blob", e);
069        }
070        if (thumbnailBlob == null) {
071            thumbnailBlob = getDefaultThumbnail(doc);
072        }
073        return thumbnailBlob;
074    }
075
076    @Override
077    public Blob computeThumbnail(DocumentModel doc, CoreSession session) {
078        // TODO: convert non pix attachment
079        // Image converter before thumbnail converter
080        // params.put("targetFilePath", "readyToThumbnail.png");
081        // BlobHolder bh = conversionService.convertToMimeType(
082        // ThumbnailConstants.THUMBNAIL_MIME_TYPE, blobHolder,
083        // params);
084        // params.clear();
085        ConversionService conversionService;
086        Blob thumbnailBlob = null;
087        try {
088            conversionService = Framework.getService(ConversionService.class);
089            BlobHolder bh = doc.getAdapter(BlobHolder.class);
090            if (bh != null) {
091                Map<String, Serializable> params = new HashMap<String, Serializable>();
092                // Thumbnail converter
093                params.put(ThumbnailConstants.THUMBNAIL_SIZE_PARAMETER_NAME, ThumbnailConstants.THUMBNAIL_DEFAULT_SIZE);
094                bh = conversionService.convert(ANY_TO_THUMBNAIL_CONVERTER_NAME, bh, params);
095                if (bh != null) {
096                    thumbnailBlob = bh.getBlob();
097                }
098            }
099        } catch (NuxeoException e) {
100            log.warn("Cannot compute document thumbnail", e);
101        }
102        return thumbnailBlob;
103    }
104
105    protected Blob getDefaultThumbnail(DocumentModel doc) {
106        if (doc == null) {
107            return null;
108        }
109        TypeInfo docType = doc.getAdapter(TypeInfo.class);
110        String iconPath = docType.getBigIcon();
111        if (iconPath == null) {
112            iconPath = docType.getIcon();
113        }
114        if (iconPath == null) {
115            return null;
116        }
117
118        ServletContext servletContext = ServletHelper.getServletContext();
119        String path = servletContext.getRealPath(iconPath);
120        if (path == null) {
121            return null;
122        }
123
124        try {
125            File iconFile = new File(path);
126            if (iconFile.exists()) {
127                String mimeType = servletContext.getMimeType(path);
128                if (mimeType == null) {
129                    MimetypeRegistry mimetypeRegistry = Framework.getService(MimetypeRegistry.class);
130                    mimeType = mimetypeRegistry.getMimetypeFromFilename(iconPath);
131                }
132                return Blobs.createBlob(iconFile, mimeType);
133            }
134        } catch (IOException e) {
135            log.warn(String.format("Could not fetch the thumbnail blob from icon path '%s'", iconPath), e);
136        }
137
138        return null;
139    }
140
141}