http://git-wip-us.apache.org/repos/asf/nifi/blob/7e7d79fc/nifi-nar-bundles/nifi-media-bundle/nifi-media-nar/src/main/resources/META-INF/LICENSE ---------------------------------------------------------------------- diff --git a/nifi-nar-bundles/nifi-media-bundle/nifi-media-nar/src/main/resources/META-INF/LICENSE b/nifi-nar-bundles/nifi-media-bundle/nifi-media-nar/src/main/resources/META-INF/LICENSE new file mode 100644 index 0000000..2c3e42f --- /dev/null +++ b/nifi-nar-bundles/nifi-media-bundle/nifi-media-nar/src/main/resources/META-INF/LICENSE @@ -0,0 +1,239 @@ + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + +APACHE NIFI SUBCOMPONENTS: + +The Apache NiFi project contains subcomponents with separate copyright +notices and license terms. Your use of the source code for the these +subcomponents is subject to the terms and conditions of the following +licenses. + +This product bundles 'Adobe XMPCore' which is available under "The BSD license". More +information can be found here: http://www.adobe.com/devnet/xmp/library/eula-xmp-library-java.html + + Copyright (c) 2009, Adobe Systems Incorporated All rights reserved. + + Redistribution and use in source and binary forms, with or without modification, + are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + + * Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + + * Neither the name of Adobe Systems Incorporated, nor the names of its + contributors may be used to endorse or promote products derived from this + software without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + WARRANTIES OF MERCHANT ABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, + INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, + OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF + THE POSSIBILITY OF SUCH DAMAGE. \ No newline at end of file
http://git-wip-us.apache.org/repos/asf/nifi/blob/7e7d79fc/nifi-nar-bundles/nifi-media-bundle/nifi-media-nar/src/main/resources/META-INF/NOTICE ---------------------------------------------------------------------- diff --git a/nifi-nar-bundles/nifi-media-bundle/nifi-media-nar/src/main/resources/META-INF/NOTICE b/nifi-nar-bundles/nifi-media-bundle/nifi-media-nar/src/main/resources/META-INF/NOTICE new file mode 100644 index 0000000..95481c9 --- /dev/null +++ b/nifi-nar-bundles/nifi-media-bundle/nifi-media-nar/src/main/resources/META-INF/NOTICE @@ -0,0 +1,35 @@ +nifi-media-nar +Copyright 2015-2016 The Apache Software Foundation + +This product includes software developed at +The Apache Software Foundation (http://www.apache.org/). + +****************** +Apache Software License v2 +****************** + +The following binary components are provided under the Apache Software License v2 + + (ASLv2) Metadata-Extractor + The following NOTICE information applies: + Metadata-Extractor + Copyright 2002-2015 Drew Noakes + + (ASLv2) Apache Tika + Apache Tika + Copyright 2015 The Apache Software Foundation + + This product includes software developed at + The Apache Software Foundation (http://www.apache.org/). + + Copyright 1993-2010 University Corporation for Atmospheric Research/Unidata + This software contains code derived from UCAR/Unidata's NetCDF library. + + Tika-server component uses CDDL-licensed dependencies: jersey (http://jersey.java.net/) and + Grizzly (http://grizzly.java.net/) + + Tika-parsers component uses CDDL/LGPL dual-licensed dependency: jhighlight (https://github.com/codelibs/jhighlight) + + OpenCSV: Copyright 2005 Bytecode Pty Ltd. Licensed under the Apache License, Version 2.0 + + IPTC Photo Metadata descriptions Copyright 2010 International Press Telecommunications Council. http://git-wip-us.apache.org/repos/asf/nifi/blob/7e7d79fc/nifi-nar-bundles/nifi-media-bundle/nifi-media-processors/pom.xml ---------------------------------------------------------------------- diff --git a/nifi-nar-bundles/nifi-media-bundle/nifi-media-processors/pom.xml b/nifi-nar-bundles/nifi-media-bundle/nifi-media-processors/pom.xml new file mode 100644 index 0000000..929b89b --- /dev/null +++ b/nifi-nar-bundles/nifi-media-bundle/nifi-media-processors/pom.xml @@ -0,0 +1,74 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + http://www.apache.org/licenses/LICENSE-2.0 + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +--> +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> + + <parent> + <groupId>org.apache.nifi</groupId> + <artifactId>nifi-media-bundle</artifactId> + <version>1.0.0-SNAPSHOT</version> + </parent> + + <artifactId>nifi-media-processors</artifactId> + <packaging>jar</packaging> + + <dependencies> + <dependency> + <groupId>org.apache.nifi</groupId> + <artifactId>nifi-api</artifactId> + </dependency> + <dependency> + <groupId>org.apache.nifi</groupId> + <artifactId>nifi-processor-utils</artifactId> + </dependency> + <dependency> + <groupId>org.apache.nifi</groupId> + <artifactId>nifi-mock</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>com.drewnoakes</groupId> + <artifactId>metadata-extractor</artifactId> + <version>2.8.0</version> + </dependency> + <dependency> + <groupId>org.apache.tika</groupId> + <artifactId>tika-core</artifactId> + <version>1.8</version> + </dependency> + <dependency> + <groupId>org.apache.tika</groupId> + <artifactId>tika-parsers</artifactId> + <version>1.8</version> + </dependency> + </dependencies> + + <build> + <plugins> + <plugin> + <groupId>org.apache.rat</groupId> + <artifactId>apache-rat-plugin</artifactId> + <configuration> + <excludes combine.children="append"> + <exclude>src/test/resources/notImage.txt</exclude> + <exclude>src/test/resources/textFile.txt</exclude> + <exclude>src/test/resources/textFileBig.txt</exclude> + </excludes> + </configuration> + </plugin> + </plugins> + </build> +</project> http://git-wip-us.apache.org/repos/asf/nifi/blob/7e7d79fc/nifi-nar-bundles/nifi-media-bundle/nifi-media-processors/src/main/java/org/apache/nifi/processors/image/ExtractImageMetadata.java ---------------------------------------------------------------------- diff --git a/nifi-nar-bundles/nifi-media-bundle/nifi-media-processors/src/main/java/org/apache/nifi/processors/image/ExtractImageMetadata.java b/nifi-nar-bundles/nifi-media-bundle/nifi-media-processors/src/main/java/org/apache/nifi/processors/image/ExtractImageMetadata.java new file mode 100644 index 0000000..1dd9241 --- /dev/null +++ b/nifi-nar-bundles/nifi-media-bundle/nifi-media-processors/src/main/java/org/apache/nifi/processors/image/ExtractImageMetadata.java @@ -0,0 +1,169 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.nifi.processors.image; + +import java.io.IOException; +import java.io.InputStream; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import org.apache.nifi.annotation.behavior.InputRequirement; +import org.apache.nifi.annotation.behavior.InputRequirement.Requirement; +import org.apache.nifi.annotation.behavior.SupportsBatching; +import org.apache.nifi.annotation.behavior.WritesAttribute; +import org.apache.nifi.annotation.behavior.WritesAttributes; +import org.apache.nifi.annotation.documentation.CapabilityDescription; +import org.apache.nifi.annotation.documentation.Tags; +import org.apache.nifi.components.PropertyDescriptor; +import org.apache.nifi.flowfile.FlowFile; +import org.apache.nifi.logging.ComponentLog; +import org.apache.nifi.processor.AbstractProcessor; +import org.apache.nifi.processor.ProcessContext; +import org.apache.nifi.processor.ProcessSession; +import org.apache.nifi.processor.ProcessorInitializationContext; +import org.apache.nifi.processor.Relationship; +import org.apache.nifi.processor.exception.ProcessException; +import org.apache.nifi.processor.io.InputStreamCallback; +import org.apache.nifi.processor.util.StandardValidators; +import org.apache.nifi.util.ObjectHolder; + +import com.drew.imaging.ImageMetadataReader; +import com.drew.imaging.ImageProcessingException; +import com.drew.metadata.Directory; +import com.drew.metadata.Metadata; +import com.drew.metadata.Tag; + +@InputRequirement(Requirement.INPUT_REQUIRED) +@Tags({"Exif", "Exchangeable", "image", "file", "format", "JPG", "GIF", "PNG", "BMP", "metadata","IPTC", "XMP"}) +@CapabilityDescription("Extract the image metadata from flowfiles containing images. This processor relies on this " + + "metadata extractor library https://github.com/drewnoakes/metadata-extractor. It extracts a long list of " + + "metadata types including but not limited to EXIF, IPTC, XMP and Photoshop fields. For the full list visit " + + "the library's website." + + "NOTE: The library being used loads the images into memory so extremely large images may cause problems.") +@WritesAttributes({@WritesAttribute(attribute = "<directory name>.<tag name>", description = "The extracted image metadata " + + "will be inserted with the attribute name \"<directory name>.<tag name>\". ")}) +@SupportsBatching +public class ExtractImageMetadata extends AbstractProcessor { + + public static final PropertyDescriptor MAX_NUMBER_OF_ATTRIBUTES = new PropertyDescriptor.Builder() + .name("Max Number of Attributes") + .description("Specify the max number of attributes to add to the flowfile. There is no guarantee in what order" + + " the tags will be processed. By default it will process all of them.") + .required(false) + .addValidator(StandardValidators.NON_NEGATIVE_INTEGER_VALIDATOR) + .build(); + + public static final Relationship SUCCESS = new Relationship.Builder() + .name("success") + .description("Any FlowFile that successfully has image metadata extracted will be routed to success") + .build(); + + public static final Relationship FAILURE = new Relationship.Builder() + .name("failure") + .description("Any FlowFile that fails to have image metadata extracted will be routed to failure") + .build(); + + private Set<Relationship> relationships; + private List<PropertyDescriptor> properties; + + @Override + protected void init(final ProcessorInitializationContext context) { + + final List<PropertyDescriptor> properties = new ArrayList<>(); + properties.add(MAX_NUMBER_OF_ATTRIBUTES); + this.properties = Collections.unmodifiableList(properties); + + final Set<Relationship> relationships = new HashSet<>(); + relationships.add(SUCCESS); + relationships.add(FAILURE); + this.relationships = Collections.unmodifiableSet(relationships); + } + + @Override + public Set<Relationship> getRelationships() { + return this.relationships; + } + + @Override + protected List<PropertyDescriptor> getSupportedPropertyDescriptors() { + return this.properties; + } + + @Override + public void onTrigger(final ProcessContext context, final ProcessSession session) throws ProcessException { + FlowFile flowfile = session.get(); + if (flowfile == null) { + return; + } + + final ComponentLog logger = this.getLogger(); + final ObjectHolder<Metadata> value = new ObjectHolder<>(null); + final Integer max = context.getProperty(MAX_NUMBER_OF_ATTRIBUTES).asInteger(); + + try { + session.read(flowfile, new InputStreamCallback() { + @Override + public void process(InputStream in) throws IOException { + try { + Metadata imageMetadata = ImageMetadataReader.readMetadata(in); + value.set(imageMetadata); + } catch (ImageProcessingException ex) { + throw new ProcessException(ex); + } + } + }); + + Metadata metadata = value.get(); + Map<String, String> results = getTags(max, metadata); + + // Write the results to an attribute + if (!results.isEmpty()) { + flowfile = session.putAllAttributes(flowfile, results); + } + + session.transfer(flowfile, SUCCESS); + } catch (ProcessException e) { + logger.error("Failed to extract image metadata from {} due to {}", new Object[]{flowfile, e}); + session.transfer(flowfile, FAILURE); + } + } + + private Map<String, String> getTags(Integer max, Metadata metadata) { + Map<String, String> results = new HashMap<>(); + int i =0; + + for (Directory directory : metadata.getDirectories()) { + for (Tag tag : directory.getTags()) { + results.put(directory.getName() + "." + tag.getTagName(), tag.getDescription()); + + if(max!=null) { + i++; + if (i >= max) { + return results; + } + } + } + } + + return results; + } +} http://git-wip-us.apache.org/repos/asf/nifi/blob/7e7d79fc/nifi-nar-bundles/nifi-media-bundle/nifi-media-processors/src/main/java/org/apache/nifi/processors/image/ResizeImage.java ---------------------------------------------------------------------- diff --git a/nifi-nar-bundles/nifi-media-bundle/nifi-media-processors/src/main/java/org/apache/nifi/processors/image/ResizeImage.java b/nifi-nar-bundles/nifi-media-bundle/nifi-media-processors/src/main/java/org/apache/nifi/processors/image/ResizeImage.java new file mode 100644 index 0000000..176561f --- /dev/null +++ b/nifi-nar-bundles/nifi-media-bundle/nifi-media-processors/src/main/java/org/apache/nifi/processors/image/ResizeImage.java @@ -0,0 +1,198 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.nifi.processors.image; + +import java.awt.Graphics2D; +import java.awt.Image; +import java.awt.image.BufferedImage; +import java.io.BufferedInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.Iterator; +import java.util.List; +import java.util.Set; +import java.util.concurrent.TimeUnit; + +import javax.imageio.ImageIO; +import javax.imageio.ImageReader; +import javax.imageio.stream.ImageInputStream; + +import org.apache.nifi.annotation.behavior.EventDriven; +import org.apache.nifi.annotation.behavior.InputRequirement; +import org.apache.nifi.annotation.behavior.InputRequirement.Requirement; +import org.apache.nifi.annotation.behavior.SupportsBatching; +import org.apache.nifi.annotation.documentation.CapabilityDescription; +import org.apache.nifi.annotation.documentation.Tags; +import org.apache.nifi.components.AllowableValue; +import org.apache.nifi.components.PropertyDescriptor; +import org.apache.nifi.flowfile.FlowFile; +import org.apache.nifi.processor.AbstractProcessor; +import org.apache.nifi.processor.ProcessContext; +import org.apache.nifi.processor.ProcessSession; +import org.apache.nifi.processor.Relationship; +import org.apache.nifi.processor.exception.ProcessException; +import org.apache.nifi.processor.io.StreamCallback; +import org.apache.nifi.processor.util.StandardValidators; +import org.apache.nifi.util.StopWatch; + +@EventDriven +@SupportsBatching +@InputRequirement(Requirement.INPUT_REQUIRED) +@Tags({ "resize", "image", "jpg", "jpeg", "png", "bmp", "wbmp", "gif" }) +@CapabilityDescription("Resizes an image to user-specified dimensions. This Processor uses the image codecs registered with the " + + "environment that NiFi is running in. By default, this includes JPEG, PNG, BMP, WBMP, and GIF images.") +public class ResizeImage extends AbstractProcessor { + static final AllowableValue RESIZE_DEFAULT = new AllowableValue("Default", "Default", "Use the default algorithm"); + static final AllowableValue RESIZE_FAST = new AllowableValue("Scale Fast", "Scale Fast", "Emphasize speed of the scaling over smoothness"); + static final AllowableValue RESIZE_SMOOTH = new AllowableValue("Scale Smooth", "Scale Smooth", "Emphasize smoothness of the scaling over speed"); + static final AllowableValue RESIZE_REPLICATE = new AllowableValue("Replicate Scale Filter", "Replicate Scale Filter", "Use the Replicate Scale Filter algorithm"); + static final AllowableValue RESIZE_AREA_AVERAGING = new AllowableValue("Area Averaging", "Area Averaging", "Use the Area Averaging scaling algorithm"); + + static final PropertyDescriptor IMAGE_WIDTH = new PropertyDescriptor.Builder() + .name("Image Width (in pixels)") + .description("The desired number of pixels for the image's width") + .required(true) + .expressionLanguageSupported(true) + .addValidator(StandardValidators.POSITIVE_INTEGER_VALIDATOR) + .build(); + static final PropertyDescriptor IMAGE_HEIGHT = new PropertyDescriptor.Builder() + .name("Image Height (in pixels)") + .description("The desired number of pixels for the image's height") + .required(true) + .expressionLanguageSupported(true) + .addValidator(StandardValidators.POSITIVE_INTEGER_VALIDATOR) + .build(); + static final PropertyDescriptor SCALING_ALGORITHM = new PropertyDescriptor.Builder() + .name("Scaling Algorithm") + .description("Specifies which algorithm should be used to resize the image") + .required(true) + .allowableValues(RESIZE_DEFAULT, RESIZE_FAST, RESIZE_SMOOTH, RESIZE_REPLICATE, RESIZE_AREA_AVERAGING) + .defaultValue(RESIZE_DEFAULT.getValue()) + .build(); + + static final Relationship REL_SUCCESS = new Relationship.Builder() + .name("success") + .description("A FlowFile is routed to this relationship if it is successfully resized") + .build(); + static final Relationship REL_FAILURE = new Relationship.Builder() + .name("failure") + .description("A FlowFile is routed to this relationship if it is not in the specified format") + .build(); + + @Override + protected List<PropertyDescriptor> getSupportedPropertyDescriptors() { + final List<PropertyDescriptor> properties = new ArrayList<>(); + properties.add(IMAGE_WIDTH); + properties.add(IMAGE_HEIGHT); + properties.add(SCALING_ALGORITHM); + return properties; + } + + @Override + public Set<Relationship> getRelationships() { + final Set<Relationship> relationships = new HashSet<>(); + relationships.add(REL_SUCCESS); + relationships.add(REL_FAILURE); + return relationships; + } + + @Override + public void onTrigger(final ProcessContext context, final ProcessSession session) throws ProcessException { + FlowFile flowFile = session.get(); + if (flowFile == null) { + return; + } + + final int width, height; + try { + width = context.getProperty(IMAGE_WIDTH).evaluateAttributeExpressions(flowFile).asInteger(); + height = context.getProperty(IMAGE_HEIGHT).evaluateAttributeExpressions(flowFile).asInteger(); + } catch (final NumberFormatException nfe) { + getLogger().error("Failed to resize {} due to {}", new Object[] { flowFile, nfe }); + session.transfer(flowFile, REL_FAILURE); + return; + } + + final String algorithm = context.getProperty(SCALING_ALGORITHM).getValue(); + final int hints; + if (algorithm.equalsIgnoreCase(RESIZE_DEFAULT.getValue())) { + hints = Image.SCALE_DEFAULT; + } else if (algorithm.equalsIgnoreCase(RESIZE_FAST.getValue())) { + hints = Image.SCALE_FAST; + } else if (algorithm.equalsIgnoreCase(RESIZE_SMOOTH.getValue())) { + hints = Image.SCALE_SMOOTH; + } else if (algorithm.equalsIgnoreCase(RESIZE_REPLICATE.getValue())) { + hints = Image.SCALE_REPLICATE; + } else if (algorithm.equalsIgnoreCase(RESIZE_AREA_AVERAGING.getValue())) { + hints = Image.SCALE_AREA_AVERAGING; + } else { + throw new AssertionError("Invalid Scaling Algorithm: " + algorithm); + } + + final StopWatch stopWatch = new StopWatch(true); + try { + flowFile = session.write(flowFile, new StreamCallback() { + @Override + public void process(final InputStream rawIn, final OutputStream out) throws IOException { + try (final BufferedInputStream in = new BufferedInputStream(rawIn)) { + final ImageInputStream iis = ImageIO.createImageInputStream(in); + if (iis == null) { + throw new ProcessException("FlowFile is not in a valid format"); + } + + final Iterator<ImageReader> readers = ImageIO.getImageReaders(iis); + if (!readers.hasNext()) { + throw new ProcessException("FlowFile is not in a valid format"); + } + + final ImageReader reader = readers.next(); + final String formatName = reader.getFormatName(); + reader.setInput(iis, true); + final BufferedImage image = reader.read(0); + + final Image scaledImage = image.getScaledInstance(width, height, hints); + final BufferedImage scaledBufferedImg; + if (scaledImage instanceof BufferedImage) { + scaledBufferedImg = (BufferedImage) scaledImage; + } else { + scaledBufferedImg = new BufferedImage(scaledImage.getWidth(null), scaledImage.getHeight(null), image.getType()); + final Graphics2D graphics = scaledBufferedImg.createGraphics(); + try { + graphics.drawImage(scaledImage, 0, 0, null); + } finally { + graphics.dispose(); + } + } + + ImageIO.write(scaledBufferedImg, formatName, out); + } + } + }); + + session.getProvenanceReporter().modifyContent(flowFile, stopWatch.getElapsed(TimeUnit.MILLISECONDS)); + session.transfer(flowFile, REL_SUCCESS); + } catch (final ProcessException pe) { + getLogger().error("Failed to resize {} due to {}", new Object[] { flowFile, pe }); + session.transfer(flowFile, REL_FAILURE); + } + } + +} http://git-wip-us.apache.org/repos/asf/nifi/blob/7e7d79fc/nifi-nar-bundles/nifi-media-bundle/nifi-media-processors/src/main/java/org/apache/nifi/processors/media/ExtractMediaMetadata.java ---------------------------------------------------------------------- diff --git a/nifi-nar-bundles/nifi-media-bundle/nifi-media-processors/src/main/java/org/apache/nifi/processors/media/ExtractMediaMetadata.java b/nifi-nar-bundles/nifi-media-bundle/nifi-media-processors/src/main/java/org/apache/nifi/processors/media/ExtractMediaMetadata.java new file mode 100644 index 0000000..ab7e6ed --- /dev/null +++ b/nifi-nar-bundles/nifi-media-bundle/nifi-media-processors/src/main/java/org/apache/nifi/processors/media/ExtractMediaMetadata.java @@ -0,0 +1,251 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.nifi.processors.media; + +import java.io.IOException; +import java.io.InputStream; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.atomic.AtomicReference; +import java.util.regex.Pattern; + +import org.apache.nifi.annotation.behavior.InputRequirement; +import org.apache.nifi.annotation.behavior.InputRequirement.Requirement; +import org.apache.nifi.annotation.behavior.SupportsBatching; +import org.apache.nifi.annotation.behavior.WritesAttribute; +import org.apache.nifi.annotation.behavior.WritesAttributes; +import org.apache.nifi.annotation.documentation.CapabilityDescription; +import org.apache.nifi.annotation.documentation.Tags; +import org.apache.nifi.annotation.lifecycle.OnScheduled; +import org.apache.nifi.components.PropertyDescriptor; +import org.apache.nifi.flowfile.FlowFile; +import org.apache.nifi.logging.ComponentLog; +import org.apache.nifi.processor.AbstractProcessor; +import org.apache.nifi.processor.ProcessContext; +import org.apache.nifi.processor.ProcessSession; +import org.apache.nifi.processor.ProcessorInitializationContext; +import org.apache.nifi.processor.Relationship; +import org.apache.nifi.processor.exception.ProcessException; +import org.apache.nifi.processor.io.InputStreamCallback; +import org.apache.nifi.processor.util.StandardValidators; + +import org.apache.tika.exception.TikaException; +import org.apache.tika.io.TikaInputStream; +import org.apache.tika.metadata.Metadata; +import org.apache.tika.parser.AutoDetectParser; +import org.xml.sax.SAXException; +import org.xml.sax.helpers.DefaultHandler; + +@InputRequirement(Requirement.INPUT_REQUIRED) +@Tags({"media", "file", "format", "metadata", "audio", "video", "image", "document", "pdf"}) +@CapabilityDescription("Extract the content metadata from flowfiles containing audio, video, image, and other file " + + "types. This processor relies on the Apache Tika project for file format detection and parsing. It " + + "extracts a long list of metadata types for media files including audio, video, and print media " + + "formats." + + "NOTE: the attribute names and content extracted may vary across upgrades because parsing is performed by " + + "the external Tika tools which in turn depend on other projects for metadata extraction. For the more " + + "details and the list of supported file types, visit the library's website at http://tika.apache.org/.") +@WritesAttributes({@WritesAttribute(attribute = "<Metadata Key Prefix><attribute>", description = "The extracted content metadata " + + "will be inserted with the attribute name \"<Metadata Key Prefix><attribute>\", or \"<attribute>\" if " + + "\"Metadata Key Prefix\" is not provided.")}) +@SupportsBatching +public class ExtractMediaMetadata extends AbstractProcessor { + + static final PropertyDescriptor MAX_NUMBER_OF_ATTRIBUTES = new PropertyDescriptor.Builder() + .name("Max Number of Attributes") + .description("Specify the max number of attributes to add to the flowfile. There is no guarantee in what order" + + " the tags will be processed. By default it will process all of them.") + .required(false) + .defaultValue("100") + .addValidator(StandardValidators.NON_NEGATIVE_INTEGER_VALIDATOR) + .build(); + + private static final PropertyDescriptor MAX_ATTRIBUTE_LENGTH = new PropertyDescriptor.Builder() + .name("Max Attribute Length") + .description("Specifies the maximum length of a single attribute value. When a metadata item has multiple" + + " values, they will be merged until this length is reached and then \", ...\" will be added as" + + " an indicator that additional values where dropped. If a single value is longer than this, it" + + " will be truncated and \"(truncated)\" appended to indicate that truncation occurred.") + .required(true) + .defaultValue("100") + .addValidator(StandardValidators.NON_NEGATIVE_INTEGER_VALIDATOR) + .build(); + + static final PropertyDescriptor METADATA_KEY_FILTER = new PropertyDescriptor.Builder() + .name("Metadata Key Filter") + .description("A regular expression identifying which metadata keys received from the parser should be" + + " added to the flowfile attributes. If left blank, all metadata keys parsed will be added to the" + + " flowfile attributes.") + .required(false) + .addValidator(StandardValidators.REGULAR_EXPRESSION_VALIDATOR) + .build(); + + static final PropertyDescriptor METADATA_KEY_PREFIX = new PropertyDescriptor.Builder() + .name("Metadata Key Prefix") + .description("Text to be prefixed to metadata keys as the are added to the flowfile attributes. It is" + + " recommended to end with with a separator character like '.' or '-', this is not automatically " + + " added by the processor.") + .required(false) + .addValidator(StandardValidators.ATTRIBUTE_KEY_VALIDATOR) + .expressionLanguageSupported(true) + .build(); + + static final Relationship SUCCESS = new Relationship.Builder() + .name("success") + .description("Any FlowFile that successfully has media metadata extracted will be routed to success") + .build(); + + static final Relationship FAILURE = new Relationship.Builder() + .name("failure") + .description("Any FlowFile that fails to have media metadata extracted will be routed to failure") + .build(); + + private Set<Relationship> relationships; + private List<PropertyDescriptor> properties; + + private final AtomicReference<Pattern> metadataKeyFilterRef = new AtomicReference<>(); + + private volatile AutoDetectParser autoDetectParser; + + @Override + protected void init(final ProcessorInitializationContext context) { + + final List<PropertyDescriptor> properties = new ArrayList<>(); + properties.add(MAX_NUMBER_OF_ATTRIBUTES); + properties.add(MAX_ATTRIBUTE_LENGTH); + properties.add(METADATA_KEY_FILTER); + properties.add(METADATA_KEY_PREFIX); + this.properties = Collections.unmodifiableList(properties); + + final Set<Relationship> relationships = new HashSet<>(); + relationships.add(SUCCESS); + relationships.add(FAILURE); + this.relationships = Collections.unmodifiableSet(relationships); + } + + @Override + public Set<Relationship> getRelationships() { + return this.relationships; + } + + @Override + protected List<PropertyDescriptor> getSupportedPropertyDescriptors() { + return this.properties; + } + + @SuppressWarnings("unused") + @OnScheduled + public void onScheduled(ProcessContext context) { + String metadataKeyFilterInput = context.getProperty(METADATA_KEY_FILTER).getValue(); + if (metadataKeyFilterInput != null && metadataKeyFilterInput.length() > 0) { + metadataKeyFilterRef.set(Pattern.compile(metadataKeyFilterInput)); + } else { + metadataKeyFilterRef.set(null); + } + + autoDetectParser = new AutoDetectParser(); + } + + @Override + public void onTrigger(final ProcessContext context, final ProcessSession session) throws ProcessException { + FlowFile flowFile = session.get(); + if (flowFile == null) { + return; + } + + final ComponentLog logger = this.getLogger(); + final AtomicReference<Map<String, String>> value = new AtomicReference<>(null); + final Integer maxAttribCount = context.getProperty(MAX_NUMBER_OF_ATTRIBUTES).asInteger(); + final Integer maxAttribLength = context.getProperty(MAX_ATTRIBUTE_LENGTH).asInteger(); + final String prefix = context.getProperty(METADATA_KEY_PREFIX).evaluateAttributeExpressions(flowFile).getValue(); + + try { + session.read(flowFile, new InputStreamCallback() { + @Override + public void process(InputStream in) throws IOException { + try { + Map<String, String> results = tika_parse(in, prefix, maxAttribCount, maxAttribLength); + value.set(results); + } catch (SAXException | TikaException e) { + throw new IOException(e); + } + } + }); + + // Write the results to attributes + Map<String, String> results = value.get(); + if (results != null && !results.isEmpty()) { + flowFile = session.putAllAttributes(flowFile, results); + } + + session.transfer(flowFile, SUCCESS); + session.getProvenanceReporter().modifyAttributes(flowFile, "media attributes extracted"); + } catch (ProcessException e) { + logger.error("Failed to extract media metadata from {} due to {}", new Object[]{flowFile, e}); + flowFile = session.penalize(flowFile); + session.transfer(flowFile, FAILURE); + } + } + + private Map<String, String> tika_parse(InputStream sourceStream, String prefix, Integer maxAttribs, + Integer maxAttribLen) throws IOException, TikaException, SAXException { + final Metadata metadata = new Metadata(); + final TikaInputStream tikaInputStream = TikaInputStream.get(sourceStream); + autoDetectParser.parse(tikaInputStream, new DefaultHandler(), metadata); + + final Map<String, String> results = new HashMap<>(); + final Pattern metadataKeyFilter = metadataKeyFilterRef.get(); + final StringBuilder dataBuilder = new StringBuilder(); + for (final String key : metadata.names()) { + if (metadataKeyFilter != null && !metadataKeyFilter.matcher(key).matches()) { + continue; + } + dataBuilder.setLength(0); + if (metadata.isMultiValued(key)) { + for (String val : metadata.getValues(key)) { + if (dataBuilder.length() > 1) { + dataBuilder.append(", "); + } + if (dataBuilder.length() + val.length() < maxAttribLen) { + dataBuilder.append(val); + } else { + dataBuilder.append("..."); + break; + } + } + } else { + dataBuilder.append(metadata.get(key)); + } + if (prefix == null) { + results.put(key, dataBuilder.toString().trim()); + } else { + results.put(prefix + key, dataBuilder.toString().trim()); + } + + // cutoff at max if provided + if (maxAttribs != null && results.size() >= maxAttribs) { + break; + } + } + return results; + } +} http://git-wip-us.apache.org/repos/asf/nifi/blob/7e7d79fc/nifi-nar-bundles/nifi-media-bundle/nifi-media-processors/src/main/resources/META-INF/services/org.apache.nifi.processor.Processor ---------------------------------------------------------------------- diff --git a/nifi-nar-bundles/nifi-media-bundle/nifi-media-processors/src/main/resources/META-INF/services/org.apache.nifi.processor.Processor b/nifi-nar-bundles/nifi-media-bundle/nifi-media-processors/src/main/resources/META-INF/services/org.apache.nifi.processor.Processor new file mode 100644 index 0000000..d5d0075 --- /dev/null +++ b/nifi-nar-bundles/nifi-media-bundle/nifi-media-processors/src/main/resources/META-INF/services/org.apache.nifi.processor.Processor @@ -0,0 +1,17 @@ +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +org.apache.nifi.processors.image.ExtractImageMetadata +org.apache.nifi.processors.image.ResizeImage +org.apache.nifi.processors.media.ExtractMediaMetadata http://git-wip-us.apache.org/repos/asf/nifi/blob/7e7d79fc/nifi-nar-bundles/nifi-media-bundle/nifi-media-processors/src/test/java/org/apache/nifi/processors/image/ExtractImageMetadataTest.java ---------------------------------------------------------------------- diff --git a/nifi-nar-bundles/nifi-media-bundle/nifi-media-processors/src/test/java/org/apache/nifi/processors/image/ExtractImageMetadataTest.java b/nifi-nar-bundles/nifi-media-bundle/nifi-media-processors/src/test/java/org/apache/nifi/processors/image/ExtractImageMetadataTest.java new file mode 100644 index 0000000..49268fb --- /dev/null +++ b/nifi-nar-bundles/nifi-media-bundle/nifi-media-processors/src/test/java/org/apache/nifi/processors/image/ExtractImageMetadataTest.java @@ -0,0 +1,161 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.nifi.processors.image; + +import org.apache.nifi.processor.Relationship; +import org.apache.nifi.util.MockFlowFile; +import org.apache.nifi.util.TestRunner; +import org.apache.nifi.util.TestRunners; +import org.junit.Before; +import org.junit.Test; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Map; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + + +public class ExtractImageMetadataTest { + private static String BMP_HEADER = "BMP Header."; + private static String JPEG_HEADER = "JPEG."; + private static String GIF_HEADER = "GIF Header."; + private static String PNG_HEADER = "PNG-"; + + private TestRunner testRunner; + + @Before + public void init() { + testRunner = TestRunners.newTestRunner(ExtractImageMetadata.class); + } + + @Test + public void testFailedExtraction() throws IOException { + MockFlowFile flowFile = verifyTestRunnerFlow("src/test/resources/notImage.txt", ExtractImageMetadata.FAILURE,null); + } + + @Test + public void testExtractJPG() throws IOException { + MockFlowFile flowFile = verifyTestRunnerFlow("src/test/resources/simple.jpg", ExtractImageMetadata.SUCCESS,null); + Map<String, String> attributes = flowFile.getAttributes(); + + assertEquals("800 pixels", attributes.get(JPEG_HEADER + "Image Width")); + assertEquals("600 pixels", attributes.get(JPEG_HEADER + "Image Height")); + assertEquals("8 bits", attributes.get(JPEG_HEADER + "Data Precision")); + assertEquals("Baseline", attributes.get(JPEG_HEADER + "Compression Type")); + assertEquals("3", attributes.get(JPEG_HEADER + "Number of Components")); + assertEquals("Y component: Quantization table 0, Sampling factors 2 horiz/2 vert", + attributes.get(JPEG_HEADER + "Component 1")); + assertEquals("Cb component: Quantization table 1, Sampling factors 1 horiz/1 vert", + attributes.get(JPEG_HEADER + "Component 2")); + assertEquals("Cr component: Quantization table 1, Sampling factors 1 horiz/1 vert", + attributes.get(JPEG_HEADER + "Component 3")); + } + + @Test + public void testExtractGIF() throws IOException { + MockFlowFile flowFile = verifyTestRunnerFlow( + "src/test/resources/photoshop-8x12-32colors-alpha.gif", ExtractImageMetadata.SUCCESS,null); + Map<String, String> attributes = flowFile.getAttributes(); + + assertEquals("8", attributes.get(GIF_HEADER + "Image Width")); + assertEquals("12", attributes.get(GIF_HEADER + "Image Height")); + assertEquals("true", attributes.get(GIF_HEADER + "Has Global Color Table")); + assertEquals("32", attributes.get(GIF_HEADER + "Color Table Size")); + assertEquals("8", attributes.get(GIF_HEADER + "Transparent Color Index")); + assertEquals("89a", attributes.get(GIF_HEADER + "GIF Format Version")); + assertEquals("5", attributes.get(GIF_HEADER + "Bits per Pixel")); + assertEquals("false", attributes.get(GIF_HEADER + "Is Color Table Sorted")); + } + + @Test + public void testExtractPNG() throws IOException { + MockFlowFile flowFile = verifyTestRunnerFlow("src/test/resources/mspaint-8x10.png", ExtractImageMetadata.SUCCESS, null); + Map<String, String> attributes = flowFile.getAttributes(); + + assertEquals("8", attributes.get(PNG_HEADER + "IHDR.Image Width")); + assertEquals("12", attributes.get(PNG_HEADER + "IHDR.Image Height")); + assertEquals("0.45455", attributes.get(PNG_HEADER + "gAMA.Image Gamma")); + assertEquals("Deflate", attributes.get(PNG_HEADER + "IHDR.Compression Type")); + assertEquals("No Interlace", attributes.get(PNG_HEADER + "IHDR.Interlace Method")); + assertEquals("Perceptual", attributes.get(PNG_HEADER + "sRGB.sRGB Rendering Intent")); + assertEquals("Adaptive", attributes.get(PNG_HEADER + "IHDR.Filter Method")); + assertEquals("8", attributes.get(PNG_HEADER + "IHDR.Bits Per Sample")); + assertEquals("True Color", attributes.get(PNG_HEADER + "IHDR.Color Type")); + } + @Test + public void testExtractBMP() throws IOException { + MockFlowFile flowFile = verifyTestRunnerFlow("src/test/resources/16color-10x10.bmp", ExtractImageMetadata.SUCCESS, null); + Map<String, String> attributes = flowFile.getAttributes(); + + assertEquals("10", attributes.get(BMP_HEADER+"Image Width")); + assertEquals("10", attributes.get(BMP_HEADER+"Image Height")); + assertEquals("4", attributes.get(BMP_HEADER+"Bits Per Pixel")); + assertEquals("None", attributes.get(BMP_HEADER+"Compression")); + assertEquals("0", attributes.get(BMP_HEADER+"X Pixels per Meter")); + assertEquals("0", attributes.get(BMP_HEADER+"Y Pixels per Meter")); + assertEquals("0", attributes.get(BMP_HEADER+"Palette Colour Count")); + assertEquals("0", attributes.get(BMP_HEADER+"Important Colour Count")); + assertEquals("1", attributes.get(BMP_HEADER+"Planes")); + assertEquals("40", attributes.get(BMP_HEADER+"Header Size")); + } + @Test + public void testExtractLimitedAttributesBMP() throws IOException { + MockFlowFile flowFile = verifyTestRunnerFlow("src/test/resources/16color-10x10.bmp", ExtractImageMetadata.SUCCESS, "5"); + Map<String, String> attributes = flowFile.getAttributes(); + + assertEquals("10", attributes.get(BMP_HEADER+"Image Width")); + assertEquals("10", attributes.get(BMP_HEADER+"Image Height")); + assertEquals("4", attributes.get(BMP_HEADER+"Bits Per Pixel")); + assertEquals("1", attributes.get(BMP_HEADER+"Planes")); + assertEquals("40", attributes.get(BMP_HEADER+"Header Size")); + + + assertNull(attributes.get(BMP_HEADER + "Compression")); + assertNull(attributes.get(BMP_HEADER + "X Pixels per Meter")); + assertNull(attributes.get(BMP_HEADER + "Y Pixels per Meter")); + assertNull(attributes.get(BMP_HEADER + "Palette Colour Count")); + assertNull(attributes.get(BMP_HEADER + "Important Colour Count")); + } + + public MockFlowFile verifyTestRunnerFlow(String pathStr,Relationship rel, String max) throws IOException { + Path path = Paths.get(pathStr); + testRunner.enqueue(path); + if(max != null) { + testRunner.setProperty(ExtractImageMetadata.MAX_NUMBER_OF_ATTRIBUTES, max); + } + + testRunner.run(); + testRunner.assertAllFlowFilesTransferred(rel, 1); + + + MockFlowFile flowFile = testRunner.getFlowFilesForRelationship(rel).get(0); + testRunner.assertQueueEmpty(); + + testRunner.enqueue(flowFile); + testRunner.clearTransferState(); + testRunner.run(); + testRunner.assertAllFlowFilesTransferred(rel, 1); + + flowFile = testRunner.getFlowFilesForRelationship(rel).get(0); + flowFile.assertContentEquals(new File(pathStr)); + return flowFile; + } +} http://git-wip-us.apache.org/repos/asf/nifi/blob/7e7d79fc/nifi-nar-bundles/nifi-media-bundle/nifi-media-processors/src/test/java/org/apache/nifi/processors/image/TestResizeImage.java ---------------------------------------------------------------------- diff --git a/nifi-nar-bundles/nifi-media-bundle/nifi-media-processors/src/test/java/org/apache/nifi/processors/image/TestResizeImage.java b/nifi-nar-bundles/nifi-media-bundle/nifi-media-processors/src/test/java/org/apache/nifi/processors/image/TestResizeImage.java new file mode 100644 index 0000000..53bf17e --- /dev/null +++ b/nifi-nar-bundles/nifi-media-bundle/nifi-media-processors/src/test/java/org/apache/nifi/processors/image/TestResizeImage.java @@ -0,0 +1,57 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.nifi.processors.image; + +import org.apache.nifi.util.MockFlowFile; +import org.apache.nifi.util.TestRunner; +import org.apache.nifi.util.TestRunners; +import org.junit.Test; + +import javax.imageio.ImageIO; +import java.awt.image.BufferedImage; +import java.io.ByteArrayInputStream; +import java.io.File; +import java.io.IOException; +import java.nio.file.Paths; + +import static org.junit.Assert.assertEquals; + +public class TestResizeImage { + + @Test + public void testResize() throws IOException { + final TestRunner runner = TestRunners.newTestRunner(new ResizeImage()); + runner.setProperty(ResizeImage.IMAGE_HEIGHT, "64"); + runner.setProperty(ResizeImage.IMAGE_WIDTH, "64"); + runner.setProperty(ResizeImage.SCALING_ALGORITHM, ResizeImage.RESIZE_SMOOTH); + + runner.enqueue(Paths.get("src/test/resources/simple.jpg")); + runner.run(); + + runner.assertAllFlowFilesTransferred(ResizeImage.REL_SUCCESS, 1); + final MockFlowFile mff = runner.getFlowFilesForRelationship(ResizeImage.REL_SUCCESS).get(0); + final byte[] data = mff.toByteArray(); + + final BufferedImage img = ImageIO.read(new ByteArrayInputStream(data)); + assertEquals(64, img.getWidth()); + assertEquals(64, img.getHeight()); + final File out = new File("target/simple.jpg"); + ImageIO.write(img, "JPG", out); + } + +} http://git-wip-us.apache.org/repos/asf/nifi/blob/7e7d79fc/nifi-nar-bundles/nifi-media-bundle/nifi-media-processors/src/test/java/org/apache/nifi/processors/media/TestExtractMediaMetadata.java ---------------------------------------------------------------------- diff --git a/nifi-nar-bundles/nifi-media-bundle/nifi-media-processors/src/test/java/org/apache/nifi/processors/media/TestExtractMediaMetadata.java b/nifi-nar-bundles/nifi-media-bundle/nifi-media-processors/src/test/java/org/apache/nifi/processors/media/TestExtractMediaMetadata.java new file mode 100644 index 0000000..40e3569 --- /dev/null +++ b/nifi-nar-bundles/nifi-media-bundle/nifi-media-processors/src/test/java/org/apache/nifi/processors/media/TestExtractMediaMetadata.java @@ -0,0 +1,456 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.nifi.processors.media; + +import org.apache.nifi.components.PropertyDescriptor; +import org.apache.nifi.processor.ProcessContext; +import org.apache.nifi.processor.Relationship; +import org.apache.nifi.provenance.ProvenanceEventRecord; +import org.apache.nifi.provenance.ProvenanceEventType; +import org.apache.nifi.util.MockFlowFile; +import org.apache.nifi.util.TestRunner; +import org.apache.nifi.util.TestRunners; +import org.junit.Test; + +import java.io.File; +import java.io.IOException; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Random; +import java.util.Set; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +public class TestExtractMediaMetadata { + + @Test + public void testProperties() { + final TestRunner runner = TestRunners.newTestRunner(new ExtractMediaMetadata()); + ProcessContext context = runner.getProcessContext(); + Map<PropertyDescriptor, String> propertyValues = context.getProperties(); + assertEquals(4, propertyValues.size()); + } + + @Test + public void testRelationships() { + final TestRunner runner = TestRunners.newTestRunner(new ExtractMediaMetadata()); + ProcessContext context = runner.getProcessContext(); + Set<Relationship> relationships = context.getAvailableRelationships(); + assertEquals(2, relationships.size()); + assertTrue(relationships.contains(ExtractMediaMetadata.SUCCESS)); + assertTrue(relationships.contains(ExtractMediaMetadata.FAILURE)); + } + + @Test + public void testTextBytes() throws IOException { + final TestRunner runner = TestRunners.newTestRunner(new ExtractMediaMetadata()); + runner.setProperty(ExtractMediaMetadata.METADATA_KEY_FILTER, ""); + runner.setProperty(ExtractMediaMetadata.METADATA_KEY_PREFIX, "txt."); + runner.assertValid(); + + final Map<String, String> attrs = new HashMap<>(); + attrs.put("filename", "test1.txt"); + runner.enqueue("test1".getBytes(), attrs); + runner.run(); + + runner.assertAllFlowFilesTransferred(ExtractMediaMetadata.SUCCESS, 1); + runner.assertTransferCount(ExtractMediaMetadata.FAILURE, 0); + + final List<MockFlowFile> successFiles = runner.getFlowFilesForRelationship(ExtractMediaMetadata.SUCCESS); + MockFlowFile flowFile0 = successFiles.get(0); + flowFile0.assertAttributeExists("filename"); + flowFile0.assertAttributeEquals("filename", "test1.txt"); + flowFile0.assertAttributeExists("txt.Content-Type"); + assertTrue(flowFile0.getAttribute("txt.Content-Type").startsWith("text/plain")); + flowFile0.assertAttributeExists("txt.X-Parsed-By"); + assertTrue(flowFile0.getAttribute("txt.X-Parsed-By").contains("org.apache.tika.parser.DefaultParser")); + assertTrue(flowFile0.getAttribute("txt.X-Parsed-By").contains("org.apache.tika.parser.txt.TXTParser")); + flowFile0.assertAttributeExists("txt.Content-Encoding"); + flowFile0.assertAttributeEquals("txt.Content-Encoding", "ISO-8859-1"); + flowFile0.assertContentEquals("test1".getBytes("UTF-8")); + } + + @Test + public void testProvenance() throws IOException { + final TestRunner runner = TestRunners.newTestRunner(new ExtractMediaMetadata()); + runner.setProperty(ExtractMediaMetadata.METADATA_KEY_FILTER, ""); + runner.setProperty(ExtractMediaMetadata.METADATA_KEY_PREFIX, "txt."); + runner.assertValid(); + + final Map<String, String> attrs = new HashMap<>(); + attrs.put("filename", "test1.txt"); + runner.enqueue("test1".getBytes(), attrs); + runner.run(); + + runner.assertAllFlowFilesTransferred(ExtractMediaMetadata.SUCCESS, 1); + runner.assertTransferCount(ExtractMediaMetadata.FAILURE, 0); + + final List<ProvenanceEventRecord> events = runner.getProvenanceEvents(); + assertEquals(1, events.size()); + + final ProvenanceEventRecord event = events.get(0); + assertEquals(ExtractMediaMetadata.class.getSimpleName(), event.getComponentType()); + assertEquals("media attributes extracted", event.getDetails()); + assertEquals(ProvenanceEventType.ATTRIBUTES_MODIFIED, event.getEventType()); + } + + @Test + public void testNoFlowFile() throws IOException { + final TestRunner runner = TestRunners.newTestRunner(new ExtractMediaMetadata()); + runner.setProperty(ExtractMediaMetadata.METADATA_KEY_FILTER, ""); + runner.setProperty(ExtractMediaMetadata.METADATA_KEY_PREFIX, "txt."); + runner.assertValid(); + + runner.run(); + + runner.assertAllFlowFilesTransferred(ExtractMediaMetadata.SUCCESS, 0); + runner.assertTransferCount(ExtractMediaMetadata.FAILURE, 0); + } + + @Test + public void testTextFile() throws IOException { + final TestRunner runner = TestRunners.newTestRunner(new ExtractMediaMetadata()); + runner.setProperty(ExtractMediaMetadata.METADATA_KEY_FILTER, ""); + runner.setProperty(ExtractMediaMetadata.METADATA_KEY_PREFIX, "txt."); + runner.assertValid(); + + runner.enqueue(new File("target/test-classes/textFile.txt").toPath()); + runner.run(); + + runner.assertAllFlowFilesTransferred(ExtractMediaMetadata.SUCCESS, 1); + runner.assertTransferCount(ExtractMediaMetadata.FAILURE, 0); + + final List<MockFlowFile> successFiles = runner.getFlowFilesForRelationship(ExtractMediaMetadata.SUCCESS); + MockFlowFile flowFile0 = successFiles.get(0); + flowFile0.assertAttributeExists("filename"); + flowFile0.assertAttributeEquals("filename", "textFile.txt"); + flowFile0.assertAttributeExists("txt.Content-Type"); + assertTrue(flowFile0.getAttribute("txt.Content-Type").startsWith("text/plain")); + flowFile0.assertAttributeExists("txt.X-Parsed-By"); + assertTrue(flowFile0.getAttribute("txt.X-Parsed-By").contains("org.apache.tika.parser.DefaultParser")); + assertTrue(flowFile0.getAttribute("txt.X-Parsed-By").contains("org.apache.tika.parser.txt.TXTParser")); + flowFile0.assertAttributeExists("txt.Content-Encoding"); + flowFile0.assertAttributeEquals("txt.Content-Encoding", "ISO-8859-1"); + flowFile0.assertContentEquals("This file is not an image and is used for testing the image metadata extractor.".getBytes("UTF-8")); + } + + @Test + public void testBigTextFile() throws IOException { + File textFile = new File("target/test-classes/textFileBig.txt"); + + final TestRunner runner = TestRunners.newTestRunner(new ExtractMediaMetadata()); + runner.setProperty(ExtractMediaMetadata.METADATA_KEY_PREFIX, "txt."); + runner.assertValid(); + + runner.enqueue(textFile.toPath()); + runner.run(2); + + runner.assertAllFlowFilesTransferred(ExtractMediaMetadata.SUCCESS, 1); + runner.assertTransferCount(ExtractMediaMetadata.FAILURE, 0); + + final List<MockFlowFile> successFiles = runner.getFlowFilesForRelationship(ExtractMediaMetadata.SUCCESS); + MockFlowFile flowFile0 = successFiles.get(0); + flowFile0.assertAttributeExists("filename"); + flowFile0.assertAttributeEquals("filename", "textFileBig.txt"); + flowFile0.assertAttributeExists("txt.Content-Type"); + assertTrue(flowFile0.getAttribute("txt.Content-Type").startsWith("text/plain")); + flowFile0.assertAttributeExists("txt.X-Parsed-By"); + assertTrue(flowFile0.getAttribute("txt.X-Parsed-By").contains("org.apache.tika.parser.DefaultParser")); + assertTrue(flowFile0.getAttribute("txt.X-Parsed-By").contains("org.apache.tika.parser.txt.TXTParser")); + flowFile0.assertAttributeExists("txt.Content-Encoding"); + flowFile0.assertAttributeEquals("txt.Content-Encoding", "ISO-8859-1"); + assertEquals(flowFile0.getSize(), textFile.length()); + } + + @Test + public void testJunkBytes() throws IOException { + final TestRunner runner = TestRunners.newTestRunner(new ExtractMediaMetadata()); + runner.setProperty(ExtractMediaMetadata.METADATA_KEY_FILTER, ""); + runner.setProperty(ExtractMediaMetadata.METADATA_KEY_PREFIX, "junk."); + runner.assertValid(); + + final Map<String, String> attrs = new HashMap<>(); + attrs.put("filename", "junk"); + Random random = new Random(); + byte[] bytes = new byte[2048]; + random.nextBytes(bytes); + runner.enqueue(bytes, attrs); + runner.run(); + + runner.assertAllFlowFilesTransferred(ExtractMediaMetadata.SUCCESS, 1); + runner.assertTransferCount(ExtractMediaMetadata.FAILURE, 0); + + final List<MockFlowFile> successFiles = runner.getFlowFilesForRelationship(ExtractMediaMetadata.SUCCESS); + MockFlowFile flowFile0 = successFiles.get(0); + flowFile0.assertAttributeExists("filename"); + flowFile0.assertAttributeEquals("filename", "junk"); + flowFile0.assertAttributeExists("junk.Content-Type"); + assertTrue(flowFile0.getAttribute("junk.Content-Type").startsWith("application/octet-stream")); + flowFile0.assertAttributeExists("junk.X-Parsed-By"); + assertTrue(flowFile0.getAttribute("junk.X-Parsed-By").contains("org.apache.tika.parser.EmptyParser")); + flowFile0.assertContentEquals(bytes); + } + + @Test + public void testMetadataKeyFilter() throws IOException { + final TestRunner runner = TestRunners.newTestRunner(new ExtractMediaMetadata()); + runner.setProperty(ExtractMediaMetadata.METADATA_KEY_FILTER, "(X-Parsed.*)"); + runner.setProperty(ExtractMediaMetadata.METADATA_KEY_PREFIX, "txt."); + runner.assertValid(); + + runner.enqueue(new File("target/test-classes/textFile.txt").toPath()); + runner.run(); + + runner.assertAllFlowFilesTransferred(ExtractMediaMetadata.SUCCESS, 1); + runner.assertTransferCount(ExtractMediaMetadata.FAILURE, 0); + + final List<MockFlowFile> successFiles = runner.getFlowFilesForRelationship(ExtractMediaMetadata.SUCCESS); + MockFlowFile flowFile0 = successFiles.get(0); + flowFile0.assertAttributeExists("filename"); + flowFile0.assertAttributeEquals("filename", "textFile.txt"); + flowFile0.assertAttributeExists("txt.X-Parsed-By"); + assertTrue(flowFile0.getAttribute("txt.X-Parsed-By").contains("org.apache.tika.parser.DefaultParser")); + assertTrue(flowFile0.getAttribute("txt.X-Parsed-By").contains("org.apache.tika.parser.txt.TXTParser")); + flowFile0.assertAttributeNotExists("txt.Content-Encoding"); + } + + @Test + public void testMetadataKeyPrefix() throws IOException { + TestRunner runner = TestRunners.newTestRunner(new ExtractMediaMetadata()); + runner.assertValid(); + + runner.enqueue(new File("target/test-classes/textFile.txt").toPath()); + runner.run(); + + runner.assertAllFlowFilesTransferred(ExtractMediaMetadata.SUCCESS, 1); + runner.assertTransferCount(ExtractMediaMetadata.FAILURE, 0); + + List<MockFlowFile> successFiles = runner.getFlowFilesForRelationship(ExtractMediaMetadata.SUCCESS); + MockFlowFile flowFile0 = successFiles.get(0); + flowFile0.assertAttributeExists("filename"); + flowFile0.assertAttributeExists("X-Parsed-By"); + + runner = TestRunners.newTestRunner(new ExtractMediaMetadata()); + runner.setProperty(ExtractMediaMetadata.METADATA_KEY_PREFIX, "txt."); + runner.assertValid(); + + runner.enqueue(new File("target/test-classes/textFile.txt").toPath()); + runner.run(); + + runner.assertAllFlowFilesTransferred(ExtractMediaMetadata.SUCCESS, 1); + runner.assertTransferCount(ExtractMediaMetadata.FAILURE, 0); + + successFiles = runner.getFlowFilesForRelationship(ExtractMediaMetadata.SUCCESS); + flowFile0 = successFiles.get(0); + flowFile0.assertAttributeExists("filename"); + flowFile0.assertAttributeExists("txt.X-Parsed-By"); + } + + @Test + public void testMaxAttributes() throws IOException { + TestRunner runner = TestRunners.newTestRunner(new ExtractMediaMetadata()); + runner.setProperty(ExtractMediaMetadata.METADATA_KEY_PREFIX, "txt."); + runner.assertValid(); + + runner.enqueue(new File("target/test-classes/textFile.txt").toPath()); + runner.run(); + + runner.assertAllFlowFilesTransferred(ExtractMediaMetadata.SUCCESS, 1); + runner.assertTransferCount(ExtractMediaMetadata.FAILURE, 0); + + List<MockFlowFile> successFiles0 = runner.getFlowFilesForRelationship(ExtractMediaMetadata.SUCCESS); + MockFlowFile flowFile0 = successFiles0.get(0); + int fileAttrCount0 = 0; + for (Map.Entry attr : flowFile0.getAttributes().entrySet()) { + if (attr.getKey().toString().startsWith("txt.")) { + fileAttrCount0++; + } + } + assertTrue(fileAttrCount0 > 1); + + runner = TestRunners.newTestRunner(new ExtractMediaMetadata()); + runner.setProperty(ExtractMediaMetadata.MAX_NUMBER_OF_ATTRIBUTES, Integer.toString(fileAttrCount0 - 1)); + runner.setProperty(ExtractMediaMetadata.METADATA_KEY_PREFIX, "txt."); + runner.assertValid(); + + runner.enqueue(new File("target/test-classes/textFile.txt").toPath()); + runner.run(); + + runner.assertAllFlowFilesTransferred(ExtractMediaMetadata.SUCCESS, 1); + runner.assertTransferCount(ExtractMediaMetadata.FAILURE, 0); + + List<MockFlowFile> successFiles = runner.getFlowFilesForRelationship(ExtractMediaMetadata.SUCCESS); + MockFlowFile flowFile1 = successFiles.get(0); + int fileAttrCount1 = 0; + for (Map.Entry attr : flowFile1.getAttributes().entrySet()) { + if (attr.getKey().toString().startsWith("txt.")) { + fileAttrCount1++; + } + } + assertEquals(fileAttrCount0, fileAttrCount1 + 1); + } + + @Test + public void testBmp() throws IOException { + final TestRunner runner = TestRunners.newTestRunner(new ExtractMediaMetadata()); + runner.setProperty(ExtractMediaMetadata.METADATA_KEY_PREFIX, "bmp."); + runner.assertValid(); + + runner.enqueue(new File("target/test-classes/16color-10x10.bmp").toPath()); + runner.run(2); + + runner.assertAllFlowFilesTransferred(ExtractMediaMetadata.SUCCESS, 1); + runner.assertTransferCount(ExtractMediaMetadata.FAILURE, 0); + + final List<MockFlowFile> successFiles = runner.getFlowFilesForRelationship(ExtractMediaMetadata.SUCCESS); + MockFlowFile flowFile0 = successFiles.get(0); + flowFile0.assertAttributeExists("filename"); + flowFile0.assertAttributeEquals("filename", "16color-10x10.bmp"); + flowFile0.assertAttributeExists("bmp.Content-Type"); + flowFile0.assertAttributeEquals("bmp.Content-Type", "image/x-ms-bmp"); + flowFile0.assertAttributeExists("bmp.X-Parsed-By"); + assertTrue(flowFile0.getAttribute("bmp.X-Parsed-By").contains("org.apache.tika.parser.DefaultParser")); + assertTrue(flowFile0.getAttribute("bmp.X-Parsed-By").contains("org.apache.tika.parser.image.ImageParser")); + flowFile0.assertAttributeExists("bmp.height"); + flowFile0.assertAttributeEquals("bmp.height", "10"); + flowFile0.assertAttributeExists("bmp.width"); + flowFile0.assertAttributeEquals("bmp.width", "10"); + } + + @Test + public void testJpg() throws IOException { + final TestRunner runner = TestRunners.newTestRunner(new ExtractMediaMetadata()); + runner.setProperty(ExtractMediaMetadata.METADATA_KEY_PREFIX, "jpg."); + runner.assertValid(); + + runner.enqueue(new File("target/test-classes/simple.jpg").toPath()); + runner.run(2); + + runner.assertAllFlowFilesTransferred(ExtractMediaMetadata.SUCCESS, 1); + runner.assertTransferCount(ExtractMediaMetadata.FAILURE, 0); + + final List<MockFlowFile> successFiles = runner.getFlowFilesForRelationship(ExtractMediaMetadata.SUCCESS); + MockFlowFile flowFile0 = successFiles.get(0); + flowFile0.assertAttributeExists("filename"); + flowFile0.assertAttributeEquals("filename", "simple.jpg"); + flowFile0.assertAttributeExists("jpg.Content-Type"); + flowFile0.assertAttributeEquals("jpg.Content-Type", "image/jpeg"); + flowFile0.assertAttributeExists("jpg.X-Parsed-By"); + assertTrue(flowFile0.getAttribute("jpg.X-Parsed-By").contains("org.apache.tika.parser.DefaultParser")); + assertTrue(flowFile0.getAttribute("jpg.X-Parsed-By").contains("org.apache.tika.parser.jpeg.JpegParser")); + } + + @Test + public void testWav() throws IOException { + final TestRunner runner = TestRunners.newTestRunner(new ExtractMediaMetadata()); + runner.setProperty(ExtractMediaMetadata.METADATA_KEY_FILTER, ""); + runner.setProperty(ExtractMediaMetadata.METADATA_KEY_PREFIX, "wav."); + runner.assertValid(); + + runner.enqueue(new File("target/test-classes/testWAV.wav").toPath()); + runner.run(); + + runner.assertAllFlowFilesTransferred(ExtractMediaMetadata.SUCCESS, 1); + runner.assertTransferCount(ExtractMediaMetadata.FAILURE, 0); + + final List<MockFlowFile> successFiles = runner.getFlowFilesForRelationship(ExtractMediaMetadata.SUCCESS); + MockFlowFile flowFile0 = successFiles.get(0); + flowFile0.assertAttributeExists("filename"); + flowFile0.assertAttributeEquals("filename", "testWAV.wav"); + flowFile0.assertAttributeExists("wav.Content-Type"); + assertTrue(flowFile0.getAttribute("wav.Content-Type").startsWith("audio/x-wav")); + flowFile0.assertAttributeExists("wav.X-Parsed-By"); + assertTrue(flowFile0.getAttribute("wav.X-Parsed-By").contains("org.apache.tika.parser.DefaultParser")); + assertTrue(flowFile0.getAttribute("wav.X-Parsed-By").contains("org.apache.tika.parser.audio.AudioParser")); + flowFile0.assertAttributeExists("wav.encoding"); + flowFile0.assertAttributeEquals("wav.encoding", "PCM_SIGNED"); + } + + @Test + public void testOgg() throws IOException { + final TestRunner runner = TestRunners.newTestRunner(new ExtractMediaMetadata()); + runner.setProperty(ExtractMediaMetadata.METADATA_KEY_FILTER, ""); + runner.setProperty(ExtractMediaMetadata.METADATA_KEY_PREFIX, "ogg."); + runner.assertValid(); + + runner.enqueue(new File("target/test-classes/testVORBIS.ogg").toPath()); + runner.run(); + + runner.assertAllFlowFilesTransferred(ExtractMediaMetadata.SUCCESS, 1); + runner.assertTransferCount(ExtractMediaMetadata.FAILURE, 0); + + final List<MockFlowFile> successFiles = runner.getFlowFilesForRelationship(ExtractMediaMetadata.SUCCESS); + MockFlowFile flowFile0 = successFiles.get(0); + flowFile0.assertAttributeExists("filename"); + flowFile0.assertAttributeEquals("filename", "testVORBIS.ogg"); + flowFile0.assertAttributeExists("ogg.Content-Type"); + assertTrue(flowFile0.getAttribute("ogg.Content-Type").startsWith("audio/vorbis")); + flowFile0.assertAttributeExists("ogg.X-Parsed-By"); + assertTrue(flowFile0.getAttribute("ogg.X-Parsed-By").contains("org.apache.tika.parser.DefaultParser")); + assertTrue(flowFile0.getAttribute("ogg.X-Parsed-By").contains("org.gagravarr.tika.VorbisParser")); + } + + @Test + public void testOggCorruptFails() throws IOException { + final TestRunner runner = TestRunners.newTestRunner(new ExtractMediaMetadata()); + runner.setProperty(ExtractMediaMetadata.METADATA_KEY_FILTER, ""); + runner.setProperty(ExtractMediaMetadata.METADATA_KEY_PREFIX, "ogg."); + runner.assertValid(); + + runner.enqueue(new File("target/test-classes/testVORBIS-corrupt.ogg").toPath()); + runner.run(2); + + runner.assertTransferCount(ExtractMediaMetadata.SUCCESS, 0); + runner.assertTransferCount(ExtractMediaMetadata.FAILURE, 1); + + final List<MockFlowFile> failureFiles = runner.getFlowFilesForRelationship(ExtractMediaMetadata.FAILURE); + MockFlowFile flowFile0 = failureFiles.get(0); + flowFile0.assertAttributeExists("filename"); + flowFile0.assertAttributeEquals("filename", "testVORBIS-corrupt.ogg"); + flowFile0.assertAttributeNotExists("ogg.Content-Type"); + flowFile0.assertAttributeNotExists("ogg.X-Parsed-By"); + } + + @Test + public void testMp3() throws IOException { + final TestRunner runner = TestRunners.newTestRunner(new ExtractMediaMetadata()); + runner.setProperty(ExtractMediaMetadata.METADATA_KEY_FILTER, ""); + runner.setProperty(ExtractMediaMetadata.METADATA_KEY_PREFIX, "mp3."); + runner.assertValid(); + + runner.enqueue(new File("target/test-classes/testMP3id3v1.mp3").toPath()); + runner.run(); + + runner.assertAllFlowFilesTransferred(ExtractMediaMetadata.SUCCESS, 1); + runner.assertTransferCount(ExtractMediaMetadata.FAILURE, 0); + + final List<MockFlowFile> successFiles = runner.getFlowFilesForRelationship(ExtractMediaMetadata.SUCCESS); + MockFlowFile flowFile0 = successFiles.get(0); + flowFile0.assertAttributeExists("filename"); + flowFile0.assertAttributeEquals("filename", "testMP3id3v1.mp3"); + flowFile0.assertAttributeExists("mp3.Content-Type"); + assertTrue(flowFile0.getAttribute("mp3.Content-Type").startsWith("audio/mpeg")); + flowFile0.assertAttributeExists("mp3.X-Parsed-By"); + assertTrue(flowFile0.getAttribute("mp3.X-Parsed-By").contains("org.apache.tika.parser.DefaultParser")); + assertTrue(flowFile0.getAttribute("mp3.X-Parsed-By").contains("org.apache.tika.parser.mp3.Mp3Parser")); + flowFile0.assertAttributeExists("mp3.title"); + flowFile0.assertAttributeEquals("mp3.title", "Test Title"); + } + +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/nifi/blob/7e7d79fc/nifi-nar-bundles/nifi-media-bundle/nifi-media-processors/src/test/resources/16color-10x10.bmp ---------------------------------------------------------------------- diff --git a/nifi-nar-bundles/nifi-media-bundle/nifi-media-processors/src/test/resources/16color-10x10.bmp b/nifi-nar-bundles/nifi-media-bundle/nifi-media-processors/src/test/resources/16color-10x10.bmp new file mode 100644 index 0000000..26206f4 Binary files /dev/null and b/nifi-nar-bundles/nifi-media-bundle/nifi-media-processors/src/test/resources/16color-10x10.bmp differ http://git-wip-us.apache.org/repos/asf/nifi/blob/7e7d79fc/nifi-nar-bundles/nifi-media-bundle/nifi-media-processors/src/test/resources/mspaint-8x10.png ---------------------------------------------------------------------- diff --git a/nifi-nar-bundles/nifi-media-bundle/nifi-media-processors/src/test/resources/mspaint-8x10.png b/nifi-nar-bundles/nifi-media-bundle/nifi-media-processors/src/test/resources/mspaint-8x10.png new file mode 100644 index 0000000..9e74690 Binary files /dev/null and b/nifi-nar-bundles/nifi-media-bundle/nifi-media-processors/src/test/resources/mspaint-8x10.png differ
