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 various file metadata like EXIF or PDF. It calls the tool exiftool. |
|
20 # With no options it returns "File:MIMEType" and "exiftool_xml" attributes. |
|
21 # Specific attributes can be selected using options – e.g. --option 'attribute' '…' |
|
22 # List of available attributes can be obtained by directly calling the "exiftool -X" command on given file or from the "available_attributes" attribute. |
|
23 # Two additional attributes are provided by this streamlet: |
|
24 # - "exiftool_xml" – all attributes provided by exiftool in form of XML |
|
25 # - "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) |
|
26 |
|
27 |
|
28 . "$(dirname $0)/streamlet-common.sh" |
|
29 |
|
30 processMessage_WAITING_FOR_OUTPUT_ATTRIBUTES_METADATA() { |
|
31 streamletFields=() |
|
32 |
|
33 for (( i=0; i<${#optionNames[@]}; i++)); do |
|
34 if [[ "x${optionNames[$i]}" == "xattribute" ]]; then |
|
35 streamletFields+=("${optionValues[$i]}"); |
|
36 elif [[ "x${optionNames[$i]}" == "xprefix" ]]; then |
|
37 pdfPrefix="${optionValues[$i]}"; |
|
38 else |
|
39 echo "Unsupported option: ${optionNames[$i]}" >&2 |
|
40 fi |
|
41 done |
|
42 |
|
43 if [[ -z "$streamletFields" ]]; then |
|
44 streamletFields=( "File:MIMEType" "exiftool_xml" ); |
|
45 fi |
|
46 |
|
47 for (( i=0; i<${#streamletFields[@]}; i++)); do |
|
48 # TODO: data type mappings (integers, booleans) |
|
49 send OUTPUT_ATTRIBUTE_METADATA "${outputAttributeAliases[$i]-$pdfPrefix${streamletFields[$i]}}" "string" |
|
50 done |
|
51 |
|
52 send WAITING_FOR_INPUT_ATTRIBUTES |
|
53 } |
|
54 |
|
55 processMessage_WAITING_FOR_OUTPUT_ATTRIBUTES() { |
|
56 local streamletInfo streamletValid value isNull; |
|
57 |
|
58 [[ -d "$currentFile" ]] || streamletInfo="$(exiftool -X "$currentFile")"; |
|
59 streamletValid="$?"; |
|
60 |
|
61 for (( i=0; i<${#streamletFields[@]}; i++)); do |
|
62 if [[ "x${streamletFields[$i]}" == "xexiftool_xml" ]]; then value="$streamletInfo"; |
|
63 elif [[ "x${streamletFields[$i]}" == "xavailable_attributes" ]]; then |
|
64 value=$'available_attributes\nexiftool_xml\n'"$(echo "$streamletInfo" | relpipe-in-xmltable --relation exif --records '/*/*/*' --attribute 'name' string 'name()' | relpipe-out-nullbyte | tr \\0 \\n)"; |
|
65 else |
|
66 value="$(echo "$streamletInfo" | relpipe-in-xmltable --relation exif --records "/*/*/*[name() = '${streamletFields[$i]}']" --attribute 'value' string '.' | relpipe-out-nullbyte | tr -d \\0)"; |
|
67 # TODO: parse the XML only once |
|
68 # TODO: validate parameter or use parametrized XPath |
|
69 # TODO: use real namespaces |
|
70 fi |
|
71 |
|
72 # 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 |
|
73 if [[ ! "x$streamletValid" == "x0" ]] && [[ "x$value" == "x" ]]; then value=""; isNull="true"; |
|
74 else isNull="false"; |
|
75 fi |
|
76 |
|
77 send OUTPUT_ATTRIBUTE "$value" "$isNull"; |
|
78 done |
|
79 |
|
80 send WAITING_FOR_INPUT_ATTRIBUTES; |
|
81 } |
|
82 |
|
83 initialize |
|
84 processMessages |
|