001/* 002 * (C) Copyright 2002-2012 Nuxeo SAS (http://nuxeo.com/) and contributors. 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the GNU Lesser General Public License 006 * (LGPL) version 2.1 which accompanies this distribution, and is available at 007 * http://www.gnu.org/licenses/lgpl.html 008 * 009 * This library is distributed in the hope that it will be useful, 010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 012 * Lesser General Public License for more details. 013 * 014 * Contributors: 015 * Nuxeo - initial API and implementation 016 * 017 */ 018 019package org.nuxeo.ecm.platform.convert.plugins; 020 021import java.io.IOException; 022import java.io.InputStream; 023import java.io.Serializable; 024import java.util.Arrays; 025import java.util.List; 026import java.util.Map; 027 028import org.nuxeo.common.utils.ZipUtils; 029import org.nuxeo.ecm.core.api.Blob; 030import org.nuxeo.ecm.core.api.Blobs; 031import org.nuxeo.ecm.core.api.blobholder.BlobHolder; 032import org.nuxeo.ecm.core.convert.api.ConversionException; 033import org.nuxeo.ecm.core.convert.cache.SimpleCachableBlobHolder; 034import org.nuxeo.ecm.core.convert.extension.Converter; 035import org.nuxeo.ecm.core.convert.extension.ConverterDescriptor; 036 037/** 038 * iWork2PDF converter. 039 * 040 * @author ldoguin 041 */ 042public class IWork2PDFConverter implements Converter { 043 044 public static final List<String> IWORK_MIME_TYPES = Arrays.asList(new String[] { "application/vnd.apple.pages", 045 "application/vnd.apple.keynote", "application/vnd.apple.numbers", "application/vnd.apple.iwork" }); 046 047 private static final String IWORK_PREVIEW_FILE = "QuickLook/Preview.pdf"; 048 049 @Override 050 public BlobHolder convert(BlobHolder blobHolder, Map<String, Serializable> parameters) throws ConversionException { 051 try { 052 // retrieve the blob and verify its mimeType 053 Blob blob = blobHolder.getBlob(); 054 String mimeType = blob.getMimeType(); 055 056 if (mimeType == null || !IWORK_MIME_TYPES.contains(mimeType)) { 057 throw new ConversionException("not an iWork file"); 058 } 059 // look for the pdf file 060 if (ZipUtils.hasEntry(blob.getStream(), IWORK_PREVIEW_FILE)) { 061 // pdf file exist, let's extract it and return it as a 062 // BlobHolder. 063 Blob previewBlob; 064 try (InputStream previewPDFFile = ZipUtils.getEntryContentAsStream(blob.getStream(), IWORK_PREVIEW_FILE)) { 065 previewBlob = Blobs.createBlob(previewPDFFile); 066 } 067 return new SimpleCachableBlobHolder(previewBlob); 068 } else { 069 // Pdf file does not exist, conversion cannot be done. 070 throw new ConversionException("iWork file does not contain a pdf preview."); 071 } 072 } catch (IOException e) { 073 throw new ConversionException("Could not find the pdf preview in the iWork file", e); 074 } 075 } 076 077 @Override 078 public void init(ConverterDescriptor descriptor) { 079 } 080 081}