001/*
002 * (C) Copyright 2006-2013 Nuxeo SAS (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Nuxeo - initial API and implementation
016 *
017 */
018package org.nuxeo.ecm.platform.convert;
019
020import java.io.Serializable;
021import java.util.HashMap;
022import java.util.Map;
023
024import org.nuxeo.common.utils.FileUtils;
025import org.nuxeo.ecm.core.api.Blob;
026import org.nuxeo.ecm.core.api.blobholder.BlobHolder;
027import org.nuxeo.ecm.core.api.blobholder.SimpleBlobHolder;
028import org.nuxeo.ecm.core.convert.api.ConversionService;
029import org.nuxeo.ecm.platform.mimetype.interfaces.MimetypeRegistry;
030import org.nuxeo.runtime.api.Framework;
031
032/**
033 * @since 5.7
034 */
035public class ConvertHelper {
036
037    protected String findConverter(Blob blob, String destMimeType) {
038        MimetypeRegistry mtr = Framework.getLocalService(MimetypeRegistry.class);
039        String srcMt = mtr.getMimetypeFromFilenameAndBlobWithDefault(blob.getFilename(), blob, blob.getMimeType());
040        blob.setMimeType(srcMt);
041        ConversionService cs = Framework.getLocalService(ConversionService.class);
042        return cs.getConverterName(srcMt, destMimeType);
043    }
044
045    protected Blob applyConverter(Blob blob, String converter, String destMimeType, Map<String, Serializable> params) {
046        ConversionService cs = Framework.getLocalService(ConversionService.class);
047        if (params == null) {
048            params = new HashMap<String, Serializable>();
049            params.put("updateDocumentIndex", Boolean.TRUE);
050        }
051        BlobHolder bh = cs.convert(converter, new SimpleBlobHolder(blob), params);
052
053        if (bh == null || bh.getBlob() == null) {
054            return blob;
055        } else {
056            Blob result = bh.getBlob();
057            MimetypeRegistry mtr = Framework.getLocalService(MimetypeRegistry.class);
058            String filename = FileUtils.getFileNameNoExt(blob.getFilename());
059            filename = filename + "." + mtr.getExtensionsFromMimetypeName(destMimeType).get(0);
060            result.setFilename(filename);
061            return result;
062        }
063    }
064
065    public Blob convertBlob(Blob blob, String mimeType) {
066        String converter = findConverter(blob, mimeType);
067        if (converter != null) {
068            return applyConverter(blob, converter, mimeType, null);
069        }
070        return blob;
071    }
072
073    public Blob convertBlob(Blob blob, String mimeType, Map<String, Serializable> params) {
074        String converter = findConverter(blob, mimeType);
075        if (converter != null) {
076            return applyConverter(blob, converter, mimeType, params);
077        }
078        return blob;
079    }
080}