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.Map;
024
025import org.nuxeo.common.utils.FileUtils;
026import org.nuxeo.ecm.core.api.Blob;
027import org.nuxeo.ecm.core.api.blobholder.BlobHolder;
028import org.nuxeo.ecm.core.api.blobholder.SimpleBlobHolder;
029import org.nuxeo.ecm.core.convert.api.ConversionService;
030import org.nuxeo.ecm.platform.mimetype.interfaces.MimetypeRegistry;
031import org.nuxeo.runtime.api.Framework;
032
033/**
034 * @since 5.7
035 */
036public class ConvertHelper {
037
038    protected String findConverter(Blob blob, String destMimeType) {
039        MimetypeRegistry mtr = Framework.getService(MimetypeRegistry.class);
040        String srcMt = mtr.getMimetypeFromFilenameAndBlobWithDefault(blob.getFilename(), blob, blob.getMimeType());
041        blob.setMimeType(srcMt);
042        ConversionService cs = Framework.getService(ConversionService.class);
043        return cs.getConverterName(srcMt, destMimeType);
044    }
045
046    protected Blob applyConverter(Blob blob, String converter, String destMimeType, Map<String, Serializable> params) {
047        ConversionService cs = Framework.getService(ConversionService.class);
048        BlobHolder bh = cs.convert(converter, new SimpleBlobHolder(blob), params);
049
050        if (bh == null || bh.getBlob() == null) {
051            return blob;
052        } else {
053            Blob result = bh.getBlob();
054            MimetypeRegistry mtr = Framework.getService(MimetypeRegistry.class);
055            String filename = FileUtils.getFileNameNoExt(blob.getFilename());
056            filename = filename + "." + mtr.getExtensionsFromMimetypeName(destMimeType).get(0);
057            result.setFilename(filename);
058            if (result.getMimeType() == null) {
059                result.setMimeType(destMimeType);
060            }
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}