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        BlobHolder bh = cs.convert(converter, new SimpleBlobHolder(blob), params);
050
051        if (bh == null || bh.getBlob() == null) {
052            return blob;
053        } else {
054            Blob result = bh.getBlob();
055            MimetypeRegistry mtr = Framework.getLocalService(MimetypeRegistry.class);
056            String filename = FileUtils.getFileNameNoExt(blob.getFilename());
057            filename = filename + "." + mtr.getExtensionsFromMimetypeName(destMimeType).get(0);
058            result.setFilename(filename);
059            return result;
060        }
061    }
062
063    public Blob convertBlob(Blob blob, String mimeType) {
064        String converter = findConverter(blob, mimeType);
065        if (converter != null) {
066            return applyConverter(blob, converter, mimeType, null);
067        }
068        return blob;
069    }
070
071    public Blob convertBlob(Blob blob, String mimeType, Map<String, Serializable> params) {
072        String converter = findConverter(blob, mimeType);
073        if (converter != null) {
074            return applyConverter(blob, converter, mimeType, params);
075        }
076        return blob;
077    }
078}