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"); 060 } 061 // look for the pdf file 062 if (ZipUtils.hasEntry(blob.getStream(), IWORK_PREVIEW_FILE)) { 063 // pdf file exist, let's extract it and return it as a 064 // BlobHolder. 065 Blob previewBlob; 066 try (InputStream previewPDFFile = ZipUtils.getEntryContentAsStream(blob.getStream(), IWORK_PREVIEW_FILE)) { 067 previewBlob = Blobs.createBlob(previewPDFFile); 068 } 069 return new SimpleCachableBlobHolder(previewBlob); 070 } else { 071 // Pdf file does not exist, conversion cannot be done. 072 throw new ConversionException("iWork file does not contain a pdf preview."); 073 } 074 } catch (IOException e) { 075 throw new ConversionException("Could not find the pdf preview in the iWork file", e); 076 } 077 } 078 079 @Override 080 public void init(ConverterDescriptor descriptor) { 081 } 082 083}