directory-thumbnailer 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. cat /usr/local/bin/directory-thumbnailer
  2. #!/bin/bash
  3. IFS=$'\n\t'
  4. # not enough args
  5. if [ $# -lt 3 ]; then
  6. exit 1
  7. fi
  8. # extract args for execution
  9. size="$1"
  10. in="$2"
  11. out="$3"
  12. # pre-flight check that $in is not a home user folder (we don't want to modify those)
  13. if [ "$HOME" = "$(dirname $in)" ]; then
  14. exit 0
  15. fi
  16. # fine image files that we can extract
  17. # @todo: add support for video files
  18. files=()
  19. for f in "$(find "$in" -maxdepth 1 -type f -exec file {} \; | grep -o -P '^.+: \w+ image' | sort -n | head -4 | cut -d':' -f1)"; do
  20. files+=($f)
  21. done
  22. [ ${#files[*]} -gt 0 ] || exit 1
  23. # extract theme or use gnome as default
  24. config="${XDG_CONFIG_HOME:-$HOME/.config}/gtk-3.0/settings.ini"
  25. icon_theme="$(grep 'gtk-icon-theme-name' "$config" | sed 's/.*\s*=\s*//')"
  26. icon_theme="${icon_theme:-gnome}"
  27. # @todo: add behavior to try to get appropriate size first, else fallback to assumed largest/last
  28. folder_icon=$(find "/usr/share/icons/$icon_theme" -name "folder.*" | sort -d | tail -1)
  29. # check if folder icon exists
  30. # @todo: consider using known gnome folder icon as fallback
  31. if [ ! -f "$folder_icon" ]; then
  32. exit 1
  33. fi
  34. # generate the thumbnail
  35. convert -background none "$folder_icon" -resize $size <(montage -geometry "$(($size * 60 / 200))x$(($size * 60 / 300))+2+2" -alpha on -background none -tile 2 ${files[*]} png:-) -gravity center -composite "$out" 2>/dev/null