diff -r dc5c210295d0 -r f466b4c7d9b1 streamlet-examples/exiftool --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/streamlet-examples/exiftool Sat Jan 18 20:09:34 2020 +0100 @@ -0,0 +1,84 @@ +#!/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 . + + +# 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 $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 + # TODO: data type mappings (integers, booleans) + send OUTPUT_ATTRIBUTE_METADATA "${outputAttributeAliases[$i]-$pdfPrefix${streamletFields[$i]}}" "string" + 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 Unknown file type which is also valid XML and might be useful + if [[ ! "x$streamletValid" == "x0" ]] && [[ "x$value" == "x" ]]; then value=""; isNull="true"; + else isNull="false"; + fi + + send OUTPUT_ATTRIBUTE "$value" "$isNull"; + done + + send WAITING_FOR_INPUT_ATTRIBUTES; +} + +initialize +processMessages