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.IOException;
026import java.io.InputStream;
027import java.io.Serializable;
028import java.util.HashMap;
029import java.util.Map;
030
031import javax.faces.context.FacesContext;
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.thumbnail.ThumbnailConstants;
045import org.nuxeo.ecm.platform.types.adapter.TypeInfo;
046import org.nuxeo.runtime.api.Framework;
047
048/**
049 * Default thumbnail factory for all non folderish documents Return the main blob converted in thumbnail or get the
050 * document big icon as a thumbnail
051 *
052 * @since 5.7
053 */
054public class ThumbnailDocumentFactory implements ThumbnailFactory {
055
056    private static final Log log = LogFactory.getLog(ThumbnailDocumentFactory.class);
057
058    @Override
059    public Blob getThumbnail(DocumentModel doc, CoreSession session) {
060        Blob thumbnailBlob = null;
061        try {
062            if (doc.hasFacet(ThumbnailConstants.THUMBNAIL_FACET)) {
063                thumbnailBlob = (Blob) doc.getPropertyValue(ThumbnailConstants.THUMBNAIL_PROPERTY_NAME);
064            }
065        } catch (PropertyException e) {
066            log.warn("Could not fetch the thumbnail blob", e);
067        }
068        if (thumbnailBlob == null) {
069            thumbnailBlob = getDefaultThumbnail(doc);
070        }
071        return thumbnailBlob;
072    }
073
074    @Override
075    public Blob computeThumbnail(DocumentModel doc, CoreSession session) {
076        // TODO: convert non pix attachment
077        // Image converter before thumbnail converter
078        // params.put("targetFilePath", "readyToThumbnail.png");
079        // BlobHolder bh = conversionService.convertToMimeType(
080        // ThumbnailConstants.THUMBNAIL_MIME_TYPE, blobHolder,
081        // params);
082        // params.clear();
083        ConversionService conversionService;
084        Blob thumbnailBlob = null;
085        try {
086            conversionService = Framework.getService(ConversionService.class);
087            BlobHolder bh = doc.getAdapter(BlobHolder.class);
088            if (bh != null) {
089                Map<String, Serializable> params = new HashMap<String, Serializable>();
090                // Thumbnail converter
091                params.put(ThumbnailConstants.THUMBNAIL_SIZE_PARAMETER_NAME, ThumbnailConstants.THUMBNAIL_DEFAULT_SIZE);
092                bh = conversionService.convert(ANY_TO_THUMBNAIL_CONVERTER_NAME, bh, params);
093                if (bh != null) {
094                    thumbnailBlob = bh.getBlob();
095                }
096            }
097        } catch (NuxeoException e) {
098            log.warn("Cannot compute document thumbnail", e);
099        }
100        return thumbnailBlob;
101    }
102
103    protected Blob getDefaultThumbnail(DocumentModel doc) {
104        if (doc == null) {
105            return null;
106        }
107        TypeInfo docType = doc.getAdapter(TypeInfo.class);
108        String iconPath = docType.getBigIcon();
109        if (iconPath == null) {
110            iconPath = docType.getIcon();
111        }
112        if (iconPath == null) {
113            return null;
114        }
115        FacesContext ctx = FacesContext.getCurrentInstance();
116        if (ctx == null) {
117            return null;
118        }
119        try {
120            try (InputStream iconStream = ctx.getExternalContext().getResourceAsStream(iconPath)) {
121                if (iconStream != null) {
122                    return Blobs.createBlob(iconStream);
123                }
124            }
125        } catch (IOException e) {
126            log.warn(String.format("Could not fetch the thumbnail blob from icon path '%s'", iconPath), e);
127        }
128        return null;
129    }
130
131}