streamlet-examples/__relpipe_in_filesystem_script_exiv2
branchv_0
changeset 40 a41423ac9e94
parent 39 225da9aa9c94
equal deleted inserted replaced
39:225da9aa9c94 40:a41423ac9e94
       
     1 #!/bin/bash
       
     2 
       
     3 # Relational pipes
       
     4 # Copyright © 2020 František Kučera (Frantovo.cz, GlobalCode.info)
       
     5 #
       
     6 # This program is free software: you can redistribute it and/or modify
       
     7 # it under the terms of the GNU General Public License as published by
       
     8 # the Free Software Foundation, version 3 of the License.
       
     9 #
       
    10 # This program is distributed in the hope that it will be useful,
       
    11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
       
    13 # GNU General Public License for more details.
       
    14 #
       
    15 # You should have received a copy of the GNU General Public License
       
    16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
       
    17 
       
    18 
       
    19 # This streamlet provides EXIF metadata. It calls the tool exiv2.
       
    20 # With no options it returns "Image size", "Copyright" and "Exif comment" attributes.
       
    21 # Specific attributes can be selected using options – e.g. --option 'attribute' 'Image size'
       
    22 # List of available attributes can be obtained by directly calling the exiv2 command on a image file.
       
    23 # Two additional attributes are provided by this streamlet: "Image height" and "Image width" (they are extracted from "Image size").
       
    24 
       
    25 
       
    26 . "$(dirname $0)/streamlet-common.sh"
       
    27 
       
    28 processMessage_WAITING_FOR_OUTPUT_ATTRIBUTES_METADATA() {
       
    29 	streamletFields=()
       
    30 
       
    31 	for (( i=0; i<${#optionNames[@]}; i++)); do
       
    32 		if [[ "x${optionNames[$i]}" == "xattribute" ]]; then
       
    33 			streamletFields+=("${optionValues[$i]}");
       
    34 		elif [[ "x${optionNames[$i]}" == "xprefix" ]]; then
       
    35 			pdfPrefix="${optionValues[$i]}";
       
    36 		else
       
    37 			echo "Unsupported option: ${optionNames[$i]}" >&2
       
    38 		fi
       
    39 	done
       
    40 
       
    41 	if [[ -z "$streamletFields" ]]; then
       
    42 		streamletFields=( "Image size" "Copyright" "Exif comment" );
       
    43 	fi
       
    44 
       
    45 	for (( i=0; i<${#streamletFields[@]}; i++)); do
       
    46 		if [[ "x${streamletFields[$i]}" == "xImage height" ]] ||[[ "x${streamletFields[$i]}" == "xImage width" ]]; then local type="integer"; else local type="string"; fi
       
    47 		send OUTPUT_ATTRIBUTE_METADATA "${outputAttributeAliases[$i]-$pdfPrefix${streamletFields[$i]}}"    "$type"
       
    48 	done
       
    49 
       
    50 	send WAITING_FOR_INPUT_ATTRIBUTES
       
    51 }
       
    52 
       
    53 processMessage_WAITING_FOR_OUTPUT_ATTRIBUTES() {
       
    54 	local streamletInfo streamletValid value isNull;
       
    55 
       
    56 	[[ -d "$currentFile" ]] || streamletInfo="$(exiv2 "$currentFile")";
       
    57 	streamletValid="$?";
       
    58 
       
    59 	for (( i=0; i<${#streamletFields[@]}; i++)); do
       
    60 		value="$(echo "$streamletInfo" | grep -P "^\Q${streamletFields[$i]}\E\s*:" | sed -E 's/[^:]+:\s+(.*)/\1/g' | tr -d '\n';)"; # the field name must not contain "\E"
       
    61 
       
    62 		if   [[ -z "$value" ]] && [[ "x${streamletFields[$i]}" == "xImage width"  ]]; then value="$(echo "$streamletInfo" | grep -E 'Image size\s*:\s*[0-9]+ x [0-9]+' | sed -E 's/Image size\s*:\s*([0-9]+) x ([0-9]+)/\1/g')";
       
    63 		elif [[ -z "$value" ]] && [[ "x${streamletFields[$i]}" == "xImage height" ]]; then value="$(echo "$streamletInfo" | grep -E 'Image size\s*:\s*[0-9]+ x [0-9]+' | sed -E 's/Image size\s*:\s*([0-9]+) x ([0-9]+)/\2/g')";
       
    64 		fi
       
    65 
       
    66 		# n.b. if file has no exif data, exiv2 exits with error „No Exif data found in the file“ and thus $streamletValid != 0, but there still might be some value like „Image size“
       
    67 		if   [[ ! "x$streamletValid" == "x0" ]] && [[ "x$value" == "x" ]]; then value="";  isNull="true";
       
    68 		else                                                                               isNull="false";
       
    69 		fi
       
    70 
       
    71 		if ( [[ "x${streamletFields[$i]}" == "xImage height" ]] || [[ "x${streamletFields[$i]}" == "xImage width" ]] ) && [[ ! "$value" =~ ^[0-9]+$ ]]; then value="0"; isNull="true"; fi
       
    72 
       
    73 		send OUTPUT_ATTRIBUTE "$value"    "$isNull";
       
    74 	done
       
    75 	
       
    76 	send WAITING_FOR_INPUT_ATTRIBUTES;
       
    77 }
       
    78 
       
    79 initialize
       
    80 processMessages