001/*
002 * (C) Copyright 2014 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl-2.1.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *      Vladimir Pasquier <vpasquier@nuxeo.com>
016 */
017package org.nuxeo.binary.metadata.internals.operations;
018
019import java.util.ArrayList;
020
021import org.nuxeo.binary.metadata.api.BinaryMetadataService;
022import org.nuxeo.ecm.automation.OperationContext;
023import org.nuxeo.ecm.automation.core.Constants;
024import org.nuxeo.ecm.automation.core.annotations.Context;
025import org.nuxeo.ecm.automation.core.annotations.Operation;
026import org.nuxeo.ecm.automation.core.annotations.OperationMethod;
027import org.nuxeo.ecm.automation.core.annotations.Param;
028import org.nuxeo.ecm.automation.core.util.StringList;
029import org.nuxeo.ecm.core.api.Blob;
030
031/**
032 * @since 7.1
033 */
034@Operation(id = ReadMetadataFromBinaryToContext.ID, category = Constants.CAT_EXECUTION, label = "Read Metadata From "
035        + "Binary to Context", description = "Read Metadata From binary to Context "
036        + "for a given input blob and given metadata to inject into the "
037        + "Operation context (if not specified, all metadata will be injected) " + "", since = "7.1", addToStudio = true, aliases = { "Context.ReadMetadataFromBinary" })
038public class ReadMetadataFromBinaryToContext {
039
040    public static final String ID = "Context.SetMetadataFromBlob";
041
042    public static final String CTX_BINARY_METADATA = "binaryMetadata";
043
044    @Context
045    protected BinaryMetadataService binaryMetadataService;
046
047    @Context
048    protected OperationContext operationContext;
049
050    @Param(name = "ignorePrefix", required = false, description = "Ignore metadata prefixes or not")
051    boolean ignorePrefix = true;
052
053    @Param(name = "processor", required = false, description = "The processor to execute for overriding the input blob.")
054    protected String processor = "exifTool";
055
056    @Param(name = "metadata", required = false, description = "Metadata list to filter on the blob.")
057    protected StringList metadata;
058
059    @OperationMethod
060    public void run(Blob blob) {
061        if (metadata == null || metadata.isEmpty()) {
062            operationContext.put(CTX_BINARY_METADATA, binaryMetadataService.readMetadata(blob, ignorePrefix));
063        } else {
064            ArrayList<String> metadataList = new ArrayList<>();
065            for (String meta : metadata) {
066                metadataList.add(meta);
067            }
068            operationContext.put(CTX_BINARY_METADATA,
069                    binaryMetadataService.readMetadata(blob, metadataList,
070                            ignorePrefix));
071        }
072    }
073}