aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorXavier Del Campo Romero <xavi.dcr@tutanota.com>2023-07-08 03:39:40 +0200
committerXavier Del Campo Romero <xavi.dcr@tutanota.com>2023-07-11 13:30:17 +0200
commit05a76f289912a7a0cdf3749057c376019fe0b68b (patch)
treef8cd7ffae4f1cb614dc0d1419eba4d1a5393bbfc
parentaae26c510dd8a1228848d75c33a07eb992f8dd2a (diff)
WIP add thumbnail CLI toolsthumbnail
-rw-r--r--README.md4
-rwxr-xr-xtngen76
-rwxr-xr-xwatchdir52
3 files changed, 131 insertions, 1 deletions
diff --git a/README.md b/README.md
index 1c62dba..b6ea50d 100644
--- a/README.md
+++ b/README.md
@@ -45,6 +45,8 @@ to `slcl`. If required, encryption should be done before uploading e.g.: using
- `xxd` (for [`usergen`](usergen) only).
- `jq` (for [`usergen`](usergen) only).
- CMake (optional).
+- `entr` (for [`watchdir`](watchdir) only).
+- ImageMagick (for [`tngen`](tngen) only).
### Ubuntu / Debian
@@ -57,7 +59,7 @@ sudo apt install build-essential libcjson-dev libssl-dev
#### Optional packages
```sh
-sudo apt install cmake xxd jq
+sudo apt install cmake xxd jq inotify-tools imagemagick
```
## How to use
diff --git a/tngen b/tngen
new file mode 100755
index 0000000..6b871de
--- /dev/null
+++ b/tngen
@@ -0,0 +1,76 @@
+#! /bin/sh
+
+set -e
+
+usage()
+{
+ echo "$0 [-s <size>] [-h] -d <dir> <path>"
+}
+
+SIZE=96
+
+while getopts s:d:h arg
+do
+ case $arg in
+ d) DIR="$OPTARG"
+ ;;
+ s) SIZE="$OPTARG"
+ ;;
+ h) usage
+ exit 0
+ ;;
+ ?) usage >&2
+ exit 1
+ ;;
+ esac
+done
+
+if [ -z "$DIR" ]
+then
+ usage >&2
+ exit 1
+fi
+
+shift $(($OPTIND - 1))
+
+if [ $# != 1 ]; then
+ usage >&2
+ exit 1
+fi
+
+F="$1"
+
+gen()
+{
+ IN="$1"
+ OUT="$2"
+
+ if [ -z "$OUT" ]
+ then
+ echo Expected output filename >&2
+ return 1
+ fi
+
+ mkdir -p "$(dirname "$OUT")"
+
+ if convert -thumbnail x$SIZE "$IN" "$OUT"
+ then
+ echo Created $OUT
+ else
+ echo Failed to create $OUT >&2
+ fi
+}
+
+while read f; do
+ if [ "$f" != "" ]
+ then
+ THUMBNAIL=$(echo "$f" | sed "s,$DIR/user/,$DIR/thumbnails/,")
+ gen "$f" "$THUMBNAIL"
+ fi
+done <<-EOF
+ $(find "$F" -type f \
+ -a -iname '*.jpeg' \
+ -o -iname '*.jpg' \
+ -o -iname '*.png' \
+ -o -iname '*.jpeg')
+EOF
diff --git a/watchdir b/watchdir
new file mode 100755
index 0000000..fe215b2
--- /dev/null
+++ b/watchdir
@@ -0,0 +1,52 @@
+#! /bin/sh
+
+usage()
+{
+ echo "$0 [-s <size>] [-r] [-h] [-d <subdir>] <dir>"
+}
+
+REGEN=0
+SIZE=96
+
+while getopts rs:d:h arg
+do
+ case $arg in
+ r) REGEN=1
+ ;;
+ s) SIZE="$OPTARG"
+ ;;
+ h) usage
+ exit 0
+ ;;
+ d) SUBDIR="$OPTARG"
+ ;;
+ ?) usage >&2
+ exit 1
+ ;;
+ esac
+done
+
+shift $(($OPTIND - 1))
+
+if [ $# != 1 ]; then
+ usage >&2
+ exit 1
+fi
+
+DIR="$1"
+
+[ "$REGEN" -eq 1 ] && "$(dirname $0)/tngen" \
+ ${SIZE:+-s$SIZE} \
+ -d "$DIR" \
+ "$DIR/user"
+
+while :
+do
+ F="$(inotifywait -e modify,move,create,delete \
+ --format "%w%f" -qr "$DIR/user/")"
+ sleep 1 # TODO: revisit this
+ "$(dirname $0)/tngen" \
+ ${SIZE:+-s$SIZE} \
+ -d "$DIR" \
+ "$F"
+done