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