001/*
002 * (C) Copyright 2006-2011 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 *     bstefanescu
018 */
019package org.nuxeo.ecm.automation.core.operations.blob;
020
021import java.io.Serializable;
022import java.util.HashMap;
023
024import org.nuxeo.ecm.automation.core.Constants;
025import org.nuxeo.ecm.automation.core.annotations.Context;
026import org.nuxeo.ecm.automation.core.annotations.Operation;
027import org.nuxeo.ecm.automation.core.annotations.OperationMethod;
028import org.nuxeo.ecm.automation.core.util.BlobList;
029import org.nuxeo.ecm.core.api.Blob;
030import org.nuxeo.ecm.core.api.DocumentModel;
031import org.nuxeo.ecm.core.api.blobholder.BlobHolder;
032import org.nuxeo.ecm.core.api.blobholder.SimpleBlobHolder;
033import org.nuxeo.ecm.core.convert.api.ConversionService;
034
035/**
036 * Save the input document
037 *
038 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
039 * @author tiry
040 */
041@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.")
042public class BlobToPDF {
043
044    public static final String ID = "Blob.ToPDF";
045
046    @Context
047    protected ConversionService service;
048
049    @OperationMethod
050    public Blob run(DocumentModel doc) {
051        BlobHolder bh = doc.getAdapter(BlobHolder.class);
052        if (bh == null) {
053            return null;
054        }
055        if ("application/pdf".equals(bh.getBlob().getMimeType())) {
056            return bh.getBlob();
057        }
058        BlobHolder pdfBh = service.convertToMimeType("application/pdf", bh, new HashMap<String, Serializable>());
059        Blob result = pdfBh.getBlob();
060
061        String fname = result.getFilename();
062        String filename = bh.getBlob().getFilename();
063        if (filename != null && !filename.isEmpty()) {
064            // add pdf extension
065            int pos = filename.lastIndexOf('.');
066            if (pos > 0) {
067                filename = filename.substring(0, pos);
068            }
069            filename += ".pdf";
070            result.setFilename(filename);
071        } else if (fname != null && !fname.isEmpty()) {
072            result.setFilename(fname);
073        } else {
074            result.setFilename("file");
075        }
076
077        result.setMimeType("application/pdf");
078        return result;
079    }
080
081    @OperationMethod
082    public Blob run(Blob blob) {
083        if ("application/pdf".equals(blob.getMimeType())) {
084            return blob;
085        }
086        BlobHolder bh = new SimpleBlobHolder(blob);
087        bh = service.convertToMimeType("application/pdf", bh, new HashMap<String, Serializable>());
088        Blob result = bh.getBlob();
089        adjustBlobName(blob, result);
090        return result;
091    }
092
093    @OperationMethod
094    public BlobList run(BlobList blobs) {
095        BlobList bl = new BlobList();
096        for (Blob blob : blobs) {
097            bl.add(this.run(blob));
098        }
099        return bl;
100    }
101
102    protected void adjustBlobName(Blob in, Blob out) {
103        String fname = in.getFilename();
104        if (fname == null) {
105            fname = "Unknown_" + System.identityHashCode(in);
106        }
107        out.setFilename(fname + ".pdf");
108        out.setMimeType("application/pdf");
109    }
110
111}