Skip to content

Instantly share code, notes, and snippets.

@h8rt3rmin8r
Created February 7, 2019 11:15
Show Gist options
  • Save h8rt3rmin8r/36fb877ea17cf19b165bd48d9b34d298 to your computer and use it in GitHub Desktop.
Save h8rt3rmin8r/36fb877ea17cf19b165bd48d9b34d298 to your computer and use it in GitHub Desktop.
Convert files into base64 data URLs or extract underlying data from data URLs.
#! /bin/bash
#=======================================================================================#
#
# ___________.___.____ ___________________ ____ _____________.____
# \_ _____/| | | \_ _____/\_____ \| | \______ \ |
# | __) | | | | __)_ / ____/| | /| _/ |
# | \ | | |___ | \/ \| | / | | \ |___
# \___ / |___|_______ \/_______ /\_______ \______/ |____|_ /_______ \
# \/ \/ \/ \/ \/ \/
#
#=======================================================================================#
#
# FILE2URL
#
# Convert files into base64 data URLs or extract underlying data from data URLs.
#
# This script can process both remote files (URL's) and local file references.
# All outputs are sent directly to the terminal.
#
# (Created by h8rt3rmin8r on 20190207)
#
# USAGE
# _______________________________________________________________________________
# | | |
# | [ Command ] | [ Outcome ] |
# |________________________________|______________________________________________|
# | | |
# | ./file2url.sh <URL> | Encode a remote file located at <URL> |
# | | |
# | ./file2url.sh <FILE> | Encode a local file located at <FILE> |
# | | |
# | echo <DAT> | ./file2url.sh | Decode an encoded data URL, <DAT> |
# |________________________________|______________________________________________|
#
#=======================================================================================#
# VARIABLE DECLARATIONS
DT="`date '+%Y%m%d%H%M%S'`"
SRC="$1"
DL="${DT}-download"
SELF="[file2url]"
VB_H="Use '--help' for more information."
VB_E01="ERROR: No input detected! ${VB_H}"
VB_E02="ERROR: Input was not a file or valid URL reference! ${VB_H}"
# FUNCTION DECLARATIONS
function help_text() {
# Script help text function
clear
echo
cat $0 | head -n 34 | tail -n +2 | grep '^#' | tr '#' ' '
return
}
function check_mime() {
# Check the mime type of a designated file
file --mime-type -b "$1"
return
}
function run_remote() {
# Process a remote image file
wget -q -O "${DL}" "${SRC}"
MIME_TYPE="$(check_mime ${DL})"
OUT_A=$(echo "data:${MIME_TYPE};base64,")
OUT_B=$(cat "${DL}" | base64 | tr -d '\n')
echo "${OUT_A}${OUT_B}" | tr -d '\n'
rm "${DL}" &>/dev/null
return
}
function run_local() {
# Process a local image file
MIME_TYPE="$(check_mime ${SRC})"
OUT_A=$(echo "data:${MIME_TYPE};base64,")
OUT_B=$(cat "${SRC}" | base64 | tr -d '\n')
echo "${OUT_A}${OUT_B}" | tr -d '\n'
return
}
# INPUT VALIDATION AND OPERATIONS EXECUTION
if [[ -t 0 ]];
then
if [[ "x${SRC}" == "x" ]]; then echo "${SELF} ${VB_E01}"; exit 1; fi
if [[ "${SRC}" == "-h" || "${SRC}" == "-help" || "${SRC}" == "--help" ]];
then
help_text
exit 0
fi
if [[ -f "${SRC}" ]]; then run_local; exit 0; fi
if [[ "$(echo -n ${SRC} | grep '^http' | wc -c)" -gt 4 && "${SRC}" =~ '://' ]];
then run_remote; exit 0
else echo "${SELF} ${VB_E02}"; exit 1
fi
else
echo -n $(< /dev/stdin) | cut -d ',' -f2 | base64 -d
exit 0
fi
#=======================================================================================#
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment