001/*
002 * (C) Copyright 2006-2012 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 */
020
021package org.nuxeo.ecm.platform.convert.plugins;
022
023import java.io.IOException;
024import java.io.InputStream;
025import java.io.Serializable;
026import java.util.Arrays;
027import java.util.List;
028import java.util.Map;
029
030import org.nuxeo.common.utils.ZipUtils;
031import org.nuxeo.ecm.core.api.Blob;
032import org.nuxeo.ecm.core.api.Blobs;
033import org.nuxeo.ecm.core.api.blobholder.BlobHolder;
034import org.nuxeo.ecm.core.convert.api.ConversionException;
035import org.nuxeo.ecm.core.convert.cache.SimpleCachableBlobHolder;
036import org.nuxeo.ecm.core.convert.extension.Converter;
037import org.nuxeo.ecm.core.convert.extension.ConverterDescriptor;
038
039/**
040 * iWork2PDF converter.
041 *
042 * @author ldoguin
043 */
044public class IWork2PDFConverter implements Converter {
045
046    public static final List<String> IWORK_MIME_TYPES = Arrays.asList(new String[] { "application/vnd.apple.pages",
047            "application/vnd.apple.keynote", "application/vnd.apple.numbers", "application/vnd.apple.iwork" });
048
049    private static final String IWORK_PREVIEW_FILE = "QuickLook/Preview.pdf";
050
051    @Override
052    public BlobHolder convert(BlobHolder blobHolder, Map<String, Serializable> parameters) throws ConversionException {
053        try {
054            // retrieve the blob and verify its mimeType
055            Blob blob = blobHolder.getBlob();
056            String mimeType = blob.getMimeType();
057
058            if (mimeType == null || !IWORK_MIME_TYPES.contains(mimeType)) {
059                throw new ConversionException("not an iWork file", blobHolder);
060            }
061
062            // check if the stream represents a valid zip
063            try (InputStream blobStream = blob.getStream()) {
064                if (!ZipUtils.isValid(blobStream)) {
065                    throw new ConversionException("not a valid iWork file", blobHolder);
066                }
067            }
068
069            // look for the pdf file
070            try (InputStream blobStream = blob.getStream()) {
071                if (ZipUtils.hasEntry(blobStream, IWORK_PREVIEW_FILE)) {
072                    // pdf file exist, let's extract it and return it as a
073                    // BlobHolder.
074                    Blob previewBlob;
075                    try (InputStream previewPDFFile = ZipUtils.getEntryContentAsStream(blob.getStream(),
076                            IWORK_PREVIEW_FILE)) {
077                        previewBlob = Blobs.createBlob(previewPDFFile);
078                    }
079                    return new SimpleCachableBlobHolder(previewBlob);
080                } else {
081                    // Pdf file does not exist, conversion cannot be done.
082                    throw new ConversionException("iWork file does not contain a pdf preview.", blobHolder);
083                }
084            }
085        } catch (IOException e) {
086            throw new ConversionException("Could not find the pdf preview in the iWork file", blobHolder, e);
087        }
088    }
089
090    @Override
091    public void init(ConverterDescriptor descriptor) {
092    }
093
094}