streamlet-examples/exiftool
author František Kučera <franta-hg@frantovo.cz>
Fri, 13 May 2022 21:35:30 +0200
branchv_0
changeset 96 c34106244a54
parent 71 f8fe085c1c9f
permissions -rwxr-xr-x
portable order of (i++) parameters

#!/bin/bash

# Relational pipes
# Copyright © 2020 František Kučera (Frantovo.cz, GlobalCode.info)
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, version 3 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.


# This streamlet provides various file metadata like EXIF or PDF. It calls the tool exiftool.
# With no options it returns "File:MIMEType" and "exiftool_xml" attributes.
# Specific attributes can be selected using options – e.g. --option 'attribute' '…'
# List of available attributes can be obtained by directly calling the "exiftool -X" command on given file or from the "available_attributes" attribute.
# Two additional attributes are provided by this streamlet:
#  - "exiftool_xml" – all attributes provided by exiftool in form of XML
#  - "available_attributes" – list of available attributes (each file may have different) separated by line-breaks (TODO: return as an array of strings, when this data type is implemented)


. "$(dirname "$(realpath "$0")")/streamlet-common.sh"

processMessage_WAITING_FOR_OUTPUT_ATTRIBUTES_METADATA() {
	streamletFields=()

	for (( i=0; i<${#optionNames[@]}; i++)); do
		if [[ "x${optionNames[$i]}" == "xattribute" ]]; then
			streamletFields+=("${optionValues[$i]}");
		elif [[ "x${optionNames[$i]}" == "xprefix" ]]; then
			pdfPrefix="${optionValues[$i]}";
		else
			echo "Unsupported option: ${optionNames[$i]}" >&2
		fi
	done

	if [[ -z "$streamletFields" ]]; then
		streamletFields=( "File:MIMEType" "exiftool_xml" );
	fi

	for (( i=0; i<${#streamletFields[@]}; i++)); do
		local exifIntegerFields="^(PNG:ImageWidth|PNG:ImageHeight|PNG:BitDepth|File:ImageWidth|File:ImageHeight|File:BitsPerSample|File:ColorComponents)$";
		if [[ "${streamletFields[$i]}" =~ "$exifIntegerFields" ]]; then type="integer";
		else type="string";
		fi
		send OUTPUT_ATTRIBUTE_METADATA "${outputAttributeAliases[$i]-$pdfPrefix${streamletFields[$i]}}"    "$type"
	done

	send WAITING_FOR_INPUT_ATTRIBUTES
}

processMessage_WAITING_FOR_OUTPUT_ATTRIBUTES() {
	local streamletInfo streamletValid value isNull;

	[[ -d "$currentFile" ]] || streamletInfo="$(exiftool -X "$currentFile")";
	streamletValid="$?";

	for (( i=0; i<${#streamletFields[@]}; i++)); do
		if   [[ "x${streamletFields[$i]}" == "xexiftool_xml"  ]]; then value="$streamletInfo";
		elif [[ "x${streamletFields[$i]}" == "xavailable_attributes"  ]]; then
			value=$'available_attributes\nexiftool_xml\n'"$(echo "$streamletInfo" | relpipe-in-xmltable --relation exif --records '/*/*/*' --attribute 'name' string 'name()' | relpipe-out-nullbyte | tr \\0 \\n)";
		else
			value="$(echo "$streamletInfo" | relpipe-in-xmltable --relation exif --records "/*/*/*[name() = '${streamletFields[$i]}']" --attribute 'value' string '.' | relpipe-out-nullbyte | tr -d \\0)";
			# TODO: parse the XML only once
			# TODO: validate parameter or use parametrized XPath
			# TODO: use real namespaces
		fi

		# n.b. for some files exiftools returns exit code, however it provides some basic properties like file timestamps and <ExifTool:Error>Unknown file type</ExifTool:Error> which is also valid XML and might be useful
		if   [[ ! "x$streamletValid" == "x0" ]] || [[ "x$value" == "x" ]]; then isNull="true";
		else                                                                    isNull="false";
		fi

		send OUTPUT_ATTRIBUTE "$value"    "$isNull";
	done
	
	send WAITING_FOR_INPUT_ATTRIBUTES;
}

initialize
processMessages