Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added function to identify file type #6

Merged
merged 3 commits into from
Mar 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* 0.0.3 _Mar.03.2024_
* added pdf thumb generation [#4](https://github.com/iambumblehead/render-thumb-for.sh/pull/4)
* added font thumb generation [#5](https://github.com/iambumblehead/render-thumb-for.sh/pull/5)
* added function to return simplified filetype for file [#6](https://github.com/iambumblehead/render-thumb-for.sh/pull/6)
* 0.0.2 _Mar.02.2024_
* added video thumb generation [#2](https://github.com/iambumblehead/render-thumb-for.sh/pull/2)
* added audio thumb generation [#3](https://github.com/iambumblehead/render-thumb-for.sh/pull/3)
Expand Down
74 changes: 50 additions & 24 deletions render-thumb-for.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,44 @@ is_cmd_exiftool=$(command -v exiftool)
is_cmd_identify=$(command -v identify)
is_cmd_ffmpeg=$(command -v ffmpeg)

mimeTypeSVG="svg"
mimeTypeIMAGE="imgage"
mimeTypeVIDEO="video"
mimeTypeAUDIO="audio"
mimeTypeFONT="font"
mimeTypePDF="pdf"

timecode_re="([[:digit:]]{2}[:][[:digit:]]{2}[:][[:digit:]]{2})"
resolution_re="([[:digit:]]{2,8}[x][[:digit:]]{2,8})"

mime_video_re="video/"
mime_svg_re="image/svg"
mime_img_re="image/"
mime_audio_re="audio/"
mime_pdf_re="application/pdf"
mime_font_re="(ttf|truetype|opentype|woff|woff2|sfnt)$"


#default_wh="640 480"

img_dir="$HOME/.config/render-thumb-for"
if [ -n "${XDG_CONFIG_HOME}" ]; then
img_dir="$XDG_CONFIG_HOME/render-thumb-for"
fi

file_type_get () {
mime=$(file -b --mime-type "$1")

# font, pdf, video, audio, epub
if [[ $mime =~ ^"image/svg" ]]; then
echo "$mimeTypeSVG"
elif [[ $mime =~ ^"image/" ]]; then
echo "$mimeTypeIMAGE"
elif [[ $mime =~ ^"video/" ]]; then
echo "$mimeTypeVIDEO"
elif [[ $mime =~ ^"audio/" ]]; then
echo "$mimeTypeAUDIO"
elif [[ $mime =~ ^"application/pdf" ]]; then
echo "$mimeTypePDF"
elif [[ $mime =~ (ttf|truetype|opentype|woff|woff2|sfnt)$ ]]; then
echo "$mimeTypeFONT"
else
echo "unsupported"
fi
}

wh_max_get () {
IFS=" " read -r -a wh <<< "$1"

Expand Down Expand Up @@ -238,28 +258,34 @@ show_font () {
}

start () {
img_path=$1
path=$1
max_wh="$2 $3"
img_mimetype=$(file -b --mime-type "$img_path")

if [ ! -d "${img_dir}" ]; then
mkdir -p "${img_dir}"
fi

# font, pdf, video, audio, epub
if [[ $img_mimetype =~ ^$mime_svg_re ]]; then
img_sixel_paint "$img_path" "$max_wh"
elif [[ $img_mimetype =~ ^$mime_img_re ]]; then
img_sixel_paint_downscale "$img_path" "$max_wh"
elif [[ $img_mimetype =~ ^$mime_video_re ]]; then
show_video "$img_path" "$max_wh"
elif [[ $img_mimetype =~ ^$mime_audio_re ]]; then
show_audio "$img_path" "$max_wh"
elif [[ $img_mimetype =~ ^$mime_pdf_re ]]; then
show_pdf "$img_path" "$max_wh"
elif [[ $img_mimetype =~ $mime_font_re ]]; then
show_font "$img_path" "$max_wh"
fi
case $(file_type_get "$path") in
"$mimeTypeSVG")
img_sixel_paint "$path" "$max_wh"
;;
"$mimeTypeIMAGE")
img_sixel_paint_downscale "$path" "$max_wh"
;;
"$mimeTypeVIDEO")
show_video "$path" "$max_wh"
;;
"$mimeTypeAUDIO")
show_audio "$path" "$max_wh"
;;
"$mimeTypePDF")
show_pdf "$path" "$max_wh"
;;
"$mimeTypeFONT")
show_font "$path" "$max_wh"
;;
*)
esac
}

#start "/home/bumble/software/Guix_logo.png" 800 400
Expand Down