blob: 49258048413d7a0a5114ded11d94262034bc9b56 (
plain) (
tree)
|
|
#!/bin/bash
#
# "THE BEER-WARE LICENSE" (Revision 42):
# Matěj Cepl, <mcepl@cepl.eu> wrote this file. As long as you retain
# this notice you can do whatever you want with this stuff. If we meet
# some day, and you think this stuff is worth it, you can buy me a beer
# in return. Matěj Cepl
#
# Instructions for setting up appropriate folders on
# file.rdu.redhat.com see https://mojo.redhat.com/docs/DOC-14590
# See also variable URL_VARIANTS below for necessary configuration.
set -eu
DEBUG=1
debug() {
if [ $DEBUG -eq 1 ] ; then
printf "%s\n" "$@"
fi
}
error() {
printf "%s\n" "$@"
exit 1
}
COMPRESSED_EXTENSIONS=('.jpg' '.webm' '.ogg' '.gz' '.bz2' '.zip' \
'.xz' '.odp' '.odt' '.ods' '.mp4' '.mp3' \
'.png' '.pdf' '.apk' '.rpm' '.epub' '.mkv' \
'.avi' '.jar' '.opus' '.ogv' '.flac' '.msi' \
'.m4a')
# THE VARIABLE URL_VARIANTS IS MEANT TO BE EDITED. Change various
# attributes to correspond to your situation.
# If new section is added (as here I have also 'tmp' for fedorapeople.org)
# and the script has a symlink named as the section (so I have tmp as
# a symlink to this script) it saves the file to the location defined in
# such section when called under the other name.
# So, `tmp filename` saves file filename to fedorapeople for me.
export URL_VARIANTS="{
'barstool': {
'base_url': 'mcepl@shell.eng.rdu.redhat.com:public_html/',
'target_url': 'http://file.rdu.redhat.com/~mcepl/',
'shorten_api': 'https://url.corp.redhat.com/new?'
},
'wotan': {
'base_url': 'wotan:Export/',
'target_url': 'https://w3.suse.de/~mcepl/'
},
'tmp': {
'base_url': 'fedorapeople.org:public_html/tmp/',
'target_url': 'https://mcepl.fedorapeople.org/tmp/',
'shorten_api': 'https://da.gd/s?url='
}
}"
# THE ALTERNATIVE WAY
#
# barstool_base_url='mcepl@shell.eng.rdu.redhat.com:public_html'
# barstool_target_url='http://file.rdu.redhat.com/~mcepl/'
# barstool_shorten_api='https://url.corp.redhat.com/new?'
#
# wotan_base_url='wotan:Export/'
# wotan_target_url='https://w3.suse.de/~mcepl/'
#
# tmp_base_url='fedorapeople.org:public_html/tmp/'
# tmp_target_url='https://mcepl.fedorapeople.org/tmp/'
# # tmp_shorten_api='http://is.gd/create.php?format=simple&url='
# tmp_shorten_api='https://da.gd/s?url='
#
# get() { local x; x="${cur_conf}_$1"; echo "${!x}"; }
#
# Example usage:
#
# $ cur_conf=barstool
#
# $ get base_url
# mcepl@shell.eng.rdu.redhat.com:public_html
#
# $ get target_url
# http://file.rdu.redhat.com/~mcepl/
#
# $ cur_conf=wotan
#
# $ get base_url
# wotan:Export/
# Requires GNU sed
CMDNAME="$(basename "$0"|sed 's/./\L&/g')"
CONFIG="$(echo "$URL_VARIANTS" |tr "'" '"'|jq -r ".$CMDNAME")"
BASE_URL="$(echo "$CONFIG" | jq -r ".base_url")"
TARGET_URL="$(echo "$CONFIG" | jq -r ".target_url")"
[ $# -lt 1 ] && exit 1
for processed_file in "${@:1}" ; do
st=$(stat -c "%s" "$processed_file")
base_name="$(basename -- "$processed_file")"
file_ext="${base_name##*.}"
debug "size of $processed_file is ${st} bytes"
debug "extension is ${file_ext}"
# shellcheck disable=SC2076
if [ "$st" -gt 1000000 ] && [[ ! " ${COMPRESSED_EXTENSIONS[*]} " =~ " ${file_ext} " ]] ; then
debug "file ${processed_file} should be compressed (${st} b)"
gzip --keep --best "${processed_file}" || error 'Compressing the file failed!'
fname="${processed_file}.gz"
COMPRESSED=1
else
debug "file ${processed_file} should not be compressed (${st} b)"
fname="${processed_file}"
COMPRESSED=0
fi
chmod 644 "$fname"
# To make scp happy
modified_fname="$(echo "${fname}"|tr ':' '_')"
mv -n "${fname}" "${modified_fname}"
debug "Unshorten fname=${modified_fname}"
scp "${modified_fname}" "${BASE_URL}"
target_URL="${TARGET_URL}$(basename "${modified_fname}")"
# Make target_URL into a safe URL
safe_target_URL="${target_URL// /%20}"
echo "${safe_target_URL}"
# curl -s 'http://is.gd/create.php?format=simple&url=www.example.com'
# http://is.gd/MOgh5q
SHORTEN_API="$(echo "$CONFIG" | jq -r ".shorten_api")"
if [ "x${SHORTEN_API}" != "xnull" ] ; then
shortened_URL="$(curl -s "${SHORTEN_API}""${safe_target_URL}")"
echo "${shortened_URL}"
fi
# The script should have no side-effects
if [ "${COMPRESSED}" -eq 1 ] ; then
rm "${modified_fname}"
else
mv "${modified_fname}" "${fname}"
fi
done
|