001/* 002 * Copyright (c) 2006-2011 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the Eclipse Public License v1.0 006 * which accompanies this distribution, and is available at 007 * http://www.eclipse.org/legal/epl-v10.html 008 * 009 * Contributors: 010 * bstefanescu 011 */ 012package org.nuxeo.ecm.automation.core.operations.blob; 013 014import java.io.Serializable; 015import java.util.HashMap; 016 017import org.nuxeo.ecm.automation.core.Constants; 018import org.nuxeo.ecm.automation.core.annotations.Context; 019import org.nuxeo.ecm.automation.core.annotations.Operation; 020import org.nuxeo.ecm.automation.core.annotations.OperationMethod; 021import org.nuxeo.ecm.automation.core.util.BlobList; 022import org.nuxeo.ecm.core.api.Blob; 023import org.nuxeo.ecm.core.api.DocumentModel; 024import org.nuxeo.ecm.core.api.blobholder.BlobHolder; 025import org.nuxeo.ecm.core.api.blobholder.SimpleBlobHolder; 026import org.nuxeo.ecm.core.convert.api.ConversionService; 027 028/** 029 * Save the input document 030 * 031 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a> 032 * @author tiry 033 */ 034@Operation(id = BlobToPDF.ID, category = Constants.CAT_CONVERSION, label = "Convert To PDF", description = "Convert the input file to a PDF and return the new file.") 035public class BlobToPDF { 036 037 public static final String ID = "Blob.ToPDF"; 038 039 @Context 040 protected ConversionService service; 041 042 @OperationMethod 043 public Blob run(DocumentModel doc) { 044 BlobHolder bh = doc.getAdapter(BlobHolder.class); 045 if (bh == null) { 046 return null; 047 } 048 if ("application/pdf".equals(bh.getBlob().getMimeType())) { 049 return bh.getBlob(); 050 } 051 BlobHolder pdfBh = service.convertToMimeType("application/pdf", bh, new HashMap<String, Serializable>()); 052 Blob result = pdfBh.getBlob(); 053 054 String fname = result.getFilename(); 055 String filename = bh.getBlob().getFilename(); 056 if (filename != null && !filename.isEmpty()) { 057 // add pdf extension 058 int pos = filename.lastIndexOf('.'); 059 if (pos > 0) { 060 filename = filename.substring(0, pos); 061 } 062 filename += ".pdf"; 063 result.setFilename(filename); 064 } else if (fname != null && !fname.isEmpty()) { 065 result.setFilename(fname); 066 } else { 067 result.setFilename("file"); 068 } 069 070 result.setMimeType("application/pdf"); 071 return result; 072 } 073 074 @OperationMethod 075 public Blob run(Blob blob) { 076 if ("application/pdf".equals(blob.getMimeType())) { 077 return blob; 078 } 079 BlobHolder bh = new SimpleBlobHolder(blob); 080 bh = service.convertToMimeType("application/pdf", bh, new HashMap<String, Serializable>()); 081 Blob result = bh.getBlob(); 082 adjustBlobName(blob, result); 083 return result; 084 } 085 086 @OperationMethod 087 public BlobList run(BlobList blobs) { 088 BlobList bl = new BlobList(); 089 for (Blob blob : blobs) { 090 bl.add(this.run(blob)); 091 } 092 return bl; 093 } 094 095 protected void adjustBlobName(Blob in, Blob out) { 096 String fname = in.getFilename(); 097 if (fname == null) { 098 fname = "Unknown_" + System.identityHashCode(in); 099 } 100 out.setFilename(fname + ".pdf"); 101 out.setMimeType("application/pdf"); 102 } 103 104}