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