001/*
002 * (C) Copyright 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 *     Thomas Roger
018 */
019
020package org.nuxeo.ecm.platform.thumbnail.converter;
021
022import static org.nuxeo.ecm.platform.thumbnail.ThumbnailConstants.ANY_TO_PDF_TO_THUMBNAIL_CONVERTER_NAME;
023import static org.nuxeo.ecm.platform.thumbnail.ThumbnailConstants.PDF_AND_IMAGE_TO_THUMBNAIL_CONVERTER_NAME;
024
025import java.io.Serializable;
026import java.util.Map;
027import java.util.regex.Pattern;
028
029import org.nuxeo.ecm.core.api.Blob;
030import org.nuxeo.ecm.core.api.blobholder.BlobHolder;
031import org.nuxeo.ecm.core.convert.api.ConversionService;
032import org.nuxeo.ecm.core.convert.extension.Converter;
033import org.nuxeo.ecm.core.convert.extension.ConverterDescriptor;
034import org.nuxeo.runtime.api.Framework;
035
036/**
037 * Base converter choosing the correct convert to generate a thumbnail according to the Blob's mime type.
038 *
039 * @since 5.8
040 */
041public class AnyToThumbnailConverter implements Converter {
042
043    public static final String PDF_MIME_TYPE = "application/pdf";
044
045    public static final Pattern PDF_MIME_TYPE_PATTERN = Pattern.compile("application/.*pdf");
046
047    public static final String ANY_TO_PDF_CONVERTER_NAME = "any2pdf";
048
049    @Override
050    public void init(ConverterDescriptor descriptor) {
051    }
052
053    @Override
054    public BlobHolder convert(BlobHolder blobHolder, Map<String, Serializable> parameters) {
055        Blob sourceBlob;
056        sourceBlob = blobHolder.getBlob();
057        if (sourceBlob == null) {
058            return null;
059        }
060
061        String mimeType = sourceBlob.getMimeType();
062        if (mimeType == null) {
063            return null;
064        }
065
066        ConversionService conversionService = Framework.getService(ConversionService.class);
067
068        String converterName = null;
069        if ((mimeType.startsWith("image/") || PDF_MIME_TYPE_PATTERN.matcher(mimeType).matches())
070                && conversionService.isConverterAvailable(PDF_AND_IMAGE_TO_THUMBNAIL_CONVERTER_NAME, true)
071                                    .isAvailable()) {
072            converterName = PDF_AND_IMAGE_TO_THUMBNAIL_CONVERTER_NAME;
073        } else if (conversionService.isSourceMimeTypeSupported(ANY_TO_PDF_CONVERTER_NAME, mimeType)
074                && conversionService.isConverterAvailable(ANY_TO_PDF_CONVERTER_NAME, true).isAvailable()) {
075            converterName = ANY_TO_PDF_TO_THUMBNAIL_CONVERTER_NAME;
076        }
077        return converterName == null ? null : conversionService.convert(converterName, blobHolder, parameters);
078    }
079}