001/*
002 * Copyright (c) 2006-2013 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 *     ldoguin
011 */
012package org.nuxeo.ecm.automation.core.operations.blob;
013
014import org.nuxeo.ecm.automation.core.Constants;
015import org.nuxeo.ecm.automation.core.annotations.Context;
016import org.nuxeo.ecm.automation.core.annotations.Operation;
017import org.nuxeo.ecm.automation.core.annotations.OperationMethod;
018import org.nuxeo.ecm.automation.core.annotations.Param;
019import org.nuxeo.ecm.automation.core.util.BlobList;
020import org.nuxeo.ecm.core.api.Blob;
021import org.nuxeo.ecm.core.api.DocumentModel;
022import org.nuxeo.ecm.core.api.blobholder.BlobHolder;
023import org.nuxeo.ecm.core.convert.api.ConversionService;
024import org.nuxeo.ecm.platform.convert.ConvertHelper;
025
026/**
027 * Convert the given blob to a file with given mimetype.
028 *
029 * @author ldoguin
030 */
031@Operation(id = ConvertBlob.ID, category = Constants.CAT_CONVERSION, label = "Convert to given mime-type", description = "Convert the input file to a file of the given mime-type and return the new file.", since = "5.7")
032public class ConvertBlob {
033
034    public static final String ID = "Blob.Convert";
035
036    protected final ConvertHelper convertHelper = new ConvertHelper();
037
038    @Context
039    protected ConversionService service;
040
041    @Param(name = "mimeType", required = true)
042    protected String mimeType;
043
044    @OperationMethod
045    public Blob run(DocumentModel doc) {
046        BlobHolder bh = doc.getAdapter(BlobHolder.class);
047        if (bh == null) {
048            return null;
049        }
050        return run(bh.getBlob());
051    }
052
053    @OperationMethod
054    public Blob run(Blob blob) {
055        Blob result = convertHelper.convertBlob(blob, mimeType);
056        return result;
057    }
058
059    @OperationMethod
060    public BlobList run(BlobList blobs) {
061        BlobList bl = new BlobList();
062        for (Blob blob : blobs) {
063            bl.add(this.run(blob));
064        }
065        return bl;
066    }
067
068}