From 7f347f63fe53fef285377addc0da3a68b11312a6 Mon Sep 17 00:00:00 2001 From: Bastian de Byl Date: Thu, 12 Dec 2019 12:01:44 -0500 Subject: [PATCH] Removed thumbnails make target, updated make-thumbs.sh script for portability --- Makefile | 5 +-- make-thumbs.sh | 82 ++++++++++++++++++++++++++++++++++++++++++-------- 2 files changed, 71 insertions(+), 16 deletions(-) diff --git a/Makefile b/Makefile index d98685b..60ff7a0 100644 --- a/Makefile +++ b/Makefile @@ -44,9 +44,6 @@ version: new: $(DOCKER_RUN) ${HUGO_IMAGE} new post/$(shell read -p "Post Name (i.e. my_post.md): " pn; echo $$pn) -thumbnails: - @./make-thumbs.sh - clean: @# Clean up existing generated site rm -rf public/ resources/ @@ -60,5 +57,5 @@ cache: @$(DOCKER_RUN) ${AWS_ENV} ${AWS_IMAGE} ${CLOUDFRONT_CMD} # Default target for make (<=3.80) -.PHONY: static unmount build _run run version new thumbnails clean deploy cache +.PHONY: static unmount build _run run version new clean deploy cache default: build diff --git a/make-thumbs.sh b/make-thumbs.sh index b328016..e9798fa 100755 --- a/make-thumbs.sh +++ b/make-thumbs.sh @@ -1,26 +1,84 @@ -#!/bin/bash +#!/bin/sh +{ usage="$(cat)"; }<<'EOF' +USAGE + make-thumbs.sh [OPTIONS] + +DESCRIPTION + Recursively searches through the passed path, ignoring existing thumbnails, + and generates thumbnails for images greater than 600px in width. + +OPTIONS + -h, --help Shows this help prompt + -d, --dry-run Dry-run that will not create actual thumbnails +EOF + +die() { + printf '%s\n' "$1" >&2 + exit 1 +} + +show_help() { + printf '%s\n' "$usage" + exit +} + +while :; do + case $1 in + -h|-\?|--help) + show_help + exit + ;; + -d|--dry-run) + DRYRUN=1 + ;; + --) + shift + break + ;; + -?*) + printf 'WARN: Unknown option (ignored): %s\n' "$1" >&2 + ;; + *) + if [ "$1" ]; then + BROWSE_PATH=$1 + else + break + fi + ;; + esac + + shift +done + +if [ -z "$BROWSE_PATH" ]; then + die 'ERROR: path not specified! (See -h/--help)' +fi + CONVERT=$(command -v convert) -if [ ! $CONVERT ]; then +if [ ! "$CONVERT" ]; then echo "ERROR: imagemagick must be installed!" exit 1 fi -for i in $(find static/img/*/ -type f -not -path "*thumb*"); do - THUMB_PATH=$(dirname $i)/thumb - IMG_NAME=$(basename $i) +find "$BROWSE_PATH" -type f -not -path '*thumb*' | while read -r i; do + THUMB_PATH="$(dirname "$i")/thumb" + IMG_NAME="$(basename "$i")" # Generate a thumbnail if the width is greater than 600px - if [ $(identify -format "%w" $i) -gt 600 ]; then + if [ "$(identify -format "%w" "$i")" -gt 600 ]; then # Create the thumbnail directory fo the image to be made - if [ ! -d $THUMB_PATH ]; then - echo "Creating directory $THUMB_PATH..." - mkdir -p $THUMB_PATH + if [ ! -d "$THUMB_PATH" ]; then + if [ ! "$DRYRUN" ]; then + mkdir -p "$THUMB_PATH" + fi fi # Create the thumbnail image - if [ ! -f $THUMB_PATH/$IMG_NAME ]; then - echo "Converting $IMG_NAME to thumbnail..." - $CONVERT -resize 600x $i $THUMB_PATH/$IMG_NAME; + if [ ! -f "$THUMB_PATH/$IMG_NAME" ]; then + printf "└─ Converting %s to thumbnail in %s \n" "$BROWSE_PATH/$IMG_NAME" "$THUMB_PATH" + if [ ! "$DRYRUN" ]; then + "$CONVERT" -resize 600x "$i" "$THUMB_PATH/$IMG_NAME"; + fi fi fi done