001/* 002 * (C) Copyright 2015 Nuxeo SA (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-2.1.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 * Nelson Silva 016 */ 017package org.nuxeo.ecm.liveconnect.google.drive.converter; 018 019import java.io.IOException; 020import java.io.InputStream; 021import java.io.Serializable; 022import java.util.Map; 023 024import org.nuxeo.ecm.core.api.Blob; 025import org.nuxeo.ecm.core.api.Blobs; 026import org.nuxeo.ecm.core.api.DocumentModel; 027import org.nuxeo.ecm.core.api.blobholder.BlobHolder; 028import org.nuxeo.ecm.core.api.blobholder.DocumentBlobHolder; 029import org.nuxeo.ecm.core.blob.BlobManager; 030import org.nuxeo.ecm.core.convert.api.ConversionException; 031import org.nuxeo.ecm.core.convert.cache.SimpleCachableBlobHolder; 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 * Converter that relies on {@link org.nuxeo.ecm.core.blob.BlobProvider} conversions. 038 * 039 * @since 7.3 040 */ 041public class GoogleDriveBlobConverter implements Converter { 042 043 protected ConverterDescriptor descriptor; 044 045 @Override 046 public void init(ConverterDescriptor descriptor) { 047 this.descriptor = descriptor; 048 } 049 050 @Override 051 public BlobHolder convert(BlobHolder blobHolder, Map<String, Serializable> parameters) throws ConversionException { 052 053 Blob srcBlob, dstBlob; 054 055 srcBlob = blobHolder.getBlob(); 056 if (srcBlob == null) { 057 return null; 058 } 059 060 try { 061 DocumentModel doc = null; 062 if (blobHolder instanceof DocumentBlobHolder) { 063 doc = ((DocumentBlobHolder) blobHolder).getDocument(); 064 } 065 dstBlob = convert(srcBlob, doc); 066 } catch (IOException e) { 067 throw new ConversionException("Unable to fetch conversion", e); 068 } 069 070 if (dstBlob == null) { 071 return null; 072 } 073 074 return new SimpleCachableBlobHolder(dstBlob); 075 } 076 077 protected Blob convert(Blob blob, DocumentModel doc) throws IOException { 078 String mimetype = descriptor.getDestinationMimeType(); 079 InputStream is = Framework.getService(BlobManager.class).getConvertedStream(blob, mimetype, doc); 080 return is == null ? null : Blobs.createBlob(is, mimetype); 081 } 082}