Files
dotfiles/local/bin/x-thumbgen
Ismo Vuorinen 6d72003446 fix(lint): fix all sonarcloud detected issues (#279)
* fix(ci): replace broad permissions with specific scopes in workflows

Replace read-all/write-all with minimum required permission scopes
across all GitHub Actions workflows to follow the principle of least
privilege (SonarCloud rule githubactions:S8234).

* fix(shell): use [[ instead of [ for conditional tests

Replace single brackets with double brackets in bash conditional
expressions across 14 files (28 changes). All scripts use bash
shebangs so [[ is safe everywhere (SonarCloud rule shelldre:S7688).

* fix(shell): add explicit return statements to functions

Add return 0 as the last statement in ~46 shell functions across
17 files that previously relied on implicit return codes
(SonarCloud rule shelldre:S7682).

* fix(shell): assign positional parameters to local variables

Replace direct $1/$2/$3 usage with named local variables in _log(),
msg(), msg_err(), msg_done(), msg_run(), msg_ok(), and array_diff()
(SonarCloud rule shelldre:S7679).

* fix(python): replace dict() constructor with literal

Use {} instead of dict() for empty dictionary initialization
(SonarCloud rule python:S7498).

* fix(shell): fix husky shebang and tolerate npm outdated exit code

* docs(shell): add function docstring comments

* fix(shell): fix heredoc indentation in x-sonarcloud

* feat(python): add ruff linter and formatter configuration

* fix(ci): align megalinter config with biome, ruff, and shfmt settings

* fix(ci): disable black and yaml-prettier in megalinter config

* chore(ci): update ruff-pre-commit to v0.15.0 and fix hook name

* fix(scripts): check for .git dir before skipping clone in install-fonts

* fix(shell): address code review issues in scripts and shared.sh

- Guard wezterm show-keys failure in create-wezterm-keymaps.sh
- Stop masking git failures with return 0 in install-cheat-purebashbible.sh
- Add missing shared.sh source in install-xcode-cli-tools.sh
- Replace exit 1 with return 1 in sourced shared.sh

* fix(scripts): address code review and security findings

- Guard wezterm show-keys failure in create-wezterm-keymaps.sh
- Stop masking git failures with return 0 in install-cheat-purebashbible.sh
- Add missing shared.sh source in install-xcode-cli-tools.sh
- Replace exit 1 with return 1 in sourced shared.sh
- Remove shell=True subprocess calls in x-git-largest-files.py

* style(shell): apply shfmt formatting and add args to pre-commit hook

* fix(python): suppress bandit false positives in x-git-largest-files

* fix(python): add nosemgrep suppression for check_output call

* feat(format): add prettier for YAML formatting

Install prettier, add .prettierrc.json config (200-char width, 2-space
indent, LF endings), .prettierignore, yarn scripts (lint:prettier,
fix:prettier, format:yaml), and pre-commit hook scoped to YAML files.

* style(yaml): apply prettier formatting

* fix(scripts): address remaining code review findings

- Python: use list comprehension to filter empty strings instead of
  slicing off the last element
- create-wezterm-keymaps: write to temp file and mv for atomic updates
- install-xcode-cli-tools: fix shellcheck source directive path

* fix(python): sort imports alphabetically in x-git-largest-files

* fix(lint): disable PYTHON_ISORT in MegaLinter, ruff handles it

* chore(git): add __pycache__ to gitignore

* fix(python): rename ambiguous variable l to line (E741)

* style: remove trailing whitespace and blank lines

* style(fzf): apply shfmt formatting

* style(shell): apply shfmt formatting

* docs(plans): add design documents

* style(docs): add language specifier to fenced code block

* feat(lint): add markdown-table-formatter to dev tooling

Add markdown-table-formatter as a dev dependency with yarn scripts
(lint:md-table, fix:md-table) and a local pre-commit hook to
automatically format markdown tables on commit.
2026-02-07 19:01:02 +02:00

197 lines
5.4 KiB
Bash
Executable File

#!/usr/bin/env bash
#
# Generate thumbnails using ImageMagick (magick) with MIME type filtering.
# https://imagemagick.org/script/download.php
#
# This script recursively processes images in a given directory (and its subdirectories)
# by using the `mimetype` command to detect file types. Files with MIME types that are not
# supported by ImageMagick (as defined in the ALLOWED_MIMETYPES array) are skipped.
#
# Defaults (can be overridden by environment variables or command-line options):
# THUMB_SOURCE: Directory with images (provided as a positional argument)
# THUMB_OUTPUT: Directory to store thumbnails (default: same as THUMB_SOURCE)
# THUMB_BACKGROUND: Background color (default: white)
# THUMB_RESIZE: Resize dimensions (default: 800x800)
# THUMB_EXTENT: Canvas dimensions (default: 1000x1000)
# THUMB_SUFFIX: Suffix appended to filename (default: _thumb)
#
# Options:
# -o output_directory Specify the output directory for thumbnails (default: same as source).
# -s suffix Specify a custom suffix for thumbnail filenames (default: _thumb).
# -h, --help Display this help message and exit.
#
# Example:
# THUMB_BACKGROUND=black x-thumbgen.sh -o ~/thumbnails ~/images/
#
# Author: Ismo Vuorinen <https://github.com/ivuorinen> 2015
# Improved in 2025
set -euo pipefail
# Display usage information and options
usage()
{
cat << EOF
Usage: $0 [options] source_directory
Options:
-o output_directory Specify the output directory for thumbnails (default: same as source).
-s suffix Specify a custom suffix for thumbnail filenames (default: _thumb).
-h, --help Display this help message and exit.
EOF
exit 1
}
# Default values (can be overridden by ENV variables)
THUMB_SOURCE=""
THUMB_OUTPUT=""
THUMB_BACKGROUND="${THUMB_BACKGROUND:-white}"
THUMB_RESIZE="${THUMB_RESIZE:-800x800}"
THUMB_EXTENT="${THUMB_EXTENT:-1000x1000}"
THUMB_SUFFIX="${THUMB_SUFFIX:-_thumb}"
# List of MIME types supported by ImageMagick (adjust as needed)
ALLOWED_MIMETYPES=("image/jpeg" "image/png" "image/gif" "image/bmp" "image/tiff" "image/webp")
# Verify ImageMagick is available
check_magick_installed()
{
if ! command -v magick &> /dev/null; then
echo "Error: 'magick' command not found. Please install ImageMagick from https://imagemagick.org/script/download.php" >&2
exit 1
fi
}
# Verify mimetype command is available
check_mimetype_installed()
{
if ! command -v mimetype &> /dev/null; then
echo "Error: 'mimetype' command not found. Please install it (e.g. via 'sudo apt install libfile-mimeinfo-perl' on Debian/Ubuntu)." >&2
exit 1
fi
}
# Helper function to check if a given MIME type is allowed
is_supported_mimetype()
{
local mt=$1
for allowed in "${ALLOWED_MIMETYPES[@]}"; do
if [[ "$mt" == "$allowed" ]]; then
return 0
fi
done
return 1
}
# Parse command-line options using getopts
parse_options()
{
while getopts ":o:s:h-:" opt; do
case $opt in
o)
THUMB_OUTPUT="$OPTARG"
;;
s)
THUMB_SUFFIX="$OPTARG"
;;
h)
usage
;;
-)
if [[ "$OPTARG" == "help" ]]; then
usage
else
echo "Error: Unknown option --$OPTARG" >&2
usage
fi
;;
\?)
echo "Error: Invalid option -$OPTARG" >&2
usage
;;
:)
echo "Error: Option -$OPTARG requires an argument." >&2
usage
;;
esac
done
shift $((OPTIND - 1))
# The remaining argument should be the source directory.
if [ $# -lt 1 ]; then
echo "Error: Source directory is required." >&2
usage
fi
THUMB_SOURCE="$1"
}
# Generate thumbnails recursively using find and filtering by MIME type
generate_thumbnails()
{
local source_dir="$1"
local output_dir="$2"
# Ensure the output directory exists (create if necessary)
if [ ! -d "$output_dir" ]; then
mkdir -p "$output_dir"
fi
# Recursively find all files.
while IFS= read -r -d '' file; do
# Use mimetype to determine the file's MIME type.
file_mimetype=$(mimetype -b "$file")
if ! is_supported_mimetype "$file_mimetype"; then
echo "Skipping unsupported MIME type '$file_mimetype' for file: $file" >&2
continue
fi
# Determine the relative path with respect to the source directory.
rel_path="${file#"$source_dir"/}"
dir="$(dirname "$rel_path")"
base="$(basename "$rel_path")"
filename="${base%.*}"
ext="${base##*.}"
# Create corresponding output subdirectory
out_dir="${output_dir}/${dir}"
mkdir -p "$out_dir"
outfile="${out_dir}/${filename}${THUMB_SUFFIX}.${ext}"
echo "Processing '$file' -> '$outfile'..."
magick "$file" \
-resize "$THUMB_RESIZE" \
-background "$THUMB_BACKGROUND" \
-gravity center \
-extent "$THUMB_EXTENT" \
"$outfile"
done < <(find "$source_dir" -type f -print0)
}
# Parse options, validate inputs, and generate thumbnails
main()
{
parse_options "$@"
if [ -z "$THUMB_SOURCE" ]; then
echo "Error: Source directory not specified." >&2
usage
fi
if [ ! -d "$THUMB_SOURCE" ]; then
echo "Error: Source directory '$THUMB_SOURCE' does not exist or is not accessible." >&2
exit 1
fi
# If output directory is not specified, default to the source directory.
if [ -z "$THUMB_OUTPUT" ]; then
THUMB_OUTPUT="$THUMB_SOURCE"
fi
check_magick_installed
check_mimetype_installed
generate_thumbnails "$THUMB_SOURCE" "$THUMB_OUTPUT"
}
main "$@"