From 295b30935465211eaf542f20406e39630db94108 Mon Sep 17 00:00:00 2001 From: Chad Date: Fri, 1 Sep 2023 06:01:01 -0700 Subject: [PATCH] Re-Enable Normal Firefox, Add Image Script (#46) * Add normal Firefox flatpak in case Librewolf is ever too strict. Keep having it installed from package in case the flatpak version still has issues. * Add missing interpretation. * Add a check which makes sure the user does not mistake a *compressed* file for being smaller. * Add a timestamp for when the program completed. * Add today's version of shrink.sh with an extra comment. * Add a comment regarding the final resolution. * Create file which will allow the full-scale downsizing of a media library. * Add goals and skeleton. * Rearrange variable setup. * Fix permissions. * Add most functionality, has not been fully tested. * Add spacing and a success message. * Program is testing really well, most functionality seems to exist. * BASH should handle this fine with "". * Fix spaces in names, add prompt to delete compressed files which grew. * Image compression program complete, remove old optionless shrink program. --- files/scripts/compress-images.sh | 170 ++++++++++++++++++ files/scripts/compress-video.sh | 25 ++- tasks/workstation/linux/software/flatpaks.yml | 38 ++-- tasks/workstation/shared/settings/gnome.yml | 29 +-- 4 files changed, 211 insertions(+), 51 deletions(-) create mode 100755 files/scripts/compress-images.sh diff --git a/files/scripts/compress-images.sh b/files/scripts/compress-images.sh new file mode 100755 index 0000000..128f472 --- /dev/null +++ b/files/scripts/compress-images.sh @@ -0,0 +1,170 @@ +#!/bin/bash +# 2023-08-31 Hyperling +# Lower resolution of images for uploading to websites or keeping in storage. +# Also see: compress-video.sh + +## Setup ## + +DIR="$(dirname -- "${BASH_SOURCE[0]}")" +PROG="$(basename -- "${BASH_SOURCE[0]}")" +echo "Running $DIR/$PROG" + +# Integers +typeset -i size status +size=2000 + +# Strings +tag="shrunk" +date_YYYYMMDD="`date "+%Y%m%d"`" +location="." +search="ls" +large_extension="DoNotUse-LargerThanOriginal" +large_created=".$PROG.large_created.true" + +## Functions ## + +function usage() { + # Hit the user with knowledge on how to use this program. + # Parameters: + # 1) The exit status to use. + status=$1 + echo "Usage: $PROG [-s SIZE] [-l LOCATION] [-r] [-f] [-d] [-h] [-x]" >&2 + cat <<- EOF + Compress JPG or PNG image(s). Can handle folders and work recursively. + + Parameters: + -s SIZE : Integer for the maximum length of either image dimension. + -l LOCATION : The specific image or folder which needs images shrunk. + -r : Recursively shrink images based on the location passed. + -f : Force the image to be shrunk even if a file already exists for it. + -d : Delete the original image if the compressed image is smaller. + -h : Display this usage text. + -x : Enable BASH debugging. + EOF + exit $status +} + +## Parameters ## + +while getopts ":s:l:rfdhx" opt; do + case $opt in + s) in_size="$OPTARG" && size="$in_size" ;; + l) location="$OPTARG" ;; + r) recurse="Y" && search="find" ;; + f) force="Y" ;; + d) delete="Y" ;; + h) usage 0 ;; + x) set -x ;; + *) echo "ERROR: Option $OPTARG not recognized." >&2 && usage 1 ;; + esac +done + +## Validations ## + +if [[ -n "$in_size" && "$size" != "$in_size" ]]; then + echo "ERROR: Size value '$in_size' included non-integer characters." >&2 + usage 1 +fi + +## Main ## + +# If using ls, make sure full path is passed to the loop by adding '/*'. +if [[ -z "$recurse" && -d "$location" && "$location" != *'/*' ]]; then + if [[ "$location" != *'/' ]]; then + location="${location}/" + fi + location="${location}*" +fi + +$search "$location" | sort | while read image; do + # Avoid processing directories no matter the name. + [ -d "$image" ] && continue + + # Avoid processing files previously shrunk. + [[ "$image" == *"$tag"* ]] && continue + + echo -e "\n$image" + + # Only look through JPG and PNG for now. + typeset -l extension + extension="${image##*.}" + if [[ "$extension" != *"jpg" + && "$extension" != *"jpeg" + && "$extension" != *"png" ]] + then + echo " SKIP: Sorry, currently only JPG and PNG are supported." + continue + fi + + new_image="${image//.$extension/}.$tag-$date_YYYYMMDD.$extension" + + # Delete the existing shrunk image if we are forcing a new compression. + if [[ -n "$force" && -e "$new_image" ]]; then + echo -n " FORCE: " + rm -v "$new_image" + fi + + # Skip if a compressed image was already created today. + if [[ -e "$new_image" ]]; then + echo " SKIP: Image has already been shrunk previously, moving on." + continue + fi + + # This modifies the image to be $size at its longest end, not be a square. + convert "$image" -resize ${size}x${size} "$new_image" + + # Check file sizes and if the new one is larger then flag it as large. + echo " Checking file sizes:" + ls -sh "$image" "$new_image" | sort -hr | while read line; do + echo " $line" + done + smaller_file=` + ls -sh "$image" "$new_image" | sort -h | awk '{print $2}' | head -n 1 + ` + if [[ "$smaller_file" == "$image" ]]; then + echo -n " WARNING: Conversion caused growth, original was likely lesser " + echo "quality. Adding a suffix to the file to signify that it may be bad." + echo -n " " + mv -v "$new_image" "$new_image.$large_extension" + touch "$large_created" + continue + fi + + echo " SUCCESS: Conversion succeeded, file has been compressed." + + if [[ -n "$delete" ]]; then + echo -n " DELETE: " + if [[ -d ~/TRASH ]]; then + mv -v "$image" ~/TRASH/ + else + rm -v "$image" + fi + fi +done + +# If large files do end up being created, allow the user to bulk delete them. +echo "FORTEST" +if [[ -e "$large_created" ]]; then + echo -e "\n*********************************************************" + echo -e "WARNING: The files below are larger than their originals!\n" + find "$location" -name "*"$large_extension + echo -e "*********************************************************" + + echo -en "\nWould you like to delete them? (Y/n): " + typeset -u confirm_delete + read confirm_delete + + if [[ -z "$confirm_delete" || "$confirm_delete" == "Y"* ]]; then + echo "" + find "$location" -name "*"$large_extension -exec rm -v {} \; + else + echo -e "\nKeeping files. Please use this if you change your mind:" + echo " find \"$location\" -name \"*\"$large_extension -exec rm -v {} \;" + fi + + rm "$large_created" +fi + +echo -e "\nDone!" + +exit 0 diff --git a/files/scripts/compress-video.sh b/files/scripts/compress-video.sh index 563ddca..ac6433c 100755 --- a/files/scripts/compress-video.sh +++ b/files/scripts/compress-video.sh @@ -2,6 +2,8 @@ # 2023-06-13 Hyperling # Compress a video to good-enough quality for high quality streaming. +## Setup ## + DIR=`dirname $0` PROG=`basename $0` if [[ "$DIR" == '.' ]]; then @@ -9,6 +11,9 @@ if [[ "$DIR" == '.' ]]; then fi echo "Running $DIR/$PROG" +filename_flag='compressed' +date_YYYYMMDD="`date "+%Y%m%d"`" + ## Functions ## function usage { @@ -93,11 +98,6 @@ if [[ -z "$time_command" ]]; then time_command="" fi -## Other Variables ## - -filename_flag='compressed' -date_YYYYMMDD="`date "+%Y%m%d"`" - ## Main ## if [[ "$verbose" == "Y" ]]; then @@ -159,12 +159,25 @@ $search_command $input | sort | while read file; do $time_command bash -c "ffmpeg -nostdin -hide_banner -loglevel quiet \ -i '$file' -b:v $video_bitrate -b:a $audio_bitrate \ $vcodec -movflags +faststart $newfile" + + # Check the filesize compared to the original and note if it is larger. + echo "Checking file sizes:" + ls -sh $file $newfile | sort -hr + smaller_file=`ls -sh $file $newfile | sort -h | awk '{print $2}' | head -n 1` + if [[ "$smaller_file" == "$file" ]]; then + echo -n "Conversion had the opposite effect, original was likely lesser " + echo "quality. Adding a suffix to the file to signify that it grew." + mv -v $newfile $newfile.DoNotUse-LargerThanOriginal + else + echo "Conversion succeeded, file has been compressed." + fi done -echo "\nDone!" +echo -e "\nDone!" # Display elapsed time if [[ -n "$time_command" ]]; then + date typeset -i hours minutes seconds hours=$(( SECONDS / 3600 )) minutes=$(( (SECONDS % 3600) / 60 )) diff --git a/tasks/workstation/linux/software/flatpaks.yml b/tasks/workstation/linux/software/flatpaks.yml index da858ff..031b513 100644 --- a/tasks/workstation/linux/software/flatpaks.yml +++ b/tasks/workstation/linux/software/flatpaks.yml @@ -40,6 +40,7 @@ - { app: "io.gitlab.librewolf-community", name: "librewolf", extra: "" } - { app: "chat.delta.desktop", name: "deltachat", extra: "" } - { app: "org.signal.Signal", name: "signal", extra: "" } + - { app: "org.mozilla.firefox", name: "firefox-flatpak", extra: "" } flatpaks_coding: - { app: "com.vscodium.codium", name: "codium", extra: "" } - { app: "com.google.AndroidStudio", name: "android-studio", extra: "" } @@ -53,7 +54,6 @@ - { app: "com.play0ad.zeroad", name: "zeroad", extra: "" } - { app: "net.supertuxkart.SuperTuxKart", name: "tuxkart", extra: "" } flatpaks_remove: - - { app: "org.mozilla.firefox", name: "firefox", extra: "" } - { app: "com.visualstudio.code", name: "vscode", extra: "" } # Why does this throw an error? It's the correct ID. - { app: "com.visualstudio.code-oss", name: "code-oss", extra: "" } - { app: "org.midori_browser.Midori", name: "midori", extra: "" } @@ -92,7 +92,7 @@ become_user: "{{ user }}" - name: Workstation | Linux | Flatpak Distro | Flatpak | Generic | Executable Permissions - file: + file: path: "{{ flatpak_exec_dir }}/{{ item.name }}" state: file mode: '0755' @@ -124,7 +124,7 @@ when: coding == true - name: Workstation | Linux | Flatpak Distro | Flatpak | Coding | Executable Permissions - file: + file: path: "{{ flatpak_exec_dir }}/{{ item.name }}" state: file mode: '0755' @@ -141,7 +141,7 @@ when: not coding == true - name: Workstation | Linux | Flatpak Distro | Flatpak | Coding | Remove Executables - file: + file: path: "{{ flatpak_exec_dir }}/{{ item.name }}" state: absent loop: "{{ flatpaks_coding }}" @@ -174,7 +174,7 @@ when: editing == true - name: Workstation | Linux | Flatpak Distro | Flatpak | Audio/Video Editors | Executable Permissions - file: + file: path: "{{ flatpak_exec_dir }}/{{ item.name }}" state: file mode: '0755' @@ -191,7 +191,7 @@ when: not editing == true - name: Workstation | Linux | Flatpak Distro | Flatpak | Audio/Video Editors | Remove Executables - file: + file: path: "{{ flatpak_exec_dir }}/{{ item.name }}" state: absent loop: "{{ flatpaks_editing }}" @@ -224,7 +224,7 @@ when: gaming == true - name: Workstation | Linux | Flatpak Distro | Flatpak | Gaming | Executable Permissions - file: + file: path: "{{ flatpak_exec_dir }}/{{ item.name }}" state: file mode: '0755' @@ -241,7 +241,7 @@ when: not gaming == true - name: Workstation | Linux | Flatpak Distro | Flatpak | Gaming | Remove Executables - file: + file: path: "{{ flatpak_exec_dir }}/{{ item.name }}" state: absent loop: "{{ flatpaks_gaming }}" @@ -259,7 +259,7 @@ ignore_errors: yes - name: Workstation | Linux | Flatpak Distro | Flatpak | Uninstalls | Remove Executables - file: + file: path: "{{ flatpak_exec_dir }}/{{ item.name }}" state: absent loop: "{{ flatpaks_remove }}" @@ -271,8 +271,8 @@ # Software not yet available or working properly in Flatpak form. - name: Workstation | Linux | Flatpak Distro | Package Manager | Install From Repo - package: - name: + package: + name: - "{{ firefox_esr }}" - vlc - "{{ appimagelauncher }}" @@ -281,7 +281,7 @@ state: present - name: Workstation | Linux | Flatpak Distro | Package Manager | Remove Firefox Normal - package: + package: name: - "{{ firefox }}" state: absent @@ -290,7 +290,7 @@ # Lutris # - name: Workstation | Linux | Flatpak Distro | Package Manager | Add Lutris PPA (Ubuntu) - apt_repository: + apt_repository: repo: ppa:lutris-team/lutris update_cache: yes state: present @@ -298,23 +298,23 @@ ignore_errors: yes - name: Workstation | Linux | Flatpak Distro | Package Manager | Add Lutris (besides ARM) - package: - name: + package: + name: - lutris state: present when: ansible_architecture != "aarch64" and gaming == true ignore_errors: yes - name: Workstation | Linux | Flatpak Distro | Package Manager | Remove Lutris (besides ARM) - package: - name: + package: + name: - lutris state: absent when: ansible_architecture != "aarch64" and gaming is not defined ignore_errors: yes - name: Workstation | Linux | Flatpak Distro | Package Manager | Remove Lutris PPA (Ubuntu) - apt_repository: + apt_repository: repo: ppa:lutris-team/lutris update_cache: yes state: absent @@ -324,7 +324,7 @@ # Remove Repo Software # - name: Workstation | Linux | Flatpak Distro | Package Manager | Remove Applications - package: + package: name: - "{{ thunderbird }}" - steam diff --git a/tasks/workstation/shared/settings/gnome.yml b/tasks/workstation/shared/settings/gnome.yml index 2adad92..296d698 100644 --- a/tasks/workstation/shared/settings/gnome.yml +++ b/tasks/workstation/shared/settings/gnome.yml @@ -1,32 +1,6 @@ --- # GNOME settings, extensions, and setup. -# Check for special software that may need added to favorites. -- name: Workstation | Account Management | GNOME | Facts | Defaults - set_fact: - browser: "com.github.Eloston.UngoogledChromium.desktop" - -- name: Workstation | Account Management | GNOME | Checks | Brave | Locate - shell: which brave-browser - register: brave - ignore_errors: yes - -- name: Workstation | Account Management | GNOME | Checks | Brave | Set As Browser - set_fact: - browser: "brave-browser.desktop" - when: not brave.failed - -#- name: Workstation | Account Management | GNOME | Checks | LibreWolf | Locate -# shell: which librewolf -# register: librewolf -# ignore_errors: yes -# -#- name: Workstation | Account Management | GNOME | Checks | LibreWolf | Set As Browser -# set_fact: -# browser: "io.gitlab.librewolf-community.desktop" -# when: not librewolf.failed or (flatpak_distro is defined and flatpak_distro) - - # Make sure Gnome-Tweaks is installed - name: Workstation | Account Management | GNOME | Install Dependencies package: @@ -163,13 +137,16 @@ become_user: "{{ user }}" ignore_errors: yes +# End block for Dash To Dock. +# The first Firefox is from Flatpak, and firefox_firefox.desktop is snap/apt. - name: Workstation | Account Management | GNOME + Cinnamon | Favorites (Linux) dconf: key: "{{ item }}" value: "[ 'org.gnome.Terminal.desktop', 'gnome-system-monitor.desktop' , 'org.gnome.Nautilus.desktop' , 'io.gitlab.librewolf-community.desktop' + , 'org.mozilla.firefox.desktop' , 'org.gnome.Evolution.desktop', 'chat.delta.desktop.desktop' , 'com.vscodium.codium.desktop', 'org.shotcut.Shotcut.desktop' , 'io.lbry.lbry-app.desktop'