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 <[email protected]> 018 * Antoine Taillefer <[email protected]> 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 org.apache.commons.logging.Log; 032import org.apache.commons.logging.LogFactory; 033import org.nuxeo.common.utils.FileUtils; 034import org.nuxeo.ecm.core.api.Blob; 035import org.nuxeo.ecm.core.api.Blobs; 036import org.nuxeo.ecm.core.api.CoreSession; 037import org.nuxeo.ecm.core.api.DocumentModel; 038import org.nuxeo.ecm.core.api.NuxeoException; 039import org.nuxeo.ecm.core.api.PropertyException; 040import org.nuxeo.ecm.core.api.blobholder.BlobHolder; 041import org.nuxeo.ecm.core.api.thumbnail.ThumbnailFactory; 042import org.nuxeo.ecm.core.convert.api.ConversionService; 043import org.nuxeo.ecm.platform.mimetype.interfaces.MimetypeRegistry; 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<>(); 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 116 try { 117 File iconFile = FileUtils.getResourceFileFromContext("nuxeo.war" + File.separator + iconPath); 118 if (iconFile.exists()) { 119 MimetypeRegistry mimetypeRegistry = Framework.getService(MimetypeRegistry.class); 120 String mimeType = mimetypeRegistry.getMimetypeFromFile(iconFile); 121 if (mimeType == null) { 122 mimeType = mimetypeRegistry.getMimetypeFromFilename(iconPath); 123 } 124 return Blobs.createBlob(iconFile, mimeType); 125 } 126 } catch (IOException e) { 127 log.warn(String.format("Could not fetch the thumbnail blob from icon path '%s'", iconPath), e); 128 } 129 130 return null; 131 } 132 133}