From 3b2b8bf37b8dbb803fdbb9091cab57df66af9518 Mon Sep 17 00:00:00 2001 From: chris Date: Tue, 5 Mar 2024 11:06:35 -0800 Subject: [PATCH 1/3] added function to identify file type --- CHANGELOG.md | 1 + render-thumb-for.sh | 74 ++++++++++++++++++++++++++++++--------------- 2 files changed, 51 insertions(+), 24 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 86d1631..59e21bd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/render-thumb-for.sh b/render-thumb-for.sh index 6e6c6e3..08a6332 100644 --- a/render-thumb-for.sh +++ b/render-thumb-for.sh @@ -5,17 +5,16 @@ 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" +mimeTypeAUDIO="pdf" +mimeTypeFONT="font" + 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" @@ -23,6 +22,27 @@ 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" @@ -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 From a07b1907dbc090ed224bce5f15613eb4d8d81d47 Mon Sep 17 00:00:00 2001 From: chris Date: Tue, 5 Mar 2024 11:09:15 -0800 Subject: [PATCH 2/3] shellcheck lint related changes --- render-thumb-for.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/render-thumb-for.sh b/render-thumb-for.sh index 08a6332..583102c 100644 --- a/render-thumb-for.sh +++ b/render-thumb-for.sh @@ -9,8 +9,8 @@ mimeTypeSVG="svg" mimeTypeIMAGE="imgage" mimeTypeVIDEO="video" mimeTypeAUDIO="audio" -mimeTypeAUDIO="pdf" mimeTypeFONT="font" +mimeTypePDF="pdf" timecode_re="([[:digit:]]{2}[:][[:digit:]]{2}[:][[:digit:]]{2})" resolution_re="([[:digit:]]{2,8}[x][[:digit:]]{2,8})" From 481aa1b28d9f0045510774d9caa20f7eb2e82976 Mon Sep 17 00:00:00 2001 From: chris Date: Tue, 5 Mar 2024 11:10:56 -0800 Subject: [PATCH 3/3] shellcheck lint related changes --- render-thumb-for.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/render-thumb-for.sh b/render-thumb-for.sh index 583102c..21969d2 100644 --- a/render-thumb-for.sh +++ b/render-thumb-for.sh @@ -36,7 +36,7 @@ file_type_get () { echo "$mimeTypeAUDIO" elif [[ $mime =~ ^"application/pdf" ]]; then echo "$mimeTypePDF" - elif [[ $mime =~ "(ttf|truetype|opentype|woff|woff2|sfnt)$" ]]; then + elif [[ $mime =~ (ttf|truetype|opentype|woff|woff2|sfnt)$ ]]; then echo "$mimeTypeFONT" else echo "unsupported"