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 * Nuxeo - initial API and implementation 018 * 019 */ 020package org.nuxeo.ecm.platform.convert; 021 022import java.io.Serializable; 023import java.util.HashMap; 024import java.util.Map; 025 026import org.nuxeo.common.utils.FileUtils; 027import org.nuxeo.ecm.core.api.Blob; 028import org.nuxeo.ecm.core.api.blobholder.BlobHolder; 029import org.nuxeo.ecm.core.api.blobholder.SimpleBlobHolder; 030import org.nuxeo.ecm.core.convert.api.ConversionService; 031import org.nuxeo.ecm.platform.mimetype.interfaces.MimetypeRegistry; 032import org.nuxeo.runtime.api.Framework; 033 034/** 035 * @since 5.7 036 */ 037public class ConvertHelper { 038 039 protected String findConverter(Blob blob, String destMimeType) { 040 MimetypeRegistry mtr = Framework.getLocalService(MimetypeRegistry.class); 041 String srcMt = mtr.getMimetypeFromFilenameAndBlobWithDefault(blob.getFilename(), blob, blob.getMimeType()); 042 blob.setMimeType(srcMt); 043 ConversionService cs = Framework.getLocalService(ConversionService.class); 044 return cs.getConverterName(srcMt, destMimeType); 045 } 046 047 protected Blob applyConverter(Blob blob, String converter, String destMimeType, Map<String, Serializable> params) { 048 ConversionService cs = Framework.getLocalService(ConversionService.class); 049 if (params == null) { 050 params = new HashMap<String, Serializable>(); 051 params.put("updateDocumentIndex", Boolean.TRUE); 052 } 053 BlobHolder bh = cs.convert(converter, new SimpleBlobHolder(blob), params); 054 055 if (bh == null || bh.getBlob() == null) { 056 return blob; 057 } else { 058 Blob result = bh.getBlob(); 059 MimetypeRegistry mtr = Framework.getLocalService(MimetypeRegistry.class); 060 String filename = FileUtils.getFileNameNoExt(blob.getFilename()); 061 filename = filename + "." + mtr.getExtensionsFromMimetypeName(destMimeType).get(0); 062 result.setFilename(filename); 063 return result; 064 } 065 } 066 067 public Blob convertBlob(Blob blob, String mimeType) { 068 String converter = findConverter(blob, mimeType); 069 if (converter != null) { 070 return applyConverter(blob, converter, mimeType, null); 071 } 072 return blob; 073 } 074 075 public Blob convertBlob(Blob blob, String mimeType, Map<String, Serializable> params) { 076 String converter = findConverter(blob, mimeType); 077 if (converter != null) { 078 return applyConverter(blob, converter, mimeType, params); 079 } 080 return blob; 081 } 082}