mirror of
https://github.com/tanrax/bash-folders.git
synced 2025-10-10 18:25:50 +02:00
Small syntax/indentation and other obvious cleanups before rewriting
This commit is contained in:
@@ -1,98 +1,67 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
# --
|
||||
# Description: Script that watches when new image (PNG or JPEG) are added and transform to WebP format.
|
||||
# --
|
||||
# Requirements: Install webp
|
||||
# Example Debian: $sudo apt install webp
|
||||
# --
|
||||
# Cron: @reboot bash-folders-image-to-webp.sh >/dev/null 2>&1 &
|
||||
# --
|
||||
|
||||
# START
|
||||
set -e
|
||||
|
||||
|
||||
# FUNCTIONS
|
||||
|
||||
usage() {
|
||||
if [ "$*" != "" ] ; then
|
||||
echo "Error: $*"
|
||||
fi
|
||||
[[ -n "${*}" ]] && printf '%s\n' "Error: ${*}" >&2
|
||||
|
||||
cat << EOF
|
||||
Usage: $PROGNAME [OPTION]
|
||||
Watches when new image (PNG or JPEG) are added and transform to WebP format.
|
||||
Options:
|
||||
--folder [path] Folder path where will be monitored.
|
||||
--help Display this usage message and exit
|
||||
USAGE: ${0##*/} [OPTION] --folder PATH
|
||||
|
||||
Watches when new image (PNG or JPEG) are added and transform to WebP format
|
||||
|
||||
OPTIONS:
|
||||
--help Display this usage message and exit
|
||||
--folder PATH PATH to be monitored
|
||||
EOF
|
||||
|
||||
exit 1
|
||||
}
|
||||
|
||||
start() {
|
||||
# Monitors the selected folder
|
||||
inotifywait -m -e create,moved_to --format '%f' "$FOLDER_ORIGIN" |
|
||||
while read -r filename; do
|
||||
# Gets the file extension
|
||||
extension="${filename##*.}"
|
||||
# Checks if the extension is in the extension list
|
||||
for ext in "${EXTENSIONS_TO_WATCH[@]}"; do
|
||||
if [[ "$ext" = "$extension" ]]; then
|
||||
filename_output="${filename%.*}.webp"
|
||||
# Displays a flat file of information
|
||||
touch "$FOLDER_ORIGIN/$MESSAGE_WAITING"
|
||||
# Converts the image to WebP
|
||||
cwebp -q "$QUALITY" "$FOLDER_ORIGIN/$filename" -o "$FOLDER_ORIGIN/$filename_output"
|
||||
# Remove a flat file of information
|
||||
rm "$FOLDER_ORIGIN/$MESSAGE_WAITING"
|
||||
fi
|
||||
done
|
||||
done
|
||||
inotifywait -m -e create,moved_to --format '%f' "$FOLDER_ORIGIN" | while read -r filename; do
|
||||
extension="${filename##*.}"
|
||||
for ext in "${EXTENSIONS_TO_WATCH[@]}"; do
|
||||
if [[ "$ext" == "$extension" ]]; then
|
||||
filename_output="${filename%.*}.webp"
|
||||
touch "$FOLDER_ORIGIN/$MESSAGE_WAITING"
|
||||
cwebp -q "$QUALITY" "$FOLDER_ORIGIN/$filename" -o "$FOLDER_ORIGIN/$filename_output"
|
||||
rm "$FOLDER_ORIGIN/$MESSAGE_WAITING"
|
||||
fi
|
||||
done
|
||||
done
|
||||
}
|
||||
|
||||
|
||||
# CONTROLE ARGUMENTS
|
||||
|
||||
# Parse command line arguments
|
||||
while [[ $# -gt 0 ]]
|
||||
do
|
||||
key="$1"
|
||||
case $key in
|
||||
--folder)
|
||||
FOLDER_ORIGIN="$2"
|
||||
shift # past argument
|
||||
shift # past value
|
||||
;;
|
||||
--quality)
|
||||
QUALITY="$2"
|
||||
shift # past argument
|
||||
shift # past value
|
||||
;;
|
||||
*)
|
||||
usage "Unknown option: $1"
|
||||
;;
|
||||
--folder)
|
||||
FOLDER_ORIGIN="$2"
|
||||
shift 2
|
||||
;;
|
||||
--quality)
|
||||
QUALITY="$2"
|
||||
shift 2
|
||||
;;
|
||||
*)
|
||||
usage "Unknown option: $1"
|
||||
;;
|
||||
esac
|
||||
done
|
||||
|
||||
# VARIABLES
|
||||
PROGNAME=$(basename "$0")
|
||||
QUALITY="90"
|
||||
MESSAGE_WAITING="converting_please_wait"
|
||||
EXTENSIONS_TO_WATCH=("jpg" "jpeg" "png")
|
||||
|
||||
# CHECKS
|
||||
|
||||
# Check if exists cwebp
|
||||
if ! which cwebp > /dev/null; then
|
||||
echo "Error: You must install the WebP terminal tools."
|
||||
if ! command -v cwebp > /dev/null; then
|
||||
printf '%s\n' "Error: You must install WebP tooling" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Check if the required --folder flag is provided
|
||||
if [ -z "$FOLDER_ORIGIN" ]; then
|
||||
echo "Error: The --folder flag is required."
|
||||
if [[ -z "$FOLDER_ORIGIN" ]]; then
|
||||
printf '%s\n' "Error: The --folder flag is required" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
|
Reference in New Issue
Block a user